C++ LNK2005 LNK1169错误c和x2B+;银行账户代码

C++ LNK2005 LNK1169错误c和x2B+;银行账户代码,c++,class,object,C++,Class,Object,我正在尝试建立一个银行帐户代码,但还没有完成。当我尝试第一部分时,我会将几个帐户写入一个银行帐户文本文件。然后读取该文本文件并创建帐户对象。这正是我的老师想要的,首先我不能改变你应该知道的方法。当我试图写入和读取文件并设置对象的参数时,我遇到了两个我不理解的错误。我检查了其他问题,但我的代码知识太少。我不能阅读别人的代码,所以我不能通过阅读来解决我的代码问题,这对我来说太复杂了。我的代码: 银行账户 #pragma once #ifndef BankAccount_h #define BankA

我正在尝试建立一个银行帐户代码,但还没有完成。当我尝试第一部分时,我会将几个帐户写入一个银行帐户文本文件。然后读取该文本文件并创建帐户对象。这正是我的老师想要的,首先我不能改变你应该知道的方法。当我试图写入和读取文件并设置对象的参数时,我遇到了两个我不理解的错误。我检查了其他问题,但我的代码知识太少。我不能阅读别人的代码,所以我不能通过阅读来解决我的代码问题,这对我来说太复杂了。我的代码:

银行账户

#pragma once
#ifndef BankAccount_h
#define BankAccount_h


#include <string>

using namespace std; 

class BankAccount
{
public:
    int static countobj;
    int userID;
    string userName;
    string userSurname;
    int balance;
    BankAccount(int, string, string, int);
    BankAccount()
    {

    }
    void openFile();
    void openAccount();
    void readAccount();
    void setParameter(int, string, string, int);
    void displayAccount();
    int depositMoney();
    int withdrawMoney();
    void displayFullAccount();
}Account[5];

#endif
#pragma一次
#ifndef银行账户
#定义银行账户
#包括
使用名称空间std;
类别银行帐户
{
公众:
int静态countobj;
int用户标识;
字符串用户名;
字符串用户名;
国际收支平衡;
银行账户(int,string,string,int);
银行帐户()
{
}
void openFile();
作废openAccount();
作废readAccount();
void setParameter(int,string,string,int);
作废显示帐户();
int存款货币();
int取款();
void displayFullAccount();
}账户[5];
#恩迪夫
BankAccount.cpp

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

int BankAccount::countobj = 0;

void BankAccount::openFile()
{
    string line;
    ofstream bankaccount;
    bankaccount.open("BankAccount.txt");
    for (int i = 0; i < 5; i++)
    {
        getline(cin, line);
        bankaccount << line << endl;
    }
    bankaccount.close();
}

void BankAccount::readAccount()
{
    ifstream read("BankAccount.txt");
    int i = 0;
    while (read >> userID >> userName >> userSurname >> balance)
    {
        Account[i].userID = userID;
        Account[i].userName = userName;
        Account[i].userSurname = userSurname;
        Account[i].balance = balance;
    }
    countobj = i;
    read.close();
}

void BankAccount::setParameter(int id, string name, string sname, int b)
{
    id = userID;
    name = userName;
    sname = userSurname;
    b = balance;
}

void BankAccount::displayFullAccount()
{
    for (int i = 0; i < countobj; i++)
    {
        cout << Account[i].userID << " " << Account[i].userName << " " << Account[i].userSurname << " " << Account[i].balance << endl;
    }
}
#包括“BankAccount.h”
#包括
#包括
使用名称空间std;
int BankAccount::countobj=0;
作废银行帐户::openFile()
{
弦线;
流动银行账户;
bankaccount.open(“bankaccount.txt”);
对于(int i=0;i<5;i++)
{
getline(cin,line);
银行账户用户ID>>用户名>>用户姓氏>>余额)
{
帐户[i]。用户ID=userID;
帐户[i]。用户名=用户名;
账户[i]。用户姓氏=用户姓氏;
账户[i]。余额=余额;
}
countobj=i;
read.close();
}
void BankAccount::setParameter(整数id、字符串名称、字符串sname、整数b)
{
id=用户id;
名称=用户名;
sname=用户姓氏;
b=平衡;
}
作废银行帐户::displayFullAccount()
{
对于(int i=0;i除了
BankAccount.cpp
,错误消息是否表明您还在另一个未显示的源文件中定义了
BankAccount::countobj
。链接器错误消息会提到该文件的名称,但您也忽略了该部分消息。我猜它说它在source.cpp中,而不是。如果我d猜测:您正在编译的实际代码与显示的代码不同。其中,
int BankAccount::countobj=0;
line(或类似代码)在头文件中,因此它最终包含在多个源文件中。实际上,我预计会出现关于
Account
变量的链接器错误。该变量肯定在头文件中定义,因此最终会有多个定义,包括该头文件的每个源文件中都有一个定义。
#include <iostream>
#include <string>
#include <fstream>
#include "BankAccount.h"
using namespace std;

int main()
{
    BankAccount acc;
    acc.openFile();
    acc.readAccount();
    acc.displayFullAccount();
}