2

I have seen this error a lot, and there're a lot of questions about it here, but now I really don't know what to do.

User.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();
};

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

I've used even #pragma once and it still not recognizing the class. What do I do?

@edit: I moved down #endif // USER_H and now the class has been recognized, but the constructor method still missed.

"error: multiple definitions of 'User::User()'"

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
Chisté
  • 39
  • 6

2 Answers2

1

You should place #endif // USER_H at the end of the header file. Only the content between #ifndef and #endif is protected to be included multiple times.

#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

EDIT

You should not include User.cpp. Only header file should be included.

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
0

Move #endif // USER_H to the bottom of the file?

Gregory McIntyre
  • 2,051
  • 1
  • 12
  • 7
  • Still not working. The compiler says that there're multiple definitions of User::User(). It still being a compiler problem. – Chisté Sep 02 '16 at 00:47