【发布时间】:2016-06-03 01:06:41
【问题描述】:
我很抱歉提出一个应该有一个简单解决方案的问题,但这让我发疯了。我检查了所有常见错误:命名空间标准、拼写、包含向量等。下面是我的 video.h 文件的缩写代码。
#include <iostream>
#include <string>
#include <new>
#include <vector>
using namespace std;
class Video
{
public:
Video(string, string, string, float, int);
vector<Video*> video_ptrs;
void print();
};
这是我的 main.cpp 的代码
#include "video.h"
using namespace std;
int main()
{
...
Video* temp_here = new Video(title, url, comment, length, rating);
video_ptrs.push_back(temp_here);
return 0;
}
返回的错误是“'video_ptrs' 未在此范围内声明。”提前感谢您提供的任何帮助。
【问题讨论】:
-
你的意思是写
temp_here->video_ptrs.push_back(temp_here);之类的东西? -
您已在视频对象本身内放置了一个视频列表。这意味着每个视频都会有一个视频列表。如果这不是您想要的,您应该将列表移到课堂之外或将其设为static member,以便所有视频只有一个列表。此外,您应该始终确保您的程序运行的
deletes 与运行news 的数量一样多,或者查找“智能指针”来为您处理deletes。 -
感谢大家的帮助。抱歉这个菜鸟问题。