I face an issue when I build and link my code with GHS multi compiler.
This is roughly the idea:
base.h -->
#ifndef base_h
#define base_h
class Base
{
void basefncn1(); // defined in src file
void basefncn2(); // defined in src file
void basefncn3(); // defined in src file
}
#endif
interface.h -->
#ifndef interface_h
#define interface_h
#include "base.h"
class Interface : public Base
{
void basefncn1();
}
#endif
derivedclass.h -->
#ifndef derived_h
#define derived_h
#include "base.h"
#include "interface.h"
class Derived : public Interface
{
void basefncn1();
}
#endif
The linker error I get is:
basefncn2() and basefncn3() is multiply defined -> Defined both in base.o and derived.o.
The header files are guarded.
Am I doing anything wrong?
Edit: I tried changing the interface.h file. The function is now defined in interface.cpp.
So basically, interface.h and derived.h does not have functions defined in it.
Related
I 've got a collision issue. I mean that in my A.h in need to include B.h but in B.h I need to include A.h so I can't figure out how to fixe it.
Interface.h
#ifndef _INTERFACE_H
#define _INTERFACE_H
#include <SDL.h>
#include <vector>
#include "Widget.h"
class Interface
{
public:
Interface(SDL_Rect &r);
~Interface();
private:
SDL_Rect m_rect;
std::vector<Widget*> m_widgets;
};
#endif
Widget.h
#ifndef _WIDGET_H
#define _WIDGET_H
#include <SDL.h>
#include "Interface.h"
class Widget
{
public:
Widget(Interface *main, SDL_Rect &r);
~Widget();
private:
SDL_Rect m_rect;
Interface* m_master;
};
#endif
Since you rely on pointers, you can declare (rather than define) the classes, and include the header files in the cpp files:
#ifndef _INTERFACE_H
#define _INTERFACE_H
#include <SDL.h>
#include <vector>
class Widget; //See the swap from include to declaration?
class Interface
{
public:
Interface(SDL_Rect &r);
~Interface();
private:
SDL_Rect m_rect;
std::vector<Widget*> m_widgets;
};
#endif
Do a similar swap in the other header.
That's not a "collosion" but a circular dependency.
For your case it's solved very easily by not including the header files at all, and only use forward declarations of the classes:
File Interface.h:
#ifndef INTERFACE_H
#define INTERFACE_H
#include <SDL.h>
#include <vector>
// No inclusion of Widget.h
// Forward declare the class instead
class Widget;
class Interface
{
public:
Interface(SDL_Rect &r);
~Interface();
private:
SDL_Rect m_rect;
std::vector<Widget*> m_widgets;
};
#endif
File Widget.h:
#ifndef WIDGET_H
#define WIDGET_H
#include <SDL.h>
// Don't include Interface.h
// Forward declare it instead
class Interface;
class Widget
{
public:
Widget(Interface *main, SDL_Rect &r);
~Widget();
private:
SDL_Rect m_rect;
Interface* m_master;
};
#endif
You of course needs to include the header files in your source files.
Also note that I changed the symbols for your include guards. Symbols with a leading underscore followed by an upper-case letter are reserved in all scopes by the "implementation" (the compiler and standard library). See this old question and its answers for details.
EDIT: Doctorlove was faster.
use forward declaration in one of the files:
#ifndef _INTERFACE_H
#define _INTERFACE_H
#include <SDL.h>
#include <vector>
#include "Widget.h"
class Widget;
class Interface
{.....
#endif
My previous question was asked wrong, so I'll post it fixed.
I have this example throwing
expected class-name before ‘{’ token
error while compiling. I am understanding why is it fails, but I don't know how to fix it. Thank you.
BaseClass.h
#ifndef INHERITTEST_BASECLASS_H
#define INHERITTEST_BASECLASS_H
#include "ElementClass.h"
class ElementClass;
class BaseClass
{
private:
ElementClass *m_someField;
};
#endif
ElementClass.h
#ifndef INHERITTEST_ELEMENTCLASS_H
#define INHERITTEST_ELEMENTCLASS_H
#include "ChildClass.h"
class ChildClass;
class ElementClass
{
private:
ChildClass *m_class;
};
#endif
ChildClass.h
#ifndef INHERITTEST_CHILDCLASS_H
#define INHERITTEST_CHILDCLASS_H
#include "BaseClass.h"
class ChildClass : public BaseClass
{
};
#endif
You have circulary dependent .h files.
In BaseClass.h:
#ifndef INHERITTEST_BASECLASS_H
#define INHERITTEST_BASECLASS_H
#include "ElementClass.h" // Includes ElementClass.h
In ElementClass.h:
#ifndef INHERITTEST_ELEMENTCLASS_H
#define INHERITTEST_ELEMENTCLASS_H
#include "ChildClass.h" // Which included BaseClass.h
You can remove those #include lines since you are using those classes by pointers only and a forward declaration is sufficient for that purpose.
When you're working with inheritance the following
#include "ChildClass.h"
class ChildClass;
is unnecessary, if you're going to break these into sepperate source files (which it looks like you are) you can say
#include "ElementClass.h"
in the source file of your derived class
I have 3 header files defining objects:
Point3d.h
#ifndef POINT3D_H
#define POINT3D_H
class Ray3d;
class Vector3d;
#include "Ray3d.h"
#include "Vector3d.h"
class Point3d {
...
};
#endif
Vector3d.h
#ifndef VECTOR3D_H
#define VECTOR3D_H
class Point3d;
class Ray3d;
#include "Ray3d.h"
#include "Point3d.h"
class Vector3d {
...
};
#endif
and Ray3d.h
#ifndef RAY3D_H
#define RAY3D_H
class Point3d;
class Vector3d;
#include "Point3d.h"
#include "Vector3d.h"
class Ray3d {
...
};
#endif
I won't include the .cpp files, but all the functions are defined there.
And then I have this class:
Transform.h
#ifndef TRANSFORM_H
#define TRANSFORM_H
#include <Eigen/Dense>
#include "../../geometry/Ray3d.cpp"
#include "../../geometry/Point3d.cpp"
#include "../../geometry/Vector3d.cpp"
using Eigen::MatrixXd;
class Transform {
...
};
#endif
AND FINALLY I have this subclass:
Translation.h
#ifndef TRANSLATION_H
#define TRANSLATION_H
//#include <Eigen/Dense>
#include "Transform.h"
//#include "../../geometry/Point3d.cpp"
//#include "../../geometry/Vector3d.cpp"
//#include "../../geometry/Ray3d.cpp"
using Eigen::MatrixXd;
class Translation : public Transform {
...
};
#endif
The problem is when I try to compile Translation.cpp:
g++ Transform.cpp Translation.cpp
I get a multiple definition of function error for every method in Ray3d, Point3d, and Vector3d. What can I do do avoid this? Should I be including less? Is my g++ command wrong? Thanks!
I'm also aware that I'm doing both forward declaration and includes in the first 3 headers, but that was the only way I could get those to compile. Part of the problem maybe?
You should not include the cpp files in transform.h
"but that was the only way I could get those to compile. Part of the problem maybe?"
You compile and link .cpp files separately, instead of including them (i.e. being seen from the preprocessor).
Your compiler command line should look rather like
g++ ../../geometry/Ray3d.cpp
../../geometry/Point3d.cpp
../../geometry/Vector3d.cpp
Transform.cpp Translation.cpp
-o MyExecutable
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 can't export a class:
#ifndef SDBIDI
#define SDBIDI
#ifndef SDBIDI_FLAG
#define SDBIDI_ORIENT __declspec(dllimport)
#else
#define SDBIDI_ORIENT __declspec(dllexport)
#endif
#include "TCInfoSuVars.h" //class is here!
SDBIDI_ORIENT int myFoo(FILE *file); // exporting function
#endif
class definition in TCInfoSuVars.h
#pragma once
#include <string>
#include <hash_map>
class SDBIDI_ORIENT TCInfoSuVars
{
public:
std::string id;
std::string tcVal;
TCInfoSuVars();
TCInfoSuVars(std::string _tcVal, std::string _id);
~TCInfoSuVars();
};
Getting a error:
myProgram.cpp
#define SDBIDI_FLAG
output:
TCInfoSuVars.h(14) : error C2470: 'TCInfoSuVars' : looks like a function definition, but there is no parameter list; skipping apparent body
And if I write
class __declspec(dllexport) TCInfoSuVars
everything works OK.
Thank you!
Somewhere you're including TCInfoSuVars.h before SDBIDI_ORIENT is defined - Make sure you include the header file that defines SDBIDI_ORIENT first.