Undeclared Identifier Issue

2019-08-31 02:55发布

I seem to have run into an odd problem and none of the previously answered Undeclared Identifier questions seem to cover it. Sorry in advance if I overlooked one.

I'm building a class file in C++ and almost finished it before I noticed I didn't include a close bracket at the end of my declarations. But once I add the bracket and semicolon, all my instances of calling two specific variables return an "undeclared identifier" report.

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

Nothing else seems to, and they're all declared in the same area. It's specifically "destinationPort" and "currentAutos" returning the issue, and comparing it to previous code that worked doesn't seem to point out any obvious issue. Is there some tiny thing I'm overlooking, or did I trip and make a mistake somewhere?

Thank you and appologies again if this has already been answered!

标签: c++
1条回答
兄弟一词,经得起流年.
2楼-- · 2019-08-31 03:35

The definition of moveBoat needs to be in the FerryBoat scope, otherwise it is a non-member function, with no access to FerryBoat's members:

void FerryBoat::moveBoat(int nextStop)
{
   ....
}
查看更多
登录 后发表回答