Переопределение типа класс ошибка

Есть главный файл main.cpp и main.h. В main.cpp подключается заголовок Engine.h. Компилятор выдает ошибку переопределения типа «class»

main.cpp

#include "main.h"
#include "Engine.h"

using namespace std;

int main()
{
    Game game = Game("init");
    cout << game.getState() << endl;
    return 0;
}

main.h

#pragma once

#include <iostream>

Engine.cpp

#include "Engine.h"

using namespace std;

class Game
{
public:
    Game(string state)
    {
        this->state = state;
    }

    string getState()
    {
        return this->state;
    }
private:
    string state;
};

Engine.h

#pragma once

#include <string>
using namespace std;

class Game
{
public:
    Game(string state);
    string getState();
private:
    string state;
};

Собирается через CMake

Harry's user avatar

Harry

215k15 золотых знаков117 серебряных знаков228 бронзовых знаков

задан 1 апр в 11:26

Vlaffi's user avatar

Рассмотрим ваш Engine.cpp:

#include "Engine.h"

using namespace std;

class Game
{
public:

Т.е., когда будет включен ваш Engine.h, получится что-то вроде

#include <string>
using namespace std;

class Game
{
public:
    Game(string state);
    string getState();
private:
    string state;
};

using namespace std;

class Game
{
public:

...

Т.е. класс Game у вас определяется в этой единице трансляции дважды.

Дальше пояснять не нужно?

ответ дан 1 апр в 11:30

Harry's user avatar

HarryHarry

215k15 золотых знаков117 серебряных знаков228 бронзовых знаков

Повторно определять класс class Game { нельзя.
А в Engine.cpp можно сделать полное определение методов которые вы предварительно объявили так :

# include "Engine.h"

Game :: Game(string state)
{
  this->state = state;
}

string Game :: getState()
{
  return this->state;
}

где Game :: означает область (класс), для кого эта функция определяется.

ответ дан 1 апр в 12:43

AlexGlebe's user avatar

AlexGlebeAlexGlebe

16.8k1 золотой знак9 серебряных знаков33 бронзовых знака

I have been attempting to work with classes in c++ for the first time. My circle class and associated header file were working fine, I then moved some files and since then keep getting an error which i have displayed below.

c:circleobje.cpp(3): error C2011: 'CircleObje' : 'class' type redefinition

c:circleobje.h(4) : see declaration of 'CircleObje'

CircleObje.h

#ifndef CircleObje_H
#define CircleObje_H
class CircleObje
{
public:
void setCol(float r, float g, float b);
void setCoord(int x, int y);
float getR();
float getG();
float getB();
int getX();
int getY();
};

#endif

CircleObje.cpp

#include "CircleObje.h"

class CircleObje {

float rVal, gVal, bVal;
int xCor, yCor;

public:

void setCol(float r, float g, float b)
{
    rVal = r;
    gVal = g;
    bVal = b;
}

void setCoord(int x, int y)
{
    xCor = x;
    yCor = y;
}

...
};

I haven’t copied all of the .cpp functions as I didn’t think they were relevant. These files were working without issue before I moved the file locations. Even after renaming them I still have the same error as above. Any ideas to solve the problem?

asked Jan 25, 2014 at 17:56

Tom smith's user avatar

3

The issue is that you are defining the class twice just as the compiler is telling you. In the cpp you should provide the definitions of the functions like so:

MyClass::MyClass() {
  //my constructor
}

or

void MyClass::foo() {
   //foos implementation
}

so your cpp should look like:

void CirleObje::setCol(float r, float g, float b)
{
    rVal = r;
    gVal = g;
    bVal = b;
}

void CircleObje::setCoord(int x, int y)
{
    xCor = x;
    yCor = y;
}

...

And all the class variables should be defined in the .h file inside of your class.

answered Jan 25, 2014 at 17:58

pippin1289's user avatar

pippin1289pippin1289

4,8212 gold badges22 silver badges37 bronze badges

You have defined the class twice, in the header and in the cpp, so in the .cpp the compiler sees two definitions. Remove the definition of the class on the .cpp.

Class functions should be implemented in the cpp in this way:

<return_type> <class_name>::<function_name>(<function_parameters>)
{
    ...
}

Consider this example class:

//foo.hpp

struct foo
{
    int a;

    void f();
}

The class is implemented in the foo.cpp file:

#include "foo.hpp"

void foo::f()
{
    //Do something...
}

answered Jan 25, 2014 at 17:58

Manu343726's user avatar

Manu343726Manu343726

13.9k3 gold badges38 silver badges74 bronze badges

you are declaring your class multiple times once in header file and another in .cpp file which is redefining your class.

CircleObje.h

#ifndef CircleObje_H
#define CircleObje_H
class CircleObje
{
public:
void setCol(float r, float g, float b);
void setCoord(int x, int y);
float getR();
float getG();
float getB();
int getX();
int getY();
public:
float rVal, gVal, bVal;
int xCor, yCor;



};

#endif



CircleObje.cpp

#include "CircleObje.h"



void CircleObje::void setCol(float r, float g, float b)
{
    rVal = r;
    gVal = g;
    bVal = b;
}

void CircleObje::setCoord(int x, int y)
{
    xCor = x;
    yCor = y;
}

answered Jan 25, 2014 at 18:09

java seeker's user avatar

java seekerjava seeker

1,24610 silver badges13 bronze badges

Remove class CircleObje {, public and the ending bracket }; and it should work. You already defined your class in the .H, thus no need to redefine it in the CPP.

Also, you should write your member implementation (in CPP file) like this :

float CircleObje::getR() { /* your code */ } 

answered Jan 25, 2014 at 17:59

Gabriel L.'s user avatar

Gabriel L.Gabriel L.

4,6185 gold badges25 silver badges34 bronze badges

You have to put #pragma once in the first line of the header file, then the errors will disappear.

avariant's user avatar

avariant

2,2345 gold badges24 silver badges33 bronze badges

answered Feb 23 at 22:04

ahmed ezzat's user avatar

1

В проекте есть 3 файла: объявление класса в Auto.h, реализация его методов в Auto.cpp и основной код программы в AutoCenter.cpp.

Вот файл класса Auto.h:

#include <iostream>
#include "stdafx.h"
#include "windows.h"
#include <string>

using namespace std;

class Auto {
private:
	int year;
	int ownersNumber;
	string color;
	string brand;
	string model;
public:
	void setYear(int y);
	void setOwnersNumber(int n);
	void setColor(string c);
	void setBrand(string b);
	void setModel(string m);

	int getYear();
	int getOwnersNumber();
	string getColor();
	string getBrand();
	string getModel();

	void displayAll();
};

Вот файл его реализации Auto.cpp:

#include "stdafx.h"
#include "windows.h"
#include "Auto.h"
#include <string>
#include <iostream>

void Auto::setYear(int y) {
	year = y;
}
void Auto::setBrand(string b) {
	brand = b;
}
void Auto::setModel(string m) {
	model = m;
}
void Auto::setColor(string c) {
	color = c;
}
void Auto::setOwnersNumber(int n) {
	ownersNumber = n;
}
int Auto::getYear() {
	return year;
};
int Auto::getOwnersNumber() {
	return ownersNumber;
};
string Auto::getColor() {
	return color;
};
string Auto::getBrand() {
	return brand;
};
string Auto::getModel() {
	return model;
};

Вот основной код:

#include <iostream>
#include "stdafx.h"
#include "windows.h"
#include "Auto.h"
#include "Auto.cpp"
#include <string>

using namespace std;

int main()
{
	Auto auto1;
	auto1.setBrand("Mercedes");
	auto1.setModel("C200");
	auto1.setColor("red");
	auto1.setYear(2005);
	auto1.setOwnersNumber(2);

	system("pause");
    return 0;
}

На этапе компиляции вылетает 27 ошибок, основная причина которых — «Auto: переопределение типа class». Где тут переопределение, я не могу понять? Подскажите, в чём может быть ошибка? Или это VS гребет?

1 / 1 / 3

Регистрация: 28.01.2013

Сообщений: 169

1

06.11.2013, 23:23. Показов 9411. Ответов 3


Студворк — интернет-сервис помощи студентам

Как я понял, ошибка «переопределение типа «class»» означает что класс уже был где-то объявлен, однако у меня он вроде нигде больше не объявлен. Вот снизу скрин и кину проект(потому-что 11 файлов и расписывать что тут где как-то неудобно)

Миниатюры

Переопределение типа "class"
 



0



I.M.

574 / 557 / 47

Регистрация: 16.12.2011

Сообщений: 1,389

06.11.2013, 23:59

2

C++
1
#pragma once

первой строкой каждого *.h файла



1



ququ_smile

1 / 1 / 3

Регистрация: 28.01.2013

Сообщений: 169

07.11.2013, 00:08

 [ТС]

3

Цитата
Сообщение от I.M.
Посмотреть сообщение

C++
1
#pragma once

первой строкой каждого *.h файла

Класс, работает, а что это и зачем это?



0



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?

meJustAndrew's user avatar

meJustAndrew

5,6177 gold badges52 silver badges71 bronze badges

asked Sep 7, 2014 at 19:01

user3164272's user avatar

1

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.

answered Sep 7, 2014 at 19:06

Ashot's user avatar

AshotAshot

10.6k13 gold badges63 silver badges114 bronze badges

3

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;
};

answered Jun 7, 2016 at 18:09

empty's user avatar

emptyempty

4,9923 gold badges32 silver badges57 bronze badges

1

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.

answered Sep 7, 2014 at 20:17

ScottMcP-MVP's user avatar

ScottMcP-MVPScottMcP-MVP

10.3k2 gold badges14 silver badges15 bronze badges

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.

Yun's user avatar

Yun

2,8346 gold badges8 silver badges28 bronze badges

answered Sep 15, 2021 at 5:48

Chaohsiung Huang's user avatar

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.

Here comes the pitfall:

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 Project1MyHeader.h (or MyProject2MyHeader.h) with MyProjectMyHeader.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!

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.

Here comes the pitfall:

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 Project1MyHeader.h (or MyProject2MyHeader.h) with MyProjectMyHeader.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!

У меня есть эта ошибка компилятора (C2011) с этим куском кода. Я не знаю, что с этим не так.

Пространство имен (Ogre) не имеет определения для PlaneMovement, Я также попробовал другое имя и все те же ошибки.

#include <Ogre.h>

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;
};

6

Решение

Включить охрану:

#ifndef FILE_H
#define FILE_H

//file contents here

#endif

По этой причине заголовочные файлы должны включать в себя элементы защиты — многократное включение в одну и ту же единицу перевода может привести к множественному определению.

Альтернатива использует

#pragma once

но это поддерживается не всеми компиляторами.

28

Другие решения

Если кто-то еще сталкивается с этой ситуацией, это может произойти, когда библиотека включается в свойство проекта, а заголовочные файлы из этой библиотеки включаются в файл проектов.

0

Неверная предварительная декларация

Еще одна возможная причина, если вы такой глупец, как я, может быть то, что вы использовали enum вместо class когда вперед объявляет class,

File1.h

namespace MyNamespace { enum NotActuallyAnEnum; }

File2.h

class NotActuallyAnEnum

{
...
}

Это приведет к следующей ошибке:

error C2011: 'enum' type redefinition

Очевидно, что исправление заключается в исправлении предварительной декларации:

namespace MyNamespace { class NotActuallyAnEnum; }

0

Вы также можете получить эту ошибку, если у вас есть несколько веток вашего проекта на вашей станции разработки и вы используете символическую ссылку, чтобы указать на одну из них.

Предположим, у вас есть две разные ветви вашего решения под названием Project1 а также Project2 и вы даете символическую ссылку под названием Project указать либо Project1 или же Project2,

Идея состоит в том, что вы можете переключаться между ветками, и проект всегда будет выглядеть Project для вашего приложения и, возможно, некоторые другие инструменты, которые ожидают его там.

отказДа, управление версиями может переключаться между ветвями, но таким образом вам не придется перестраивать все приложение каждый раз, когда вы переключаете ветки. Кроме того, обе ветви все еще могут находиться под контролем версий.

Хорошо, так что открытие Project откроется либо Project1 или же Project2 в зависимости от символической ссылки. Символическая ссылка может быть удалена / создана каким-то простым mklink_1 а также mklink_2 как файлы сценариев.

Здесь идет ловушка:

Если вы не обращаете внимания и непосредственно открываете решение в местоположении 1 или 2 напрямую (вместо того, чтобы следовать символической ссылке на каталог в Visual Studio), препроцессор может быть обманут в смешивании Project1MyHeader.h (или же MyProject2MyHeader.h) с MyProjectMyHeader.h!

Даже это технически один и тот же файл, препроцессор не знает о символической ссылке. Так вот #pragma once не спасет тебя!

0

1 / 1 / 3

Регистрация: 28.01.2013

Сообщений: 169

1

06.11.2013, 23:23. Показов 8097. Ответов 3


Как я понял, ошибка «переопределение типа «class»» означает что класс уже был где-то объявлен, однако у меня он вроде нигде больше не объявлен. Вот снизу скрин и кину проект(потому-что 11 файлов и расписывать что тут где как-то неудобно)

Миниатюры

Переопределение типа "class"
 

__________________
Помощь в написании контрольных, курсовых и дипломных работ, диссертаций здесь

0

I.M.

574 / 557 / 47

Регистрация: 16.12.2011

Сообщений: 1,389

06.11.2013, 23:59

2

C++
1
#pragma once

первой строкой каждого *.h файла

1

ququ_smile

1 / 1 / 3

Регистрация: 28.01.2013

Сообщений: 169

07.11.2013, 00:08

 [ТС]

3

Цитата
Сообщение от I.M.
Посмотреть сообщение

C++
1
#pragma once

первой строкой каждого *.h файла

Класс, работает, а что это и зачем это?

0

    msm.ru

    Нравится ресурс?

    Помоги проекту!

    >
    переопределение типа «class»
    , чушь какая-то…

    • Подписаться на тему
    • Сообщить другу
    • Скачать/распечатать тему



    Сообщ.
    #1

    ,
    20.06.09, 11:12

      Вот код:

      ExpandedWrap disabled

        #include <map>

        #include «pugixml.hpp»

        class Settings

        {

        public:

            void loadConfig(std::string filename)

            {

                pugi::xml_document config;

                config.load_file(filename.c_str());

                if (pugi::xml_node root = config.child(«OiHotkeyConfig»))

                {

                    loadFonts(root.child(«Fonts»));

                }

            };

            HFONT getFont(std::wstring name)

            {

                return fonts[name];

            };

        private:

            std::map<std::wstring, HFONT> fonts;

            void loadFonts(pugi::xml_node fontsNode)

            {

                if (fontsNode)

                    for (pugi::xml_node font = fontsNode.child(«Font»); font; font = font.next_sibling())

                    {

                        HFONT curFont = CreateFont(font.attribute(«height»).as_int(),

                                                   font.attribute(«width»).as_int(),

                                                   0,

                                                   0,

                                                   font.attribute(«weight»).as_int(),

                                                   font.attribute(«italic»).as_bool(),

                                                   font.attribute(«underline»).as_bool(),

                                                   font.attribute(«strikeout»).as_bool(),

                                                   DEFAULT_CHARSET,

                                                   OUT_DEFAULT_PRECIS,

                                                   CLIP_DEFAULT_PRECIS,

                                                   DEFAULT_QUALITY,

                                                   DEFAULT_PITCH|FF_DONTCARE,

                                                   (LPCWSTR)font.attribute(«face»).value()

                                                   );

                        fonts[pugi::as_utf16(font.attribute(«name»).value())]=curFont;

                    }

            };

        };

      Компилятор(VC++ 2008) ругается на объявление класса:

      Цитата

      d:моеprogrammingviewerviewersrcsettings.h(5) : error C2011: Settings: переопределение типа «class»

      Что это значит? и что делать?


      Twilight



      Сообщ.
      #2

      ,
      20.06.09, 11:16

        Senior Member

        ****

        Рейтинг (т): 24

        а компилятор так и пишет сообщения на русском?
        скорее всего он имеет в виду — что Settings уже объявлен


        Flex Ferrum



        Сообщ.
        #3

        ,
        20.06.09, 11:19

          Цитата miksayer @ 20.06.09, 11:12

          Компилятор(VC++ 2008) ругается на объявление класса:

          концовку файла pugixml покажи, плз. :)


          kanes



          Сообщ.
          #4

          ,
          20.06.09, 11:20

            Цитата Twilight @ 20.06.09, 11:16

            а компилятор так и пишет сообщения на русском?

            Может и на руском писать в русской студии


            miksayer



            Сообщ.
            #5

            ,
            20.06.09, 11:21

              концовка pugixml.hpp:

              ExpandedWrap disabled

                    template <typename Predicate> inline xml_node xml_node::find_node(Predicate pred) const

                    {

                        if (!empty())

                            for (xml_node node = first_child(); node; node = node.next_sibling())

                            {

                                if (pred(node))

                                    return node;

                                if (node.first_child())

                                {

                                    xml_node found = node.find_node(pred);

                                    if (found) return found;

                                }

                            }

                        return xml_node();

                    }

                }

                #endif

              Цитата Twilight @ 20.06.09, 11:16

              а компилятор так и пишет сообщения на русском?

              да, студия русская

              Цитата Twilight @ 20.06.09, 11:16

              скорее всего он имеет в виду — что Settings уже объявлен

              нет, это не так :)


              Flex Ferrum



              Сообщ.
              #6

              ,
              20.06.09, 11:25

                miksayer, запяточие перед #endif поставь. :)


                miksayer



                Сообщ.
                #7

                ,
                20.06.09, 11:29

                  блин, сколько раз уже на эти грабли наступал:

                  ExpandedWrap disabled

                    #ifndef SETTINGS_INCLUDED

                    #define SETTINGS_INCLUDED

                    ….

                    #endif

                  отсюда и эта ошибка
                  всем спасибо, вопрос решен

                  Добавлено 20.06.09, 11:30
                  Flex Ferrum, да не в этом проблема, как оказалось :)

                  Добавлено 20.06.09, 11:30
                  т.е. я включал Settings.h в несколько файлов и такая вот фигня получилась


                  Flex Ferrum



                  Сообщ.
                  #8

                  ,
                  20.06.09, 11:51

                    Цитата miksayer @ 20.06.09, 11:29

                    Flex Ferrum, да не в этом проблема, как оказалось :)

                    И в этом тоже, я думаю.


                    Alexandoros



                    Сообщ.
                    #9

                    ,
                    22.06.09, 09:56

                      Цитата miksayer @ 20.06.09, 11:12

                      Компилятор(VC++ 2008) ругается на объявление класса:

                      Цитата

                      d:моеprogrammingviewerviewersrcsettings.h(5) : error C2011: Settings: переопределение типа «class»

                      Что это значит? и что делать?

                      Цитата

                      Compiler Error C2011
                      Error Message
                      ‘identifier’ : ‘type’ type redefinition
                      The identifier was already defined as type. You may also get C2011 if you import a type library more than once into the same file.

                      Выводы делай сам.

                      Еще есть #pragma once.

                      0 пользователей читают эту тему (0 гостей и 0 скрытых пользователей)

                      0 пользователей:

                      • Предыдущая тема
                      • C/C++: Общие вопросы
                      • Следующая тема

                      Рейтинг@Mail.ru

                      [ Script execution time: 0,0836 ]   [ 16 queries used ]   [ Generated: 30.01.23, 01:14 GMT ]  

                    • Перенос слова является ли орфографической ошибкой
                    • Перезапустите игру ошибка видеокарты
                    • Перенос слова это грубая ошибка или нет
                    • Перезапуск скрипта python при ошибке
                    • Перенос слова считается орфографической ошибкой