体系结构的未定义符号 x86_64 c++ class
Undefined symbols for architecture x86_64 c++ class
尝试将基本的 'Bank Account' class 代码分成一个头文件和两个 .cpp 文件,在尝试编译时显示以下错误消息。
(通过终端在 vim 中开发 (OS X Yosemite 10.10.5))
我不确定错误消息指的是什么,也不知道如何解决问题。在此先感谢您的任何见解和反馈。
终端命令和错误消息:
$ g++ -std=c++11 -Wall Account.cpp
$ g++ -std=c++11 -Wall test.cpp
Account.h
//Account.h
//Account class definition. This file presents Account's public
//interface without revealing the implementation of Account's member
//functions, which are defined in Account.cpp.
#ifndef ACCOUNT_H
#define ACCOUNT_H
#include <iostream>
class Account
{
public:
Account(int amount); //constructor initialize accountBalance
void credit(int creditValue); //credits the account balance
void debit(int debitValue) ; //debits the account balance
int getBalance() const; //gets the account balance
private:
int accountBalance;//account balance for this Account
};//end class Account
#endif
Account.cpp
//Account.cpp
//Account member function definitions. This file contains
//implementations of the member functions prototype in Account.h
#include <iostream>
#include "Account.h" //include definition of class Account
using namespace std;
//constructor initializes accountBalance with int supplied
//as argument
Account::Account(int amount)
{
if(amount >= 0)
{
accountBalance = amount;
}
else
{
accountBalance = 0;
cerr << "The initial balance was invalid" << endl;
}
}
//function to credit amount to account balance
//value must be greater than zero
void Account::credit(int creditValue)
{
if(creditValue > 0)
{
accountBalance += creditValue;
}
else
{
cout << "Credit value cannot be negative nor zero.\n";
}
}
//function to debit amount from account balance
//value cannot exceed current account balance
void Account::debit(int debitValue)
{
if(accountBalance >= debitValue)
{
accountBalance -= debitValue;
}
else
{
cout << "Debit amount exceeds account balance.\n";
}
}
//function to get the account balance
int Account::getBalance() const
{
return accountBalance;
}
test.cpp
#include <iostream>
using namespace std;
// include definition of class Account from Account.h
#include "Account.h"
// function main begins program execution
int main()
{
Account account1( 50 ); // create Account object
Account account2( 25 ); // create Account object
Account account3( -25 ); // attempt to initialize to negative amount;
// display initial balance of each object
cout << "account1 balance: $" << account1.getBalance() << endl;
cout << "account2 balance: $" << account2.getBalance() << endl;
int depositAmount; // stores deposit amount read from user
cout << "\nEnter deposit amount for account1: "; // prompt
cin >> depositAmount; // obtain user input
cout << "\ndeposit " << depositAmount
<< " into account1 balance\n\n";
account1.credit( depositAmount ); // add to account
return 0; // indicate successful termination
} // end main
问题是您在编译每个 .cpp 文件时就好像它是程序中的唯一文件一样。相反,您必须使用 C++ 编译器(假设是 GCC 或 Clang)的 -c
选项来编译每个 .cpp 文件。这将为您提供一组相应的 .o(对象)文件。然后你 link 这些连同最后的命令行是这样的:
g++ -o myprogname Account.o test.o
如果您使用 CMake 等构建工具,这些细节将自动处理。
尝试将基本的 'Bank Account' class 代码分成一个头文件和两个 .cpp 文件,在尝试编译时显示以下错误消息。
(通过终端在 vim 中开发 (OS X Yosemite 10.10.5))
我不确定错误消息指的是什么,也不知道如何解决问题。在此先感谢您的任何见解和反馈。
终端命令和错误消息:
$ g++ -std=c++11 -Wall Account.cpp
$ g++ -std=c++11 -Wall test.cpp
Account.h
//Account.h
//Account class definition. This file presents Account's public
//interface without revealing the implementation of Account's member
//functions, which are defined in Account.cpp.
#ifndef ACCOUNT_H
#define ACCOUNT_H
#include <iostream>
class Account
{
public:
Account(int amount); //constructor initialize accountBalance
void credit(int creditValue); //credits the account balance
void debit(int debitValue) ; //debits the account balance
int getBalance() const; //gets the account balance
private:
int accountBalance;//account balance for this Account
};//end class Account
#endif
Account.cpp
//Account.cpp
//Account member function definitions. This file contains
//implementations of the member functions prototype in Account.h
#include <iostream>
#include "Account.h" //include definition of class Account
using namespace std;
//constructor initializes accountBalance with int supplied
//as argument
Account::Account(int amount)
{
if(amount >= 0)
{
accountBalance = amount;
}
else
{
accountBalance = 0;
cerr << "The initial balance was invalid" << endl;
}
}
//function to credit amount to account balance
//value must be greater than zero
void Account::credit(int creditValue)
{
if(creditValue > 0)
{
accountBalance += creditValue;
}
else
{
cout << "Credit value cannot be negative nor zero.\n";
}
}
//function to debit amount from account balance
//value cannot exceed current account balance
void Account::debit(int debitValue)
{
if(accountBalance >= debitValue)
{
accountBalance -= debitValue;
}
else
{
cout << "Debit amount exceeds account balance.\n";
}
}
//function to get the account balance
int Account::getBalance() const
{
return accountBalance;
}
test.cpp
#include <iostream>
using namespace std;
// include definition of class Account from Account.h
#include "Account.h"
// function main begins program execution
int main()
{
Account account1( 50 ); // create Account object
Account account2( 25 ); // create Account object
Account account3( -25 ); // attempt to initialize to negative amount;
// display initial balance of each object
cout << "account1 balance: $" << account1.getBalance() << endl;
cout << "account2 balance: $" << account2.getBalance() << endl;
int depositAmount; // stores deposit amount read from user
cout << "\nEnter deposit amount for account1: "; // prompt
cin >> depositAmount; // obtain user input
cout << "\ndeposit " << depositAmount
<< " into account1 balance\n\n";
account1.credit( depositAmount ); // add to account
return 0; // indicate successful termination
} // end main
问题是您在编译每个 .cpp 文件时就好像它是程序中的唯一文件一样。相反,您必须使用 C++ 编译器(假设是 GCC 或 Clang)的 -c
选项来编译每个 .cpp 文件。这将为您提供一组相应的 .o(对象)文件。然后你 link 这些连同最后的命令行是这样的:
g++ -o myprogname Account.o test.o
如果您使用 CMake 等构建工具,这些细节将自动处理。