数据"member not declared in this scope"

Data "member not declared in this scope"

我正在尝试创建一个将存储对象的向量。我已将作为私有数据成员添加到 class 的头文件中。

我试图将此向量初始化为空(以便稍后在程序中向其添加对象)但是当我编译此程序进行测试时,返回此错误:

...error: '_bookingVector' was not declared in this scope|

我认为问题出在我的默认构造函数上的初始化列表(_bookingVector 显然是向量):

Schedule::Schedule() : _bookingVector()
{ }

我的语法有误吗?或者向量的初始化方式不同?

这是我的代码:

Schedule.h

#ifndef SCHEDULE_H
#define SCHEDULE_H
#include "Booking.h"
#include <vector>    
using namespace std;


class Schedule
{    
    public:
        Schedule();
        void AddBooking(int bday, int btime, int btrainer, int bid);
        void RemoveBooking(int bday, int btime);
        void DisplaySchedule();
        void DisplayAvailableTimeSlots();    
        //For Testing
        void DisplayDebug();

    private:
        vector<Booking> _bookingVector;   

};    
#endif // SCHEDULE_H

Schedule.cpp

#include "Schedule.h"
#include "Booking.h"
#include <vector>
#include <iostream> 

Schedule::Schedule() : _bookingVector()
{ }    

void AddBooking(int bday, int btime, int btrainer, int bid){    
    Booking bookingObject(bday, btime, btrainer, bid);
    _bookingVector.push_back(bookingObject);    

}


void DisplayDebug(){

    for(int i = 0; i < _bookingVector.size(); ++i){    
        cout << _bookingVecotr[i] << endl;    
    }    
}

我非常渴望知道我做错了什么并改正它。

问题不在于构造函数,如果不需要的话看起来没问题1。问题是您已将 AddBookingDisplayDebug 定义为非成员函数,但这些应该是成员才能访问 class.

的其他成员

将定义修改为 Schedule class 的范围,因此:

void Schedule::AddBooking(int bday, int btime, int btrainer, int bid) { ...
     ^^^^^^^^^^

void Schedule::DisplayDebug(){ ...
     ^^^^^^^^^^

此外,不要在头文件中说 using namespace std(我会更进一步说不要在任何地方说,但对此没有普遍同意。)


1 您的默认构造函数不会执行编译器生成的构造函数不会执行的任何操作。您可以安全地删除它。