Include header files in c++ - c++

The C++ Premier I have doesn't say much about what I am about to ask, and this is what I got googling LINK:
When the compiler compiles the #include "example.h" line, it copies the contents of example.h into the current file.
So, if that's true, in the following example why B.h doesn't know about A.h ? Is it how the files are compiled ? Do I have to include A.h in every file that use it and then include every file that use A.h in the program.h that uses these files ?
In program.h
#include "A.h"
#include "B.h"

WARNING: VERY BAD CODE:
a.h
#ifndef A_H
#define A_H
#define SOME_LIT "string lit in A.h"
#endif
b.h
#ifndef B_H
#define B_H
#include <iostream>
void foo() { std::cout << SOME_LIT << '\n'; }
#endif
main.cpp
#include "a.h"
#include "b.h"
int main()
{
foo();
}
Prints:
$ ./a.out
string lit in A.h
So you can see b.h knows about the define in a.h. If you forgot the #include "a.h", or put it below #include "b.h", this would break.
As a general rule however, you should explicitly #include a header in any file you need it. That way you know that you only care about foo in main, so you just #include the foo header, which is b.h:
#include "b.h"
int main()
{
foo();
}

Related

c++ - Undefined reference to <function name> when using multiple header files

I am new to C++ programming and I have been stucked in this for hours.
I have three header files: A.h, B.h and common.h and two .cpp files: A.cpp, B.cpp.
A.h
#ifndef A_H
#define A_H
#include"B.h"
class AA{
public:
void fun();
};
#endif
B.h
#ifndef B_H
#define B_H
#include <iostream>
#include "common.h"
using namespace std;
class BB{
public:
void fun2();
};
#endif
common.h
extern int c;
A.cpp
#include "A.h"
void AA::fun(){
cout<<"A"<<endl;
}
B.cpp
#include "B.h"
void BB::fun2(){
cout<<"FUN 2"<<endl;
}
main.cpp
#include "A.h"
int main(){
int c = 5;
AA a;
a.fun();
return 0;
}
Error: undefined reference to `AA::fun()'
Why am I getting this error? Can anyone explain it?
Thanks a lot.

Namespaces with multiple cyclic inclusion

I've 3 classes that belong to a namespace "MyNS". "A" uses the "B" and "C", while both "B" and "C" use "A". Here are the structure of my headers:
//mynamespace.h
#ifndef SOMENAMESPACE_H
#define SOMENAMESPACE_H
namespace MyNS {
class A;
class B;
class C;
}
#endif
/*******************************************************/
//A.h
#ifndef A_H
#define A_H
#include "mynamespace.h"
#include "B.h"
#include "C.h"
class MyNS::A {
..
MyNS::B someMethod(args);
MyNS::C someMethod2(args);
..
};
#endif
/*******************************************************/
//B.h
#ifndef B_H
#define B_H
#include "mynamespace.h"
#include "A.h"
class MyNS::B {
..
MyNS::A *someMember;
..
};
#endif
/*******************************************************/
//C.h
#ifndef C_H
#define C_H
#include "mynamespace.h"
#include "A.h"
class MyNS::C {
..
MyNS::A *someMember;
..
};
#endif
Each of the classes has a C++ source file that includes the line:
using namespace MyNS;
The compiler is stopped by the B.h header guards, that's why I get an "incomplete type 'class B'" error in A::someMethod(args).
I know I could have included all the class definitions inside "mynamespace.h", but the classes are long which made my header file bloated, that's why I had to separate them like this.
How can I make this code work with separate header files ?
The correct way to do this is using forward declaration. Do not include A.h in C.h and B.h. Only actually perform #include "A.h" in B.cpp and C.cpp. The forward declaration in the namespace should be enough.
//mynamespace.h
#ifndef SOMENAMESPACE_H
#define SOMENAMESPACE_H
namespace MyNS {
class A;
class B;
class C;
}
#endif
/*******************************************************/
//A.h
#ifndef A_H
#define A_H
#include "mynamespace.h"
#include "B.h"
#include "C.h"
class MyNS::A {
..
MyNS::B someMethod(args);
MyNS::C someMethod2(args);
..
};
#endif
/*******************************************************/
//B.h
#ifndef B_H
#define B_H
#include "mynamespace.h"
class MyNS::B {
..
MyNS::A *someMember;
..
};
#endif
/*******************************************************/
//C.h
#ifndef C_H
#define C_H
#include "mynamespace.h"
class MyNS::C {
..
MyNS::A *someMember;
..
};
#endif

Passing MyClass defined in header as function argument to other file

I've spent around an hour and couldn't find anything helpful on the Web. The problem is that I've got some files like a.h, b.h, a.cpp, b.cpp and main.cpp. In a.h I've got declared a container with attributes defined by myself. I would like to pass this container as and argument to the function in b.h/b.cpp. What is the way of doing this?
a.h file
struct Container{
int size;
int* array
...};
b.cpp
void someFunction(Container container)
{...}
Thanks for any help.
Use #include "file" to include the files you need.
Further reading
For example in b.cpp:
#include "a.h"
void someFunction(Container container);
You should also put include guards into your header files.
They prevent unwanted multiple inclusion of the same file. If your file is called a.h, you would write the following:
a.h :
#ifndef A_H
#define A_H
// ... your code ...
#endif // A_H
In b.h you should put #include "a.h" so that the container description is available. After that you can simply declare your functions like you have them in the question. So your files would look like this:
a.h
#ifndef A_H
#define A_H
struct Container{
int size;
int* array
...};
#endif // A_H
b.h
#ifndef B_H
#define B_H
#include "a.h"
void someFunction(Container container);
#endif // B_H
b.cpp
void someFunction(Container container)
{ ... }

header file include-loop and multiple definition

I have a util.h containing a function which will be used in a.h and 'b.h', and further more, a.h and b.h will include each other in order to access some classes defined in each other.
//util.h
#ifndef _UTIL_H_
#define _UTIL_H_
#include <iostream>
void foo()
{
std::cout << "foo\n";
}
#endif
//a.h, it has a a.cpp
#ifndef _A_H_
#define _A_H_
#include "util.h"
#include "b.h"
//some classes' definition
#endif
//b.h, it has a b.cpp
#ifndef _B_H_
#define _B_H_
#include "util.h"
#include "a.h"
//some classes' definition
#endif
My problem is that, I got multiple definition error for foo. How?
I thought the problem might be that, a.h includes util.h and b.h, and b.h includes util.h once again, so I got multiple def error. But it can't seem to make sense, because in util.h I wrote #ifndef/#define guards.
Anyone can give me a help, thanks.
Your problem is caused by defining rather than simply declaring foo inside utils.h
For this example, assume that we have:
a.cpp
#include "a.h"
b.cpp
#include "b.h"
main.cpp
#include "a.h"
#include "b.h"
#include "utils.h"
int main(){
foo();
return 0;
}
After preprocessing, but before compilation, your files now look like this (this is a simplification, but you get the idea):
a.cpp
void foo()
{
std::cout << "foo\n";
}
b.cpp
void foo()
{
std::cout << "foo\n";
}
main.cpp
void foo()
{
std::cout << "foo\n";
}
int main(){
foo();
return 0;
}
Now when you compile, you have 3 definitions of foo (they're all identical, but this is irrelevant). After compilation, there is no way for the linker to know which definition to pick for any given call to foo and so it generates an error.
Instead of defining foo in the header, define it in utils.cpp and only put a declaration in utils.h, e.g.
utils.h
#ifndef _UTIL_H_
#define _UTIL_H_
void foo();
#endif
or, alternately, declare foo as static or inline:
utils.h
#ifndef _UTIL_H_
#define _UTIL_H_
static void foo()
{
std::cout << "foo\n";
}
/* OR */
inline void foo()
{
std::cout << "foo\n";
}
#endif
This is something you need to do only if you want to inline your function. In this case, the compiler needs a definition of the function in every translation unit where it is used, as it essentially becomes a compile-time macro.

I keep getting an error saying my class does not name a type

I have one class, called A, and it has it's own header file. Then I have another class, called B, which also has it's own header file. They each have their own .cpp file where I implement all of their functions.
I'm trying to have class B have a variable of class type A as a private variable, but I keep getting the error 'A' does not name a type
My code looks like this:
main.h:
#ifndef MAIN_H
#define MAIN_H
#include "A.h"
#include "B.h"
#endif
main.cpp:
#include "main.h"
int main( int argc, char* args[]) {
B test;
}
A.h:
#ifndef A_H
#define A_H
#include "main.h"
class A {
public:
//public functions
private:
//private variables
};
#endif
B.h:
#ifndef B_H
#define B_H
#include "main.h"
class B {
public:
//public functions...
private:
A temp;
}
#endif
So all of my includes are in main.h, which includes A before B. B has a variable of type A, but it is included from being in main.h and B.h includes main.h. However, I keep getting an error saying:
error: 'A' does not name a type.
I've done some googling, and it seems like that means that A isn't defined when you use it, but it should be defined there since it's being included in main.h, right?
The problem is that A.h includes main.h, which includes B.h, which tries to use A.
The good way to organize your files would be this:
main.h:
// not needed
main.cpp:
#include "B.h" // for using class B
int main( int argc, char* args[]) {
B test;
}
A.h:
#ifndef A_H
#define A_H
// no includes needed ATM
class A {
//...
};
#endif
B.h:
#ifndef B_H
#define B_H
#include "A.h" // for using class A
class B {
//public functions...
}
#endif
That way, B.h is self-contained and can be used without having to include anything else before it. That's very important as soon as your project grows above the toy level it is at now. Why would anyone trying to use what header x.h provides need to know to also include f.h, m.h, and u.h?
The code you provide compiles properly if you add a ; at the end of B.h
A better way of doing it would be #include "A.h" in "B.h", instead of #include "main.h"
But it is probably unrelated to your problem.
That kind of error may also be confusing if you are using templates and forget "typename".
A.h includes Main.h at the top.
Main.h skips A.h because A_H is already defined, then includes B.h.
B.h tries to make use of A, but A.h hasn't finished compiling yet so the type isn't defined.