Public Group

# #include

This topic is 4848 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Well, this shouldn't be a problem for me but anyways. #include <iostream> #include "SomeClass.cpp" int main() { SomeClass Room; Room.printMessage(); return 0; } EOF "main.cpp" BOF "SomeClass.cpp" #include "SomeClass.h" SomeClass::printMessage() { std::cout << "SomeText" << std::endl; } EOF "SomeClass.cpp" BOF "SomeClass.h" class SomeClass { public: void printMessage(); private: }; EOF"SomeClass.h" For my question: When I set my files up like this I cannot use the std namespace. As long as I don't use the Standard Namespace in my Class my classes work fine. However, what does not make sense to me is the when I split my class declations and definitions into .h and .cpp files is the only time iostream doesn't want to work, but if I put both my declations and my definitions into one file the I don't get compiles errors about std::cout etc not existing. I've always had an understanding that the method I type should work it has in the past for me.(My memory is fuzzy haven't touched C++ in years). This isn't a huge problem for me(at worst a nuance but I would like to know anyways), but I am baffled and I haven't been able to find an answer. Am I using #include improperly?

##### Share on other sites

Ie:
#ifndef HEADER_H#define HEADER_H// Code goes here.// You may include other headers as needed, but don't// put using statements if at all possible// The ifndef, define, and endif are 'inclusion guards'// to prevent including a file more than one time per .cpp file// Usually you want to put the 'interface' in a header file#endif

#include "header.h"// Code goes here.// You may include other headers as needed and usually can put// using statements.  This is where you'd put implementation// of the classes and functions you defined in the header file

main.cpp
#include "header.h"// Code goes here.// You may include other headers as needed.  Use as normal.

I believe that covers the basics I'm sure someone else will correct me if I made a mistake and/or will elaborate on a few things. Hope this helps.

##### Share on other sites
You need to #include <iostream> in your SomeClass.cpp file. Your member function can't use cout unless you do. As far as <iostream> in main.cpp, it's usually a good idea to include it, but with your current program is not necessary.

Hope this helps.

:stylin:

##### Share on other sites
Perhaps you misunderstand how the C++ compilation model works.

Each source file in a project is compiled separately from another file.

When you hit compile on your project, the preprocessor will be the first stage of the compilation process. The preprocessor will execute all of the preprocessor directives, such as #include, and turn your file into a 'translation unit'.

In the example of SomeClass.cpp, the preprocessor will execute the #include"SomeClass.h" directive, which results in the file SomeClass.h being pasted in the place of the #include"SomeClass.h" directive. This will result in a translation unit of

class SomeClass{public:void printMessage();private:};void SomeClass::printMessage(){std::cout << "SomeText" << std::endl;}

The compiler then goes to work on this translation unit. When the compiler gets around to compiling the printMessage() function, it encounters std::cout and std::endl, which, as you can see in the above code, have not been declared or defined in the translation unit. This means the compiler has no way of knowing what they are, as it only knows about things defined/declared in the translation unit which it is currently working on. It knows nothing of your main.cpp source file.

The solution is to #include<iostream> in SomeClass.cpp.

#include <iostream>#include "SomeClass.h"void SomeClass::printMessage(){std::cout << "SomeText" << std::endl;}

When the preprocessor gets to work on this, it will execute the #include<iostream> directive as well as the #include "SomeClass.h" directive this time, producing a translation unit that looks like

namespace std {extern istream cin;extern ostream cout;extern ostream cerr;extern ostream clog;extern wistream wcin;extern wostream wcout;extern wostream wcerr;extern wostream wclog;}class SomeClass{public:void printMessage();private:};void SomeClass::printMessage(){std::cout << "SomeText" << std::endl;}

When the compiler compiles the translation unit this time, it will know what std::cout is when it reaches the printMessage function, as it is declared at the very top of the source file. This will remove the compilation error.

As a side note, you have have noticed that std::endl is not declared in this translation unit. std::endl is actually declared in the <ostream> header file, so you need to include this header file too in order to be strictly correct. Most implementations #include<ostream> at the top of #include<iostream>, however, so the code will usually compile without explicitly including the <ostream> header, although this is a non-standard extension to the C++ language, and should not be relied upon.

##### Share on other sites
Thanks for the detailed explanations. The info in how compliers work was very helpful. That cleared that up for me, thanks again!

1. 1
Rutin
27
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633311
• Total Posts
3011313
• ### Who's Online (See full list)

There are no registered users currently online

×