|
Unless you are going to be programming large scale complex mathematical problems you should stay well clear of floating point numbers. There are quite a few issues to be understood which can catch you out. And given this and the question below, your time would be better spent getting hold of a good C++ study guide. Trying to learn by posting questions here will take you far too much time.
|
|
|
|
|
Try here.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
Hi,
I am a newbee to c++.
iostream include definition of cout and cin
But if I write cout without std, It throws error, which should be corrcted to std::cout.
Why so?
|
|
|
|
|
In order to have insight on <iostream > content, feel free to check out the documentation: <iostream> - C++ Reference[^].
std is the namespace[^] of the C++ Standard Library , quoting Wikipedia[^]: "Features of the C++ Standard Library are declared within the std namespace".
In order to use <iostream> objects, e.g. cout you need either to
or
modified 29-Nov-17 6:44am.
|
|
|
|
|
Hi,
But how the std and cout is related?
|
|
|
|
|
cout belongs to std namespace.
There is no global cout , there is just std::cout .
|
|
|
|
|
I would suggest that as a "newbee" to C++ that you should just consider the answer to this question to be 'magic'.
The specifics of how this works this way and why it works is quite complicated and requires not only that you understand quite a bit of basic C++ but also that you understand a bit more about how programming works.
After that then you can go back and look at this yourself and it should then your answer should be immediately clear.
|
|
|
|
|
I have a "scaffolding printout" in main C++ function working just fine in { #ifdef MY_DEBUG ... #endif } debug code block .
I have included a test header file with a class and I have a test function there with same debug code block .
I have to redefine #define MY_DEBUG and re-include
#include <iostream>
using namespace std;
in the added header file to get the debug test printout.
I have never experienced such (odd) behavior, or is that normal? This is my first real crosscompiled CPP app if that matters.
Not looking forward including all this in every header file.
Perhaps it is a compiler / crosscompiler (gcc) options I have missed?
Thanks for any hints / help
Cheers
Vaclav
|
|
|
|
|
Vaclav_Sal wrote:
#include <iostream>
using namespace std; Shouldn't these two statements be in the project's stdafx.h file? Also in that file is where you should be defining the MY_DEBUG directive.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
Quote: Shouldn't these two statements be in the project's stdafx.h file?
The OP explicitely mentioned GCC.
|
|
|
|
|
You can't re-include a header file. It is prevented by a guard definition or #pragma once with VS.
We can't help without seeing your code regarding the includes and the definitions, and where the error occurs. But the above two statements (including of iostream and using the namespace) must be before cout or a macro that contains it is used.
To be on the safe side I would always use std::cout within macros. Then you don't need to select the namespace.
|
|
|
|
|
Impossible to be sure what you are doing. Please show your code and explain what errors occur.
|
|
|
|
|
OK, here is readers digest for clarification.
The class TESTCLASS1 header is included in HF_VNA_CROSS_CPP.cpp.
This file first code line is
#define MY_DEBUG
and the cout code works in main OK
BUT not in TESTCLASS1.h UNTIL I add / redefine MY_DEBUG.
Without the
#include <iostream>
using namespace std;
added to TESTCLASS1.h
When I add ONLY #define MY_DEBUG
I get the cout error.
Basically I have two issues
1. why do I need to redefine MY_DEBUG in TESTCLASS1.h
2. why do I need to add #include <iostream>
using namespace std; to get cout to work in added class
and a minor request for help with
should I #include "TESTCLASS1.cpp" in main file instead of "header" #include "TESTCLASS1.h" file?
<pre lang="c++">//============================================================================
// Name : HF_VNA_CROSS_CPP.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#define MY_DEBUG
#include <iostream>
using namespace std;
#include <math.h> // rint round up math
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
#include <math.h> // rint round up math
#include "/usr/local/include/wiringPiSPI.h"
#include <math.h> // rint round up math libraray lm
#include "flash_DTR.h" // flash DTR via USB port
/* CCC temp out
#include "AD9851.h" // AD9851 via SPI / I2C
#include "LCD_SPI.h" // LCD via SPI
*/
#include "MY_wiringPi.h" // GPIO for RPi
//#include "LCD.h" // TEST CLASS
//#include "TESTCLASS.h" //
//#include "LCDSPI.h" // NO SPACE
#include "TESTCLASS1.h"
int main() {
cout << "NEW *** COPY 2 TEST output HF_VNA_CROSS_CPP CPP Managed project" << endl; // prints CPP Managed project
//temp bypass CCC
// class test block start
{
#ifdef MY_DEBUG
cout << " process code block START"<< endl;
cout << "*** TRACE file " << __FILE__<< endl;
cout <<" function " <<__FUNCTION__<<endl;
cout << " line " << __LINE__<< endl;
#endif
//LCD__SPI lcd; // double space INSTATIATE CLASS
//lcd.TestFunction();
TEST_CLASS_1 test_class;
test_class.TestFunction();
}
// class test block end
// */
return 43;
#ifdef MY_DEBUG
cout << "*** TRACE file " << __FILE__<<" line " <<__LINE__<< " function "<< __FUNCTION__ << endl;
//return 42;
#endif
flash();
// process code block START
{
#ifdef MY_DEBUG
cout << " process code block START"<< endl;
cout << "*** TRACE file " << __FILE__<< endl;
cout <<" function " <<__FUNCTION__<<endl;
cout << " line " << __LINE__<< endl;
#endif
// process code block END
#ifdef MY_DEBUG
cout << " process code block END"<< endl;
cout << "*** TRACE file " << __FILE__<< endl;
cout <<" function " <<__FUNCTION__<<endl;
cout << " line " << __LINE__<< endl;
#endif
}
/* temp bypass CCC
// class test block start
{
LCD lcd; // INSTATIATE CLASS
//lcd.TestFunction(10);
}
// class test block end
*/
}
/*
* TESTCLASS1.h
*
* Created on: Nov 28, 2017
* Author: os64
*/
#ifndef TESTCLASS1_H_
#define TESTCLASS1_H_
#define MY_DEBUG
namespace std {
class TEST_CLASS_1 {
public:
TEST_CLASS_1();
virtual ~TEST_CLASS_1();
int TestFunction(void);
};
} /* namespace std */
#endif /* TESTCLASS1_H_ */
08:42:36 **** Incremental Build of configuration Debug for project HF_VNA_CROSS_CPP2 ****
make all
Building file: ../src/TESTCLASS1.cpp
Invoking: Cross G++ Compiler
arm-linux-gnueabihf-g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/TESTCLASS1.d" -MT"src/TESTCLASS1.o" -o "src/TESTCLASS1.o" "../src/TESTCLASS1.cpp"
../src/TESTCLASS1.cpp: In member function ‘int std::TEST_CLASS_1::TestFunction()’:
../src/TESTCLASS1.cpp:26:4: error: ‘cout’ was not declared in this scope
cout << " process code block START"<< endl;
^
../src/TESTCLASS1.cpp:26:43: error: ‘endl’ was not declared in this scope
cout << " process code block START"<< endl;
^
make: *** [src/TESTCLASS1.o] Error 1
src/subdir.mk:39: recipe for target 'src/TESTCLASS1.o' failed
08:42:36 Build Finished (took 304ms)
</pre>
|
|
|
|
|
Why do you #define MY_DEBUG in that header file since it serves no purpose? You should write code that depends on #define statements,but only use the #define at the beginning of your source code. Something like (note the correct use of <pre> tags):
#if defined(SOME_TAG)
#endif
#define SOME_TAG // or not as the case may be
#include some .h file
#if defined(SOME_TAG)
#endif
|
|
|
|
|
Richard please no more replies.
If you do not understand the problem - stay out of it and you will
Save yourself some time.
I posted two descriptions and you obviously did not get it and went back to your preaching instead.
It has been nice not to hear from you for a while , so let's keep it that way.PLEASE.
|
|
|
|
|
Funny, I feel exactly the same.
|
|
|
|
|
The error seems to be here:
namespace std {
class TEST_CLASS_1 {
public:
TEST_CLASS_1();
virtual ~TEST_CLASS_1();
int TestFunction(void);
}; You are trying to extend the std namespace. But that is not allowed:
It is undefined behavior to add declarations or definitions to namespace std or to any namespace nested within std, with a few exceptions noted below
|
|
|
|
|
That is the way Eclipse builds the class when asked for "namespace".
But it fails same way when I do
namespace std;
I really think I have to figure out first why I cannot do
#define MY_DEBUG once in the main .cpp
Let me work on that.
|
|
|
|
|
Vaclav_Sal wrote: That is the way Eclipse builds the class when asked for "namespace". It means that you can enter the name of your own namespace but not to enter the name of the reserved std namespace.
I don't know Eclipse good enough. But it should be possible to create a class without a namespace (which is creating it in the global namespace).
Regarding the define: It should be only defined once; usually in a header file.
If you want to define it multiple times, it should be guarded:
#ifndef MY_DEBUG
#define MY_DEBUG
#endif
|
|
|
|
|
I took your code and plugged it into Visual Studio with the following changes:
***** stdafx.h ***** (I turned off pre-compiled headers so that it would more closely mimic GCC)
#define MY_DEBUG
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <math.h>
#include <math.h>
using namespace std;
***** hf_vna_cross_cpp.cpp *****
#include "stdafx.h"
#include "TESTCLASS1.h"
int main() {
cout << "NEW *** COPY 2 TEST output HF_VNA_CROSS_CPP CPP Managed project" << endl;
{
#ifdef MY_DEBUG
cout << " process code block START"<< endl;
cout << "*** TRACE file " << __FILE__<< endl;
cout <<" function " <<__FUNCTION__<<endl;
cout << " line " << __LINE__<< endl;
#endif
TEST_CLASS_1 test_class;
test_class.TestFunction();
}
return 43;
#ifdef MY_DEBUG
cout << "*** TRACE file " << __FILE__<<" line " <<__LINE__<< " function "<< __FUNCTION__ << endl;
#endif
{
#ifdef MY_DEBUG
cout << " process code block START"<< endl;
cout << "*** TRACE file " << __FILE__<< endl;
cout <<" function " <<__FUNCTION__<<endl;
cout << " line " << __LINE__<< endl;
#endif
#ifdef MY_DEBUG
cout << " process code block END"<< endl;
cout << "*** TRACE file " << __FILE__<< endl;
cout <<" function " <<__FUNCTION__<<endl;
cout << " line " << __LINE__<< endl;
#endif
}
}
***** testclass1.h *****
#ifndef TESTCLASS1_H_
#define TESTCLASS1_H_
#include "stdafx.h"
class TEST_CLASS_1
{
public:
TEST_CLASS_1(){}
virtual ~TEST_CLASS_1(){}
int TestFunction(void){return 1;}
};
#endif
The only change I had to make was implementing the three methods in the TEST_CLASS_1 class. It all compiled fine.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
I am sorry you went thru all this , but the issue is not in compiling.
The code just need those "redefinitions" to work, it compiles fine.
I ll go thru your code to see if there is some other change I could use. .
My Ellipse is on strike again - the code is not passing updated executable to the remote system. I have to fix that to see if any modifications make it work.
The "gcc" has an option to -include file and if I put the #define DEBUG in header file and use this compiler -include option it should work.
|
|
|
|
|
Vaclav_Sal wrote: I am sorry you went thru all this , but the issue is not in compiling.
The code just need those "redefinitions" to work, it compiles fine. I only mentioned that it compiled fine to show that your code still compiles even with MY_DEBUG and namespace std being defined in one place only.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
And if you took my advice and wrote the code the way it is supposed to be written it would probably be working by now. But you would rather come back with your usual whiny comments.
|
|
|
|
|
OK, I got it doing what I want putting all the requirements into a common header file and just including it in each class.
Here is my final question - does't compiler do each object file independently and linker puts them together?
That would explain why I need to redefine all. DEBUG and other.
Cheers
|
|
|
|
|
A compiler processes source files and creates an object file for each of the source files. These object files are then passed to the linker together with libraries to create an executable or a library.
The compiling process itself is not a single run but consists of multiple passes with three main tasks for the GCC:
- Running the preprocessor
- Compiling the intermediate file created by the preprocessing to assembly code
- Creating object files from the assembly code files
In your case it is all related to the preprocessor. That will look for the preprocessing directives (those beginning with #), process them and create an intermediate file. This intermediate file is then used for compilation. It will not contain #define s anymore.
If you want to see how it works, you can let GCC do each step separately and create corresponding output files which can then be inspected (use g++ for *.cpp files with *.ii as intermediate file extension):
# Run the preprocessor and stop
# The result is printed to stdout and redirected to a file
gcc -E example.c > example.i
# Compile passing the intermediate file and stop
# Creates assembly code file example.s
gcc -S example.i
# Compile passing the assembly code file and stop (do not link)
# Creates object file example.o
gcc -c example.s You might also use
gcc -save-temps -c example.c to prevent deletion of the *.i and *.s intermediate files so that they can be inspected.
|
|
|
|