我似乎遇到了一个奇怪的问题,之前回答的未申报标识符问题似乎都没有涵盖。如果我忽略了一个,请提前抱歉。
我正在使用C ++构建一个类文件,并且在我注意到我的声明结尾处没有包含一个小括号之前几乎完成了它。但是一旦我添加了括号和分号,我调用两个特定变量的所有实例都会返回一个“未声明的标识符”报告。
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
class FerryBoat
{
//Variables
private:
int numOfPorts;
int startingPort;
int destinationPort;
int maximumAutos;
int currentPort;
int currentAutos;
//Methods
public:
FerryBoat(int, int, int); //build up
void moveBoat(int); //Push the ferry to it's designated station
int getDestinationPort(); //returns destination port
int getRandNum(int); //generates randomized numbers for the program
void loadAutos(int); //Loads cars on the port limited by the number of cars at that port. The port then has those cars subtracted from their total
void unloadAutos(); //Unloads cars on board
int getCurrentAutos(); //Returns the current number of cars on the ferry
int getMaxCapacity(); //returns the max number of autos able to board
int getStartingPort(); //Returns our start position
int getNumPorts(); //returns the number of ports featured
int getCurrentPort(); //returns our current port
};
FerryBoat::FerryBoat(int ports, int capacity, int start)
{
numOfPorts = ports;
startingPort = start;
maximumAutos = capacity;
currentPort = startingPort;
destinationPort = startingPort;
currentAutos = 0;
}
void moveBoat(int nextStop)
{
destinationPort = nextStop;
cout<<"There are currently "<< currentAutos <<" autos loaded on the ferry."<<endl;
cout<<"We are now leaving port "<<currentPort<<" for port "<<destinationPort<<"."<<endl;
cout<<"[Movement in Progress]"<<endl;
currentPort = destinationPort;
cout<<"We have reached port "<<currentPort<<endl;
}
似乎没有别的东西,而且它们都在同一区域宣布。 它特别是“destinationPort”和“currentAutos”返回问题,并将它与以前的代码进行比较,它似乎没有指出任何明显的问题。是否有一些我忽视的微小事物,或者我是否曾在某个地方旅行并犯错?
如果已经回答,请再次感谢您和appologies
答案 0 :(得分:3)
moveBoat
的定义必须在FerryBoat
范围内,否则它是非成员函数,无法访问FerryBoat
的成员:
void FerryBoat::moveBoat(int nextStop)
{
....
}