简体   繁体   中英

Undefined symbols for architecture x86_64 c++ class

In an attempt to code a basic 'Bank Account' class separated into a header file, and two .cpp files the following error messages are displayed when attempting to compile.

(Developing in vim through the terminal (OS X Yosemite 10.10.5))

I am not sure what the error messages are referring to nor how to solve the issue. Thanks in advance for any insight and feedback.

Terminal Command & Error Messages:

$ 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

The problem is that you're compiling each .cpp file as if it were the only file in the program. Instead, you must use the -c option to your C++ compiler (assuming GCC or Clang) to compile each .cpp file. This will give you a corresponding set of .o (object) files. You then link these together with a final command line like this:

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

If you use a build tool like CMake, these details will be handled automatically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM