相关疑难解决方法(0)

为什么我会重新定义类错误?

代码转储道歉:

gameObject.cpp:

#include "gameObject.h"
class gameObject
{
    private:
    int x;
    int y;
    public:
    gameObject()
    {
    x = 0;
    y = 0;
    }

    gameObject(int inx, int iny)
    {
        x = inx;
        y = iny;
    }

    ~gameObject()
    {
    //
    }
    int add()
    {
        return x+y;
    }
};
Run Code Online (Sandbox Code Playgroud)

gameObject.h:

class gameObject
{
    private:
    int x;
    int y;
    public:
    gameObject();

    gameObject(int inx, int iny);
    ~gameObject();
    int add();
};
Run Code Online (Sandbox Code Playgroud)

错误:

||=== terrac, Debug ===|
C:\terrac\gameObject.cpp|4|error: redefinition of `class gameObject'|
C:\terrac\gameObject.h|3|error: previous definition of `class gameObject'| …
Run Code Online (Sandbox Code Playgroud)

c++ class redefinition

17
推荐指数
5
解决办法
10万
查看次数

C++:不同翻译单元中具有相同名称的不同类

请考虑以下示例:

// usedclass1.hpp  
#include <iostream>  
class UsedClass
{  
public:
  UsedClass() { }  
  void doit() { std::cout << "UsedClass 1 (" << this << ") doit hit" << std::endl; }
};  

// usedclass2.hpp  
#include <iostream>
class UsedClass
{
public:
  UsedClass() { }
  void doit() { std::cout << "UsedClass 2 (" << this << ") doit hit" << std::endl; }
};

// object.hpp
class Object
{
public:
  Object();
};

// object.cpp
#include "object.hpp"
#include "usedclass2.hpp"
Object::Object()
{
  UsedClass b;
  b.doit();
}

// …
Run Code Online (Sandbox Code Playgroud)

c++ one-definition-rule

7
推荐指数
2
解决办法
1717
查看次数

标签 统计

c++ ×2

class ×1

one-definition-rule ×1

redefinition ×1