简体   繁体   中英

'class' type redefinition rejection

Having some issues with my two classes and having issues with my account files. I'm not fully sure how to go about fixing it with the classes I think being defined twice. I looked through other suggestions on how to fix it and I'm not exactly sure where to go with it. All the error says is 'Account:' 'class' type redefinitions

Account.h

#include <string>
class Account
{
private:
    unsigned int accountNumber;
    std::string firstName, lastName;
    double balance;
    static unsigned int count;

public:
    Account();
    void setFirstName(std::string fName);
    void setLastName(std::string lName);
    void setBalance(double inBalance);
    void display();
};

this would be the main file

Account.cpp

#include "Account.h"
#include <iostream>
using namespace std;

//Initializes the static counter set equal to 0
unsigned int Account::count = 0;

//Sets up a default constructor
Account::Account()
{
    count++;
    //Set accountNumber to count
    accountNumber = count;
}

//Sets up the first name
void Account::setFirstName(std::string fName)
{
    firstName = fName;
}

//Sets up the last name
void Account::setLastName(std::string lName)
{
    lastName = lName;
}

//Sets up the balance
void Account::setBalance(double inBalance)
{
    balance = inBalance;
}

//Displays the details of the Account
void Account::display()
{
    cout << fixed;
    cout.precision(2);
    cout << "Balance: " << balance << endl;
    cout << "Account: " << accountNumber << endl;
    cout << "First Name: " << firstName << endl;
    cout << "Last Name: " << lastName << endl;
}

You need include guards in your header file

#ifndef ACCOUNT_H_
#define ACCOUNT_H_
#include <string>
class Account
{
private:
    unsigned int accountNumber;
    std::string firstName, lastName;
    double balance;
    static unsigned int count;

public:
    Account();
    void setFirstName(std::string fName);
    void setLastName(std::string lName);
    void setBalance(double inBalance);
    void display();
};
#endif

this prevents the header being included twice in one file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM