0

Please have a look at the following code

Main.cpp

#include <iostream>

using namespace std;

int main()
{
    Class1 c;
}

Class1.cpp

#include <iostream>

using namespace std;

class Class1
{
public:
    void click1()
    {
        cout << "Click 1" << endl;
    }
};

Class2.cpp

#include <iostream>

using namespace std;

class Class2
{
public:
    void click2()
    {
        cout << "Click 2" << endl;
    }
};

If I add header files to above classes, they work. Why C++ do not understand classes in different files without a header file?

Drew Dormann
  • 50,103
  • 11
  • 109
  • 162
PeakGen
  • 18,056
  • 71
  • 208
  • 385
  • That's simply the way how C++ works: Class declarations in header, class definitions in compilation units. – πάντα ῥεῖ Mar 01 '13 at 14:25
  • Why are you including `` everywhere, and putting `using namespace std`? You should only include the headers you actually need (i.e. that contains the declaration of something you use in the file), and [rely on using directives sparingly](http://stackoverflow.com/q/1452721/20984). – Luc Touraille Mar 01 '13 at 16:00

4 Answers4

6

In C++ a source file is called a translation unit. Each translation unit is separate from each other, and basically don't know about each others existence. You have to explicitly tell the compiler what things a translation unit should know about.

This is done by declaring things. And instead of having the same declaration in many files and places, you put them in a single header file that all source files includes.

Some programmer dude
  • 363,249
  • 31
  • 351
  • 550
1

Why C++ do not understand classes in different files without a header file?

Why should it? There is no convention requiring file names to be named a certain way, or contain a single public class per file. You are free to put entire class hierarchies in a single header file, as long as it makes sense logically. For example, the <algorithms> header does not contain anything called "algorithms", but the name is very intuitive.

Finally, there is no requirement in C++ for things to be inside classes or class templates. You can have files full of declarations of namespace-level C-style functions. There is no reasonable naming convention to put in place in order to support files of this nature.

The #include directive solves this problem, and gives you the flexibility to name your files the way you want.

Sergey Kalinichenko
  • 675,664
  • 71
  • 998
  • 1,399
  • @PeteBecker Render unto Eclipse the things which are Java's :) :) :) – Sergey Kalinichenko Mar 01 '13 at 14:57
  • Thanks for the help. I really appreciate it. +1 from me :) – PeakGen Mar 01 '13 at 15:02
  • @PeteBecker: How do you know I am from Java? – PeakGen Mar 01 '13 at 15:02
  • @Yohan - many programmers coming to C++ from another language look for idioms like the ones they are used to from that other language. Having the compiler rummage around for something that might be a definition of something you used in your program is a Java thing (yes, that's an unfair description). – Pete Becker Mar 01 '13 at 15:27
1

Why C++ do not understand classes in different files without a header file?

That's how C++'s (bad) compilation model works.

The compiler processes each translation unit (.cpp file) separately; then, all the obejct files produced by the processing of individual compilation units are merged by the linker to create your program's executable.

While processing one translation units, the compiler won't see entities that have been parsed while processing other translation units.

If I add header files to above classes, they work

That's because #includeing the corresponding headers makes the declaration (or definition) of those entities visible to each translation unit that needs to use them.

Andy Prowl
  • 114,596
  • 21
  • 355
  • 432
1

The purpose of header files is mostly to declare items. In C++ there must be always a declaration for any class, variable, etc. If you don't provide those, the compiler cannot find the corresponding classes, variables, etc.

See SO.

Community
  • 1
  • 1
bash.d
  • 12,357
  • 2
  • 24
  • 37