未在此范围中声明的数据“成员” [英] Data "member not declared in this scope"
问题描述
我想创建一个向量来存储对象。我已经添加到类的头文件作为私有数据成员。
I'm trying to create a vector which will store objects. I have added to the header file of the class as a private data member.
我试图将此向量初始化为空(以便我可以在程序中添加对象),但是当我编译这个程序进行测试时,返回错误:
I am trying to initialize this vector as being empty (so that I can add objects to it later on in the program) but when I compile this program to test, this error is returned:
...错误:'_bookingVector'未在此范围内声明|
...error: '_bookingVector' was not declared in this scope|
我认为问题是我的默认构造函数的初始化列表(_bookingVector显然是向量):
I think the problem is with my initialization list on my default constructor(_bookingVector is obviously the vector):
Schedule::Schedule() : _bookingVector()
{ }
$ b b
我的语法错误吗?
Is my syntax wrong? Or are vectors initialized differently?
这是我的代码:
Schedule.h
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
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;
}
}
我非常渴望了解做错了,修复它。
I'm very eager to learn what I'm doing wrong and fix it.
推荐答案
问题不在于构造函数,如果不必要 1 ,看起来很好。问题是你已经定义 AddBooking
和 DisplayDebug
作为非成员函数,但这些应该是成员为了访问该类的其他成员。
The issue is not with the constructor, which looks fine if unnecessary1. The issue is that you have defined AddBooking
and DisplayDebug
as non-member functions, but these should be members in order to access other members of the class.
将定义修改为 Schedule
类的范围,因此: / p>
Modify the definitions to be in the scope of the Schedule
class thus:
void Schedule::AddBooking(int bday, int btime, int btrainer, int bid) { ...
^^^^^^^^^^
void Schedule::DisplayDebug(){ ...
^^^^^^^^^^
此外,不要在头文件中使用命名空间std d进一步说,不要在任何地方说,但没有普遍的协议。)
Also, don't say using namespace std
in a header file (I'd go further and say don't say it anywhere but there isn't universal agreement on that.)
1您的默认构造函数不会执行编译器生成的不会做的任何事情。您可以安全地删除它。
这篇关于未在此范围中声明的数据“成员”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!