大家好,我是编程新手,请原谅noob问题。。。我尝试了各种方法来处理未定义的引用错误,但它一直在向我抛出错误我试着使用指针"->"one_answers"::"符号以及点"。"我该怎么办?为什么它不能编译?
这是我的cpp文件:
#include <cstdlib>
#include "account.hpp"
using namespace std;
int Account::getAccountNumber()
{
return accountNumber;
}
double Account::getBalance()
{
return balance;
}
void Account::createAccount(LinkedList<Account>& accountsList, string name, int idNumber)
{
...
case 1:
accountsList.addFront(newAcc); //Where the error occurs.
break;
case 2:
do
{
cout << "ntWhich position would you like to insert then"
<< "tnew account into?n"
<< "tPosition number: #";
cin >> target;
if (cin.fail())
{
cin.clear();
cin.ignore(20,'n');
cout << "nntSorry, wrong input. Please enter a correct position.nn";
system("pause");
}
}
while(cin.fail());
accountsList.addMiddle(newAcc, target); //and here
break;
case 3:
accountsList.addEnd(newAcc); //and here
break;
}
cout << "nntAccount Created Successfullynn"
<< accountsList;
system("pause");
}
这是我的.hpp
#ifndef ACCOUNT_HPP_INCLUDED
#define ACCOUNT_HPP_INCLUDED
#include "linkedlist.hpp"
#include "generic.hpp"
class Account : public GenericAccount
{
int accountNumber;
double balance;
public:
Account(string name = "empty", int idNumber = 0, int accountNumber = 0, double balance = 0)
: GenericAccount(name, idNumber), accountNumber(accountNumber), balance(balance) {}
int getAccountNumber();
double getBalance();
void createAccount(LinkedList<Account>&, string, int);
void deposit(LinkedList<Account>&, Account&);
void withdraw(LinkedList<Account>&, Account&);
void displayAccount(LinkedList<Account>&, Account&);
void deleteAccount(LinkedList<Account>&);
friend istream& operator>> (istream& is, Account& x)
{
is >> x.accountNumber;
return is;
}
friend ostream& operator << (ostream& os, Account& c)
{
os << "Account Number= " << c.getAccountNumber() << "t"
<< "Balance= "<< c.getBalance() << endl;
return os;
}
friend bool operator == (Account& a, Account& target)
{
return (a.getAccountNumber() == target.getAccountNumber());
}
};
#endif // ACCOUNT_HPP_INCLUDED
完整的项目可以在这里下载以供参考提前感谢大家!
我认为问题是没有为帐户类型(实际上是任何类型)定义addFront方法。请参阅为什么模板只能在头文件中实现?以获得更好的解释。
将cpp的内容以内联方式移动到.h文件中就可以了。另一个选项是将.cpp文件重命名为.inl,并将其包含在linkedList.hpp 的底部