I get the error "C2143: syntax error: missing ';' before '*' in Track.h
I believe this is due to a "missing" class definition.
These are the 3 header files:
Topics.h, the package-level header file, which #includes everything else:
#ifndef Topics_H
#define Topics_H
#include <oxf\oxf.h>
#include "Request.h"
#include "TDPoint.h"
#include "Track.h"
#include "TrackReport.h"
#endif
Then there's TDPoint (as in "3DPoint"), which simply defines a class with 3 long attributes:
#ifndef TDPoint_H
#define TDPoint_H
#include <oxf\oxf.h> // Just IBM Rational Rhapsody's Framework
#include "Topics.h"
class TDPoint {
//// Constructors and destructors ////
public :
TDPoint();
~TDPoint();
//// Additional operations ////
long getX() const;
void setX(long p_x);
long getY() const;
void setY(long p_y);
long getZ() const;
void setZ(long p_z);
//// Attributes ////
protected :
long x;
long y;
long z;};
#endif
But the problem lies here, in the marked line:
#ifndef Track_H
#define Track_H
#include <oxf\oxf.h> // Just IBM Rational Rhapsody's Framework
#include "Topics.h"
#include "TDPoint.h"
class Track {
public :
//// Operations ////
std::string getId() const;
void setId(std::string p_id);
TDPoint* getPosition() const; // <--- This line, the first line to use TDPoint, throws the error
//// Attributes ////
protected :
std::string id;
TDPoint position;
public :
Track();
~Track();
};
#endif
My guess was that the compiler (MS VS2008/ MSVC9) simply didn't know the class "TDPoint." But even defining the class in the same header file as "Track", or using a forward declaration like "class TDPoint" (which then throws the error: undefined class) didn't help.
The code was auto-generated from Rhapsody, if that makes any difference.
But maybe the error is something else entirely?
Topics.h includes TDPoint.h and Track.h
TDPoint.h includes Topics.h
and Track.h includes both Topics.h and TDPoint.h
This feels like a circular include... You should either forward declare your classes to solve it or modify Topics.h to not to have circularity.
You have circular inclusion: The file Track.h includes Topics.h which includes TDPoints.h which includes Topics.h which includes Track.h where the TDPoint class is not declared.
In fact, the TDPoint.h doesn't need any header files at all, it's completely independant (as per the code shown in your question).
The Track.h file only needs to include TDPoint.h, not Topics.h. (And possibly <string>.)
General hint: Include as few headers as possible in a header file.
The other answers are correct, but I would like to add few things for completeness.
1. Cause: your project have circular including, specifically, when you compile "TDPoint.cpp", the compiler will do the following
#include "TDPoint.h" //start compiling TDPoint.h
#include "Topics.h" //start compiling Topics.h
#include "TDPoint.h" //compilation of TDPoint.h skipped because it's guarded
#include "Track.h" //start compiling Track.h
#include "Topics.h" //compilation of Topics.h skipped because it's guarded
//resume compiling Track.h
...
TDPoint* getPosition() const; //=> error TDPoint is not defined
=>C2143: syntax error: missing ';' before '*'
2. Counter measure: replace including in header by forward declaration to remove circle of including, and use including in .cpp files. Specifically, forward declaration means:
(in Topics.h)
#ifndef Topics_H
#define Topics_H
#include <oxf\oxf.h>
#include "Request.h"
class TDPoint; //Forward declaration to replace #include "TDPoint.h"
class Track; //Forward declaration to replace #include "Track.h"
#include "TrackReport.h"
#endif
Related
One of the header files is as follows -
#include "stdafx.h"
class AAA
{
public:
std::string strX;
std::string strY;
};
When I try to compile the project, I get the error
error C2011: 'AAA' : 'class' type redefinition
Nowhere else in my program have I redefined the class AAA. How do I fix this?
Change to code to something like this:
#ifndef AAA_HEADER
#define AAA_HEADER
#include "stdafx.h"
class AAA
{
public:
std::string strX;
std::string strY;
};
#endif
If you include this header file more than once in some source file, include guards will force compiler to generate class only once so it will not give class redefinition error.
Adding
#pragma once
to the top of your AAA.h file should take care of the problem.
like this
#include "stdafx.h"
#pragma once
class AAA
{
public:
std::string strX;
std::string strY;
};
In addition to the suggested include guards you need to move #include "stdafx.h" out of the header. Put it at the top of the cpp file.
I met this problem today in VS 2017. I added #pragma once, but it didn't work until I added a macro definition:
// does not work
#pragma once
// works with or without #pragma once
#ifndef _HEADER_AAA
#define _HEADER_AAA
//
// my code here....
//
#endif
I have no clue how to explain this, but it is a solution for me.
There are two ways to go about this but you can't use both. Make sure to wrap the class definition with a compiler directive that the class declaration only gets compiled once:
#include "stdafx.h"
#pragma once
class AAA{
public:
std::string strX;
std::string strY;
};
-or-
#include "stdafx.h"
#ifndef AAA_HEADER_
#define AAA_HEADER_
class AAA
{
public:
std::string strX;
std::string strY;
};
#endif
Also: note the class import statement should be at the top of your file.
I am having these two errors.
error C2143: syntax error : missing ';' before '*'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
i already checked the solution from internet, every body recommend to #include <string> and using std::string instead of string, this is my header file. I applied the solutions but problem is still there. This is my code
friend std::ostream& operator<<(std::ostream& os, const Student& s);
friend class StudentList;
public:
Student(int id = 0,std::string name = "none");
virtual ~Student();
private:
std::string name;
int id;
Student* next;
RCourseList* rCList;
and this is my program's upper portion
#ifndef STUDENT_H
#define STUDENT_H
#include <iostream>
#include <string>
#include "RCourseList.h"
This is RCourseList.h
#ifndef COURSELIST_H
#define RCOURSELIST_H
#include "RCourse.h"
class RCourseList
{
public:
RCourseList();
private:
RCourse* rhead;
};
#endif // RCOURSELIST_H'
Your header file RCourseList.h, has an error in its include guard
#ifndef COURSELIST_H
#define RCOURSELIST_H
it should be
#ifndef RCOURSELIST_H
#define RCOURSELIST_H
The reason this is a problem is because you have another header file called CourseList.h, that header also starts with an include guard.
#ifndef COURSELIST_H
#define COURSELIST_H
So CourseList.h defines the macro COURSELIST_H and this prevents the CourseList.h file from being included twice (in a single compilation) because #ifndef COURSELIST_His true on the first include but will be false on a second include.
But because your RCourseList.h wrongly starts with #ifndef COURSELIST_H including CourseList.h will also prevent a later RCourseList.h from being included.
In short, name your include guards after your header file names. Be very careful about this otherwise you get this kind of error.
Or you could replace the traditional include guard with the non-standard but widely supported #pragma once, like this
#pragma once
#include "RCourse.h"
class RCourseList
{
public:
RCourseList();
private:
RCourse* rhead;
};
#pragma once does exactly the same as the traditional include guard but without the possibility of this kind of error.
I have a class GameObject which has a vector of Component and Transform.
The Transform is a Component but can be accessed on it's own.
I'm getting a Base class undefined error on Component when I try to include both Component.h and Transform.h in GameObject.
Error Message:
Error 1 error C2504: 'Component' : base class undefined c:\users\pyro\documents\visual studio 2010\projects\engine\main\transform.h 9
GameObject.h
#ifndef _GameObject
#define _GameObject
#include "Core.h"
#include "Component.h"
#include "Transform.h"
class Transform;
class Component;
class GameObject
{
protected:
Transform* transform;
vector<Component*> components;
};
#endif
Component.h
#ifndef _Component
#define _Component
#include "Core.h"
#include "GameObject.h"
class GameObject;
class Component
{
protected:
GameObject* container;
};
#endif
Transform.h
#ifndef _Transform
#define _Transform
#include "Core.h"
#include "Component.h"
//Base class undefined happens here
class Transform : public Component
{
};
#endif
I've found a bunch of other topics, but they don't really address the problem I'm having.
So the question is this: why am I getting this error and how do I fix it?
There are a couple of problems with your code:
1. Circular dependency
GameObject.h includes Component.h, and Component.h includes GameObject.h.
This circular dependency breaks everything. Depending on which file you're "starting from", either GameObject will not be visible from Component or vice versa, due to the inclusion guards.
Remove the circular dependency: you don't really need those #includes at all, as you're already using forward declarations. In general, minimise the use of #include in headers.
2. Syntax error
When you've fixed that, add in the missing }; in Component.h.
(Your definition for Transform thinks it's a nested class inside Component which, at that point, has not been fully defined.)
3. Reserved names
This may not cause you a practical problem today, but your macro names should not begin with _ as these are reserved for implementation (compiler) use.
Suppose some source file has a #include "Component.h" directive and no other #include directives. Here's what happens, in order:
The preprocessor symbol _Component is defined.
The #include "GameObject.h" directive in Component.h is expanded.
The #include "Component.h" directive in GameObject.h is expanded.
This does nothing because _Component is now defined.
The #include "Transform.h" directive in GameObject.h is expanded.
The definition of class Transform in Transform.h won't compile because the base class Component has not been defined yet.
The problem is that you have too many superfluous #include statements. For example, there's no need for GameObject.h to include Component.h. The forward declaration is all that is needed. In general, don't include a file in a header unless you truly do need it. If you do need to do so, you need to be very careful of circular inclusions.
Given the following two header files:
#ifndef EVENT_HANDLER_H
#define EVENT_HANDLER_H
#include <SFML/Window.hpp>
#include <SFML/Window/Event.hpp>
#include "window_handler.h"
class EventHandler
{
public:
EventHandler(WindowHandler & classOwner);
WindowHandler * m_windowHandler;
private:
bool m_leftKeyDown;
bool m_rightKeyDown;
bool m_upKeyDown;
bool m_downKeyDown;
unsigned int m_mouseX;
unsigned int m_mouseY;
};
#endif
AND
#ifndef WINDOW_HANDLER_H
#define WINDOW_HANDLER_H
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include "event_handler.h"
class WindowHandler
{
public:
WindowHandler();
sf::Window m_app;
private:
EventHandler m_eventHandler;
};
#endif
I get the following output:
In file included from window_handler.h:6:0,
from main.cpp:3:
event_handler.h:13:29: error: expected ‘)’ before ‘&’ token
event_handler.h:15:2: error: ‘WindowHandler’ does not name a type
As far as I know, though, I'm doing everything perfectly fine. Am I missing something here?
You have a circular dependency.
When window_handler.h includes event_handler.h you've defined WINDOW_HANDLER_H but haven't actually reached the point where the class is defined. When event_handler.h tries to include window_handler.h it doesn't because of WINDOW_HANDLER_H
As noted, you need to forward declare in event_handler.h by removing the include for window_handler.h and replacing it with:
class WindowHandler;
In event_handler.h, remove the line
#include "window_handler.h"
and replace it with
class WindowHandler;
The issue here is that you have a cycle in your include lists. So because of the include guards, you will either have a file that tries to use an undefined WindowHandler, or an undefined EventHandler. Take a look at the preprocessor output and this should make more sense.
Your headers have a circular dependency of includes. Depending on your needs you might be able to change one to a forward declaration, or you'll have to create a third header with the required common code in it.
Animal
|
Mammal
/ \
TwoLegged - FourLegged
/ \
Human Lion
I have this class hierarchy, each class defined in it's own header. Now when I include both
Human.h and Lion.h in the same place, I get a Mammal redefinition error.
error C2011: 'Mammal' : 'class' type redefinition
This because Mammal.h is included in both TwoLegged and OneLegged classes.
I'm not sure however, how I could resolve this cyclic dependency in headers, as I cannot change the class hierarchy.
Anybody care to assist?
EDIT:
Mammal header
#ifndef MAMMAL_H
#define MAMNAL_H
#include "stdafx.h"
#include "Animal.h"
class Mammal : public Animal
{
public:
Mammal::Mammal();
virtual Mammal::~Mammal();
std::string mammal_name();
int mammal_age();
int mammal_expectedlifedays();
bool mammal_hunter();
int mammal_power();
int mammal_birthrate();
bool mammal_alive();
protected:
Mammal::Mammal(const std::string& mname, int mexpectedlifedays, int mage, bool mhunter, int mpower, int mbirthrate, bool malive) : Animal(mname, mexpectedlifedays, mage,mhunter, mpower, mbirthrate, malive)
{}
private:
};
#endif
The errors given by the compiler:
error C2011: 'Mammal' : 'class' type redefinition
see declaration of 'Mammal'
error C2504: 'Mammal' : base class undefined
error C2614: 'TwoLegged' : illegal member initialization: 'Mammal' is not a base or member
Note: It's not homework, else I would have tagged it as such.
#pragma once
Add that at the very top of all your header files.
However, keep in mind that even though it is very well supported by compilers, it's not a standard.
You need to use include guards. The typical form is:
#ifndef NAME_OF_HEADER_H
#define NAME_OF_HEADER_H
// Rest of header code here.
#endif
Since #include in C++ just does a copy-paste of the text in the current file if the same header gets included twice that text will result in duplicate class definitions. What the include guard does is prevent the multiple inclusion of the same header.
EDIT: The problem is that you check for definition of MAMMAL_H and then define MAMNAL_H (note the N in the defined version). I always copy-paste to generate my include guards for precisely this reason.
I guess you forgot include guards. Use #ifndef /#ifdef/ #endif as John suggested.
#ifndef MAMMAL_H
#define MAMMAL_H
... definition of mammal
#endif