error C2011: '' : 'class' type redefinition - c++

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.

Related

(Redefinition error) Create multiple inherting classes from one baseclass in C++

Greetings oh mighty coders,
I am a beginner and in a bit of trouble here.
There is my baseclass (sensor.h):
class sensor
{
private:
int sensor_id;
string sensor_name;
string sensor_type;
float reading;
public:
sensor();
sensor(int, char*, char*);
~sensor();
/* Few extra methods here */
};
... and I want to create 4 other classes that inherit from my baseclass sensor
(temperaturesensor, humiditysensor... and so on).
#include "sensor.h"
class temperaturesensor:public sensor
{
public:
Temperatursensor(int, char*,char*);
~Temperatursensor();
/* Few extra methods here */
};
Thing is: Every single one of these classes has to be in its own .cpp/.h file and then be included and used in my main.cpp.
using namespace std;
#include <xyz.h>
/* Other libaries here */
....
#include "temperaturesensor.h"
#include "humiditysensor.h"
int main()
{
sensor* station[2];
station [0] = new temperaturesensor(x,y,z);
station [1] = new humiditysensor(x,y,z);
}
If I include one of them it's no biggie. However: If I use multiple ones I get an redefinition error.
error C2011: 'sensor': 'class' typeredefinition
c:\users\name\desktop\project\sensor.h 14
error c2011: 'temperaturesensor' : 'class' typeredefinition
What can I do to workaround this? Note that I am not allowed to use #pragma once
Sorry for my stupidity and thanks in advance!
You must use:
#ifndef FILE_H
#define FILE_H
.. normal code here
#endif
or
#pragma once
but too, I think, that sensor schould be abstract class and you schould use virtual destructor.
One more think is that array is numerate from 0.
you forgot to use the include guards in your header class,
this is redefining your base class everytime you use it.
so, just do a
#pragma once
or a normal include guard
#ifndef YOURFILENAME_H
#define YOURFILENAME_H
.. normal code here
#endif
Then you will not have the multiple definition error.
The definition of the class sensor is coming from both "temperaturesensor.h"
and "humiditysensor.h". Use guards https://en.wikipedia.org/wiki/Include_guard or #pragma once: https://en.wikipedia.org/wiki/Pragma_once

Unknown class? C2143 syntax error: missing ";" before '*'

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

C++ Compiler Issues

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.

cyclic includes, how can I resolve this without changing class hierarchy

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

Error while trying to use class in another class

I'm writing something in C++. I have 2 classes which I want to contain one into the other as in the folowing (these are just the header files):
//Timing.h
#ifndef _Timing_h
#define _Timing_h
#include "Agent.h"
class Timing{
private:
typedef struct Message{
Agent* _agent; //i get here a compilation problem
double _id;
} Message;
typedef struct MessageArr{
} MessageArr;
public:
Timing();
~Timing();
};
#endif
//Agent.h
#ifndef _Agent_h
#define _Agent_h
#include <string>
#include "Timing.h"
using namespace std;
class Agent{
public:
Agent(string agentName);
void SetNextAgent(Agent* nextAgent);
Agent* GetNextAgent();
void SendMessage(Agent* toAgent, double id);
void RecieveMessage(double val);
~Agent();
private:
string _agentName;
double _pID;
double _mID;
Agent* _nextAgent;
};
#endif
The compilation error is in the Timing.h file inside the definition of the struct:
expected ';' before '*' token
What am I doing wrong?
Try not to include "Agent.h" in Timing.h but include a forward reference instead:
#ifndef _Timing_h
#define _Timing_h
class Agent;
class Timing{
private:
typedef struct Message{
Agent* _agent; //I get here a compilation problem
double _id;
}Message;
typedef struct MessageArr{
}MessageArr;
public:
Timing();
~Timing();
};
#endif
You can include Agent.h in the timing.cpp file.
This way you remove the circular reference and you reduce the coupling between the classes.
Since you don't use the class Timing in your class Agent, you can remove this include as well (but this might be a copy mistake from your shortened example).
Basically - whenever you need either the size of an object or some of it's functionality, you must include its header file. If you don't need it (e.g. if you use only pointers to this object or references), you should not. This reduces compile time (especially for large projects)
For the 1 instance problem - check your favorite design patterns book (e.g. the GoF). The singleton pattern might be what you need.
Rule of thumb.
Do not include other header files from your header files if you don't need to.
Pre-Compiled header file stuff being a notable exception.
If your class only depends on a pointer or a reference you do not need the header file:
Use forward declaration in this situation.
In the source file include only the header files you need to make it work
Include them from most specific to least specific.
This will prevent the problem of hiding a dependency.
Other notes:
Do not use Underscore followed by a capitol letter.
This is reserved for the implementation. see
As in #define _Timing_h
Also note it is traditional that macros are all upper case.
Do not put using namespace X; in a header file
If you do this you pollute the namespace for everybody that uses your header file.
This is a real easy way to PO other developers who now have to re-factor their code to make sure it does not use any of a bunch of new classes/functions/templates that are suddenly being resolved against that was not there before.
So try this:
Timing.h
#ifndef TIMING_H
#define TIMING_H
class Agent;
class Timing{
// STUFF
};
#endif
Agent.h
#ifndef AGENT_H
#define AGENT_H
#include <string>
class Agent{
// STUFF
};
#endif
Timing.cpp
#include "Timing.h"
#include "Agent.h"
// STUFF
Agent.h
#include "Agent.h"
using std::string; // Bring as little as possable into the the global namespace.
// prefer to prefix all cases with std::
// STUFF.
You can't have circular includes.
Stop including "Timing.h" from "Agent.h", since it's not needed there.
Also, you don't need to have the "Agent.h" included in "Timing.h" either, just use a forward reference:
class Agent;
This makes it possible to have pointers to something called Agent.
You need to add the forward declaration of Agent in Timing.h
// Timing.h
#ifndef _Timing_h
#define _Timing_h
class Agent; // fwd declaration.
class Timing{
private:
typedef struct Message{
Agent* _agent; // without fwd decln Agent type is unknown here.
// rest all same.
EDIT:
As suggested by others, you should not be including Agent.h in Timing.h