【问题标题】:error C2011 and defined错误 C2011 并定义
【发布时间】:2014-11-23 09:09:06
【问题描述】:

我搜索了错误 2011 我找到了error C2011: '' : 'class' type redefinition,而且我的 c++ 代码中肯定有 #ifndef 和 #define。

这是我的代码

//Member.h
#ifndef MEMBER_H
#define MEMBER_H

#include <string>
using namespace std;

class member
{
private:
    int id;
    string name;
    char sex;
    int age;
public:
    void addMember();
    void setName(string n);
    void setSex(char s);
    void setAge(int a);
};
#endif

.

//Member.cpp
#include <string>
#include "Member.h"
using namespace std;

class member
{
private:
    int id;
    string name;
    char sex;
    int age;
public:
    void addMember()
    {
        void setName(string n);
        void setSex(char s);
        void setAge(int a);
        //relationship
    }
    void setName(string n) { name = n; }
    void setSex(char s) { sex = s; }
    void setAge(int a) { age = a; }
};

它给了我同样的错误 C2011。请帮忙

【问题讨论】:

  • 可能在其他地方定义了另一个类成员?
  • 您的 .cpp 文件之一中是否有其他自定义标头在包含此标头之前?也许您忘记了另一堂课结束时的;,忘记了结束时的}#endif。这是导致这类错误的常见原因,因为class 是关键字而不是类型,所以之前一定有语法错误。
  • 顺便说一句,标头中的 using 指令很糟糕。

标签: c++


【解决方案1】:

您已经明确定义了类 member 两次——一次在头文件中,一次在 C++ 文件中,就像编译器告诉您的问题一样。

【讨论】:

    猜你喜欢
    • 2014-11-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-23
    • 1970-01-01
    • 2021-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多