Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 错误:重新定义类[类名]_C++_Error Handling_Include Guards - Fatal编程技术网

C++ 错误:重新定义类[类名]

C++ 错误:重新定义类[类名],c++,error-handling,include-guards,C++,Error Handling,Include Guards,我经常看到这个错误,这里有很多关于它的问题,但现在我真的不知道该怎么办 用户.h #ifndef USER_H #define USER_H #endif // USER_H #include <iostream> using namespace std; class User { private: struct Accounts {string user, password, name;} accounts[2]; void setAccounts(); p

我经常看到这个错误,这里有很多关于它的问题,但现在我真的不知道该怎么办

用户.h

#ifndef USER_H
#define USER_H
#endif // USER_H

#include <iostream>
using namespace std;


class User
{
private:
    struct Accounts {string user, password, name;} accounts[2];
    void setAccounts();

public:
    int Access(string user, string password);
    bool online;

    User();
    ~User();
};
\ifndef用户
#定义用户
#endif//USER\u H
#包括
使用名称空间std;
类用户
{
私人:
结构帐户{string user,password,name;}帐户[2];
作废账户();
公众:
int访问(字符串用户、字符串密码);
布尔在线;
用户();
~User();
};
User.cpp

#include "User.h"

#include <iostream>

User::User() {/* data */}
User::~User() {/* data */}

void User::setAccounts()
{
    accounts[0].user = "user01";
    accounts[0].password = "pw01";
    accounts[0].name = "hi";

    accounts[1].user = "user02";
    accounts[1].password = "pw02";
    accounts[1].name = "hi2";
}

int User::Access(string user, string password)
{
    unsigned short int i;

    for (int i = 0; i <= 1; i++)
    {
        if (user.compare(this->accounts[i].user) == 0 and password.compare(this->accounts[i].password) == 0)
            return 0;
    }

    return 1;
}
#包括“User.h”
#包括
User::User(){/*数据*/}
用户::~User(){/*data*/}
void User::setAccounts()
{
帐户[0]。user=“user01”;
帐户[0]。密码=“pw01”;
帐户[0]。name=“hi”;
帐户[1]。user=“user02”;
帐户[1]。密码=“pw02”;
账户[1]。名称=“hi2”;
}
int User::Access(字符串用户、字符串密码)
{
无符号短整数i;
对于(int i=0;i accounts[i].user)==0和password.compare(this->accounts[i].password)==0)
返回0;
}
返回1;
}
我甚至使用过
#pragma一次
,但它仍然无法识别该类。 我该怎么办

@edit:我向下移动了
#endif//USER\u H
,现在该类已被识别,但构造函数方法仍然丢失

“错误:“User::User()”的多个定义”

您应该将
#endif//USER_H
放在头文件的末尾。只有
#ifndef
#endif
之间的内容才被保护为多次包含

#ifndef USER_H
#define USER_H

#include <iostream>
using namespace std;

class User
{
private:
    struct Accounts {string user, password, name;} accounts[2];
    void setAccounts();

public:
    int Access(string user, string password);
    bool online;

    User();
    ~User();
};

#endif // USER_H
\ifndef用户
#定义用户
#包括
使用名称空间std;
类用户
{
私人:
结构帐户{string user,password,name;}帐户[2];
作废账户();
公众:
int访问(字符串用户、字符串密码);
布尔在线;
用户();
~User();
};
#endif//USER\u H
编辑


您不应该包括
User.cpp
。只应包含头文件。

#endif//USER_H
移动到文件底部?

您的包含保护不会。。。好。。。没什么。#不过,如果你摆脱了#如果..,布拉格语一次应该没问题#endif Guarda假设“pragma”曾经放在正确的位置:-)仍然不起作用。编译器说User::User()有多个定义。这仍然是一个编译器问题。仍然不工作。编译器说User::User()有多个定义。这仍然是一个编译器问题。@Chisté你是否在某个地方包含了“User.cpp”。另一个noob错误hauyh21ijksajhyghuithx@Chist是的,只应包括头文件。