2016-11-10 114 views
0

我在尝试编译我的类时遇到此错误。我已经确定有函数原型和变量正确initilized,希望有人可以帮我找出问题“变量”未在此范围内声明

g++ -c main.cc 
g++ -c BankControl.cc 
g++ -c Bank.cc 
g++ -c Account.cc 
g++ -c View.cc 
g++ -c AcctList.cc 
g++ -c Customer.cc 
g++ -c CustArray.cc 
g++ -c Transaction.cc 
Transaction.cc: In function ‘int getTransID()’: 
Transaction.cc:18:34: error: ‘transID’ was not declared in this scope 
int getTransID()  { return transID; } 
           ^
Transaction.cc: In function ‘TransType getTType()’: 
Transaction.cc:19:34: error: ‘tType’ was not declared in this scope 
TransType getTType() { return tType; } 
           ^
Transaction.cc: In function ‘TransState getTState()’: 
Transaction.cc:20:34: error: ‘tState’ was not declared in this scope 
TransState getTState() { return tState; } 
           ^
Transaction.cc: In function ‘std::__cxx11::string getDate()’: 
Transaction.cc:21:34: error: ‘date’ was not declared in this scope 
string getDate()  { return date;  } 
           ^
Transaction.cc: In function ‘int getTAcctNum()’: 
Transaction.cc:22:34: error: ‘tAcctNum’ was not declared in this scope 
int getTAcctNum()  { return tAcctNum; } 
           ^
Transaction.cc: In function ‘float getTAmount()’: 
Transaction.cc:23:34: error: ‘tAmount’ was not declared in this scope 
float getTAmount()  { return tAmount; } 
           ^
Transaction.cc: In function ‘void setDate(std::__cxx11::string)’: 
Transaction.cc:28:3: error: ‘date’ was not declared in this scope 
    date = d; 
^
Makefile:31: recipe for target 'Transaction.o' failed 
make: *** [Transaction.o] Error 1 

这里是我的头文件

#ifndef TRANSACTION_H 
    #define TRANSACTION_H 

    #include <string> 
    using namespace std; 

    #include "defs.h" 

    class Transaction 
    { 
     public: 
     Transaction(TransType = TTERROR, TransState = TSERROR,int = 0 ,float = 0); 
     int   getTransID(); 
     TransType getTType(); 
     TransState getTState(); 
     string  getDate(); 
     int   getTAcctNum(); 
     void  setDate(string); 
     float  getAmount(); 
     private: 
     static int nextTransID; 
     int   transID; 
     TransType tType; 
     TransState tState; 
     string  date; 
     int   tAcctNum; 
     float  tAmount; 


    }; 

    #endif 

这里是我的源文件

#include "Transaction.h" 
#include "defs.h" 

#include <string> 
using namespace std; 

int Transaction::nextTransID = 2001; 

Transaction::Transaction(TransType t, TransState s, int acct, float amount) 
{ 
    transID = nextTransID++; 
    tType  = t; 
    tState = s; 
    tAcctNum = acct; 
    tAmount = amount; 
} 

int getTransID()  { return transID; } 
TransType getTType() { return tType; } 
TransState getTState() { return tState; } 
string getDate()  { return date;  } 
int getTAcctNum()  { return tAcctNum; } 
float getTAmount()  { return tAmount; } 


void setDate(string d) 
{ 
    date = d; 
} 

我有点遗憾是什么问题

+0

在你的源文件中,函数是全局函数,而不是'Transaction'类的成员函数。 –

回答

6

这个:

int getTransID()  { return transID; } 

与你的课无关,它是一个全局函数。

您的意思是:

int Transaction::getTransID() { return transID; } 

此外,该功能(和其他干将)应作出const表明它们不修改的对象。

+0

谢谢,我真的很抱歉。我没有注意到这一点。我没有注意到所有功能的遗憾。 – AyeJay