繁体   English   中英

未在此范围内声明“变量”

[英]“Variable” was not declared in this scope

当我尝试编译我的类时,出现此错误。 我确保有函数原型和变量已正确初始化,希望有人可以帮助我解决问题

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;
}

我有点迷失在什么问题上

这个:

int getTransID()        { return transID;  }

与您的课程无关,这是一个全局函数。

你的意思是:

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

同样,应将该函数(和其他获取器)设置为const以表示它们未修改该对象。

暂无
暂无

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

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