error C2011: » : ‘class’ type redefinition
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.
AAA_HEADER is just unique string to identify the file. Read about include guards here stackoverflow.com/questions/8020113/c-include-guards
to the top of your AAA.h file should take care of the problem.
#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; >;
#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.
Error C2011: ‘XX’ : ‘class’ type redefinition
I have this compiler error (C2011) with this piece of code. I don’t know what is wrong with it. The namespace (Ogre) doesn’t have a definition for PlaneMovement . I also tried a different name and still the same errors.
#include using namespace Ogre; class PlaneMovement < public: PlaneMovement(Degree startingAngle, Real velocity = 2, Real gravity = 2); Vector2 updateMovement(const FrameEvent& evt); private: Degree currentAngle; Real currentVelocityX; Real currentVelocityY; Real gravity; bool top; >;
4 Answers 4
#ifndef FILE_H #define FILE_H //file contents here #endif
Header files should have include guards for this exact reason — multiple inclusion in the same translation unit can lead to a multiple definition.
but this isn’t supported by all compilers.
@Pacha no, you should surround your header files with them (replace FILE_H with the name of your file).
If someone else faces this situation, it may be when library includes in property of project and header files from this library include in projects file.
Incorrect Forward Declaration
Another possible cause, if you’re a goof like me, could be that you used enum instead of class when forward declaring a class .
This will produce something like the following error:
error C2011: 'enum' type redefinition
Obviously the fix is to correct the forward declaration:
You might also get this error if you have multiple branches of your project on your development station and use a symlink to point to one of them.
Let’s suppose you have two different branches of your solution called Project1 and Project2 and you let a symlink called Project point to either Project1 or Project2 .
The idea is that you could switch between branches and the project would always appear to be Project to your application and possibly some other tools that expect it there.
Disclaimer: yes, version control can switch between branches, but this way you will not have to rebuild the whole application every single time you switch branches. Besides both branches can still be under version control.
Okay, so opening Project would open either Project1 or Project2 depending on the symlink. The symlink could be removed/created by some simple mklink_1 and mklink_2 like script files.
If you don’t pay attention and directly open the solution at location 1 or 2 directly (instead of following the directory symlink with Visual Studio), the pre-processor might be tricked into mixing Project1\MyHeader.h (or MyProject2\MyHeader.h ) with MyProject\MyHeader.h !
Even those are technically the same file, the preprocessor doesn’t know about the symlink. So here the #pragma once would not save you!
Cpp class type redefinition
Hello,
I thought i understood the passing objects by reference well. not though.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Integer:public Object < public: int value; Integer(int value); ~Integer(); int intValue(); >; Integer::Integer(int value) < Integer::value=value; >Integer::~Integer() < value=0; DBGPRINTF("Destructor of Integer ",value); > int Integer::intValue() < return value; >
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class Vector:public Object < public: Vector(); ~Vector(); void addElement(Object& object); int size(); Object& elementAt(int index); private: int datasize; Object* data; >; Vector::Vector() < datasize=0; data=new Object[100]; > Vector::~Vector() < delete [] data; DBGPRINTF("Destructor of Vector"); > void Vector::addElement(Object& object) < data[datasize]=object; datasize++; >Object& Vector::elementAt(int index) < return data[index]; > int Vector::size() < return datasize; >
int main() < Vector v; Integer a(100); Integer b(200); v.addElement(a); v.addElement(b); Integer c=static_cast(v.elementAt(0)); int value_a=a.intValue(); // 100 int value_c=c.intValue(); // -842198006 return 0; >
Now logically, value_a and value_b should be same.
Because, i have passed Integer object by reference.
It means Integer Reference «a» and «c» should be same. but it seems they aren’t.
Looked at concept of passing by references on net. the concept applied seems to be same.
Your problem is called splicing.
In Vector, you declare an array of Objects by pointer: Object* data; .
When you new Object[100] , you are allocating memory for 100 Objects. Object is an empty class.
Now in addElement, you take an object passed by reference and put it into the array. The array holds only Objects, however. This means that the compiler is «splicing» your parameter into two pieces — the Object part and «the «rest» — and it is copying ONLY THE Object PART into the array.
You have essentially lost the derived portion of your object at this point. This, of course, then makes your static_cast on line 9 in Mainc.cpp invalid.
This is not going to be an easy thing to fix. However, I will make one suggestion to you (kind of related):
Use dynamic_cast<> instead of static_cast<> for polymorphic «downcasting». This will actually cause the compiler to generate code to ensure the cast is legal. Had you done this on the above code, an exception would have been thrown on line 9.
(Note: if you dynamic_cast to a pointer, NULL is returned if the cast fails. if you dynamic_cast to a reference, an exception is thrown on failure instead).
«This means that the compiler is «splicing» your parameter into two pieces — the Object part and «the «rest» — and it is copying ONLY THE Object PART into the array.» —>
This means that i have to make an array for holding references to objects.
But a bit of look here and there. got to know that creating array for holding references is not possible in c++.
so, what can be the possible solution to it?
Probably pointers are your only option, but that could be a fairly significant change to your application.
C++ ‘class’ type redefinition error
I’m learning Visual C++ and building the «Sketcher» example program from Ivor Horton’s Visual book, and I was able to generate the toolbars, menu icons, prompts, and a rectangle drawn with a few hard-coded points. After adding some mouse handlers and references to a few other header files, I receive this error: error C2011: ‘CElement’ : ‘class’ type redefinition . Because of this «redefinition», there are 40+ errors thrown in the other shape classes that were previously based on CElement can’t seem to find that class at compile time although the Visual Studio’s intellisense seems to detect it just fine. I don’t think I’m missing anything since everything compiled properly before my last few changes, but I’ve exhausted my undo/redo history and can’t seem to find a root cause. I’ve looked at some of the other answers related to this error message, but I’m still stuck on pinpointing exactly where my class is being defined a second time. My Element.h file contains the following code:
#include #include "stdafx.h" class CElement : public CObject < protected: CPoint m_StartPoint; // Element position int m_PenWidth; // Pen width COLORREF m_Color; // Color of an element CRect m_EnclosingRect; // Rectangle enclosing an element // Create a pen void CreatePen(CPen& aPen); public: virtual ~CElement(); virtual void Draw(CDC* pDC) <>// Virtual draw operation // Get the element enclosing rectangle const CRect& GetEnclosingRect() const < return m_EnclosingRect; >protected: // Constructors protected so they cannot be called outside the class CElement(); CElement(const CPoint& start, COLORREF color, int penWidth = 1); >;
#include "stdafx.h" #include "Element.h" CElement::CElement() < >void CElement::CreatePen(CPen& aPen) < if (!aPen.CreatePen(PS_SOLID, m_PenWidth, m_Color)) < // Pen creation failed AfxMessageBox(_T("Pen creation failed."), MB_OK); AfxAbort(); >> CElement::~CElement()
3 Answers 3
Suggestion: use an include guard, in ALL of your header files:
#ifndef ELEMENT_H #define ELEMENT_H #include #include "stdafx.h" class CElement : public CObject < protected: CPoint m_StartPoint; // Element position . protected: // Constructors protected so they cannot be called outside the class CElement(); CElement(const CPoint& start, COLORREF color, int penWidth = 1); >; #endif
Thank you for the suggestion and the reference. This solved the issue and taught me something not yet covered in the book or my classes.