我正在学习C ++,在ATOM编辑器中编译和运行代码时收到错误。
这是以下错误
"Undefined symbols for architecture x86_64:
"SavingsAccount::SavingsAccount(double, double)", referenced from:
_main in main-09683f.o
"CheckingAccount::debit(double)", referenced from:
_main in main-09683f.o
"CheckingAccount::credit(double)", referenced from:
_main in main-09683f.o
"CheckingAccount::CheckingAccount(double, double)", referenced from:
_main in main-09683f.o
"Account::debit(double)", referenced from:
_main in main-09683f.o
"Account::credit(double)", referenced from:
_main in main-09683f.o
"Account::Account(double)", referenced from:
_main in main-09683f.o
"SavingsAccount::calculateInterest() const", referenced from:
_main in main-09683f.o
"Account::getBalance() const", referenced from:
_main in main-09683f.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)"
我正在使用macbook pro请有人帮忙,我认为我的代码是正确的,因为我多次检查过它。 共有7个文件,当我编译并运行main.cpp
时,我收到此消息感谢任何帮助。
account.h
#ifndef ACCOUNT_H
#define ACCOUNT_H
#include <iostream>
using namespace std;
class Account{ //base class account
public:
Account(double); //constuctor
void credit(const double); //adds amount to the current balance
bool debit( const double); //withdraw money from the account balance
double getBalance() const; //one data member of type double/returns the balance
private:
double balance;
};
#endif
account.cpp
#include <iostream>
#include "Account.h"
using namespace std;
Account:: Account(double initialBalance){
if (initialBalance >=0.0) //if parameter balance is greater than 0 set it balance
balance= initialBalance;
else
{
cout <<"Error initial Balance invalid"; //else say error initial balance invalid
balance=0.0 //and set the balance to 0.0
}
}
void Account:: credit(const double money)
{
balance+=money; //adds amount to balance
}
bool Account::debit( const double money) //if balance - debit is greater than 0 do it and return true
{
if (balance - money >=0.0)
{ balance -= money;
return true;
}
else //else return false and print insufficent funds available
{
cout<<"Insuffient Funds available";
return false;
}
}
double Account::getBalance() const //retuns the current balance
{
return balance;
}
SavingsAccount.h
#ifndef SAVINGS_ACCOUNT_H
#define SAVINGS_ACCOUNT_H
#include "Account.h"
#include <iostream>
using namespace std;
class SavingsAccount : public Account { //inherite parent class Account
public:
SavingsAccount(const double, const double); //SavingsAccount constructor
double calculateInterest() const; //calculate the interest
private:
double interestRate; //include a data member of type double indicating the interest rate
};
#endif
SavingsAccount.cpp
#include <iostream>
#include "Account.h"
#include "SavingsAccount.h"
using namespace std;
SavingsAccount:: SavingsAccount(double initialBalance, double interest_Rate) : Account(initialBalance) //calls account constructot and then sets interest rate
{
interestRate = interest_Rate;
}
double SavingsAccount::calculateInterest() const //Member function calculateInterest that returns a double indicating the amount of interest earned by an account
{
double bal;
bal= Account::getBalance(); //Calls the account class getBalance and sets it to bal and then return balance times the interest rate
return (bal*interestRate);
}
CheckingAccount.h
#ifndef CHECKING_ACCOUNT_H
#define CHECKING_ACCOUNT_H
#include <iostream>
using namespace std;
class CheckingAccount: public Account //inherit Parent class Account
{
public:
CheckingAccount(const double, const double); //checkingaccount contrucotr
bool debit(const double); //redefins member function debit from account class
void credit(const double); //edefins member function credit from account class
private:
double fee; //data member of type double that represents the fee charged per transaction.
};
#endif
CheckingAccount.cpp
#include <iostream>
#include "Account.h"
#include "CheckingAccount.h"
CheckingAccount:: CheckingAccount(double initialBalance, double feeInput): Account(initialBalance) //calls account constructor and sets fee to the class parameter
{
fee = feeInput;
}
void CheckingAccount :: credit( const double money) //redifine
{
Account::credit(money); //adds money to the balance
Account::debit(fee); //subrects the fee from balcance
}
bool CheckingAccount::debit(const double money)
{
if(Account::debit(money+fee)) //call the account debit function of money +fee. It will either subtract or see if there is enough funds
{
return true;
}
else
{
return false; //else return false
}
}
的main.cpp
#include <iostream>
#include <iostream>
#include "Account.h"
#include "CheckingAccount.h"
#include "SavingsAccount.h"
using namespace std;
int main()
{
cout<<"Testing Account Class: Inital balance $100"<<endl;
Account account1(100); //account constructor is 100
cout <<"Crediting $35.95"<<endl; //adding 35.95 dolars
account1.credit(35.95);
cout << "New Balance is $"<< account1.getBalance()<<endl; //printing it out
cout<< "Debiting $200\n"; //trying to take 200 but it is tooo much so cannot
account1.debit(200);
cout<<"\nDebiting $66.45\n"; //takes 66.45 so it works
account1.debit(66.45);
cout << "New Balance is $"<< account1.getBalance()<<endl; //printing it out
cout<<"\nConstructor with insufficent funds"<<endl; //constucor where balnce is trying to be negative
Account account4(-5);
cout<<endl<<"New Balance is $"<<account4.getBalance()<<endl; //printing it out
cout<<"\n\n\nTesting Savings Account Class: Inital balance $100 and Interest Rate 10%"<<endl;
SavingsAccount account2(100, .1); //savings account constuctor $100 and .1 interest rate
cout<<"Interest is $" <<account2.calculateInterest(); //calculate interes
cout<<"\nAdding interest";
account2.credit(account2.calculateInterest()); //adds interest
cout << "\nNew Balance is $"<< account2.getBalance()<<endl; //printing it out
cout<< "Debiting $200\n"; //trying to take 200 but it is tooo much so cannot
account2.debit(200);
cout<<"\nDebiting $66.45\n"; //takes 66.45 so it works
account2.debit(66.45);
cout << "New Balance is $"<< account2.getBalance()<<endl;//printing it out
cout<<"\n\n\nTesting Checking Account Class: Inital balance $100 and fee $5"<<endl;
CheckingAccount account3(100, 5); //checking account constuctor with 100 and 5 dollar free
cout <<"Crediting $35.95"<<endl; //adds 35.95 with the fee
account3.credit(35.95);
cout << "New Balance is $"<< account3.getBalance()<<endl; //printing it out
cout<< "Debiting $200\n"; //trying to take 200 but it is tooo much so cannot
account3.debit(200);
cout<<"\nDebiting $66.45\n"; //takes 66.45 so it works with a 5 dollar fee
account3.debit(66.45);
cout << "New Balance is $"<< account3.getBalance()<<endl; //printing it out
return 0;
}