Passing MyClass defined in header as function argument to other file - c++

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)
{ ... }

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

Include header files in 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();
}

User-Created Header Causing C2061: Syntax Error : Identifier 'classname'

So, I sort of expect this to end up being a simple answer, but I've been hacking at it for a while now, and can't seem to fix this issue. So I have a particular class, Intersection, that when included in any other header gives me:
error C2061: syntax error : identifier 'Intersection'
This is my Intersection header:
#ifndef INTERSECTION_H
#define INTERSECTION_H
#include "Coord.h"
#include "Road.h"
#include "TrafficLight.h"
class Intersection {
private:
int id;
Coord * midPoint;
Road * northRoad;
Road * eastRoad;
Road * westRoad;
Road * southRoad;
TrafficLight * trafficLight;
public:
Intersection(int, Coord *, Road *, Road *, Road *, Road *);
~Intersection();
void transitionTrafficLight();
int getId();
Road * getNorthRoad();
Road * getEastRoad();
Road * getWestRoad();
Road * getSouthRoad();
TrafficLight * getTrafficLight();
};
#endif
Now, if I attempt to use this class elsewhere, I get the error. For example:
#ifndef ROAD_H
#define ROAD_H
#include "Coord.h"
#include "Intersection.h"
#include <string>
class Road {
public:
enum LaneCount { TWO_LANE = 2, FOUR_LANE = 4 };
Road(std::string, Coord *, Coord *, LaneCount, Intersection *, Intersection *, int);
//shortened
Particularly at the Road constructor (and any other classes which reference Intersection). I don't think it's a syntax problem, as Coord is another class, defined in the same manner, and the compiler (VS 2008) doesn't complain about it. It's just Intersection in particular that's giving me this trouble. :/
I'm tagging it homework -- it's what it's for, even though this is just an error I can't get rid of rather than part of the problem.
Thoughts?
It looks like the error is that you have two header files that are circularly including one another - intersection.h and road.h. Doing this tends to lead to weird surprises in C++ because of how include guards work. For example, suppose that I have two header files that look like this:
// File: A.h
#ifndef A_Included
#define A_Included
#include "B.h"
class A {};
void MyFunction(B argument);
#endif
and
// File: B.h
#ifndef B_Included
#define B_Included
#include "A.h"
class B {};
void MyOtherFunction(A argument);
#endif
Now, if I try to #include "A.h", then it expands out to
// File: A.h
#ifndef A_Included
#define A_Included
#include "B.h"
class A {};
void MyFunction(B argument);
#endif
When I try expanding out the #include "B.h", I get this:
// File: A.h
#ifndef A_Included
#define A_Included
// File: B.h
#ifndef B_Included
#define B_Included
#include "A.h"
class B {};
void MyOtherFunction(A argument);
#endif
class A {};
void MyFunction(B argument);
#endif
At this point, the preprocessor will again try expanding out A.h, which leads to this:
// File: A.h
#ifndef A_Included
#define A_Included
// File: B.h
#ifndef B_Included
#define B_Included
// File: A.h
#ifndef A_Included
#define A_Included
#include "B.h"
class A {};
void MyFunction(B argument);
#endif
class B {};
void MyOtherFunction(A argument);
#endif
class A {};
void MyFunction(B argument);
#endif
Now, let's see what happens when we resolve all of these weird include guards. The first time we see A, it's expanded out, as is the case when we expand out B for the first time. However, when we see A for the second time, it's not expanded out at all. Thus, after taking out comments and preprocessor directives, we get this resulting code:
class B {};
void MyOtherFunction(A argument);
class A {};
void MyFunction(B argument);
Notice that when MyOtherFunction is declared, A has not yet been declared, and so the compiler reports an error.
To fix this, you can forward-declare A and B in the header files that need them:
// File: A.h
#ifndef A_Included
#define A_Included
class A {};
class B; // Forward declaration
void MyFunction(B argument);
#endif
and
// File: B.h
#ifndef B_Included
#define B_Included
class B {};
class A; // Forward declaration
void MyFunction(B argument);
#endif
Now, there are no more circular dependencies. As long as you #include the appropriate header files in the .cpp files, you should be fine.
Hope this helps!

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.