簡體   English   中英

體系結構x86_64 C ++類的未定義符號

[英]Undefined symbols for architecture x86_64 c++ class

在嘗試對基本的“銀行帳戶”類進行編碼(分為頭文件和兩個.cpp文件)時,嘗試編譯時會顯示以下錯誤消息。

(通過終端(OS X Yosemite 10.10.5)在vim中開發)

我不確定錯誤消息指的是什么,也不知道如何解決該問題。 在此先感謝您的見解和反饋。

終端命令和錯誤消息:

$ g++ -std=c++11 -Wall Account.cpp

錯誤訊息01

$ g++ -std=c++11 -Wall test.cpp

錯誤消息02

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 ++編譯器使用-c選項(假設使用GCC或Clang)來編譯每個.cpp文件。 這將為您提供一組對應的.o(目標)文件。 然后,將它們與最終的命令行鏈接在一起,如下所示:

g++ -o myprogname Account.o test.o

如果您使用CMake之類的構建工具,這些詳細信息將自動處理。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM