简体   繁体   English

体系结构x86_64 C ++类的未定义符号

[英]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. 在尝试对基本的“银行帐户”类进行编码(分为头文件和两个.cpp文件)时,尝试编译时会显示以下错误消息。

(Developing in vim through the terminal (OS X Yosemite 10.10.5)) (通过终端(OS X Yosemite 10.10.5)在vim中开发)

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.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.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 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. 问题是您正在编译每个.cpp文件,就好像它是程序中唯一的文件一样。 Instead, you must use the -c option to your C++ compiler (assuming GCC or Clang) to compile each .cpp file. 相反,您必须对C ++编译器使用-c选项(假设使用GCC或Clang)来编译每个.cpp文件。 This will give you a corresponding set of .o (object) files. 这将为您提供一组对应的.o(目标)文件。 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. 如果您使用CMake之类的构建工具,这些详细信息将自动处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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