【问题标题】:'vector' was not declared in this scope'vector' 未在此范围内声明
【发布时间】: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-&gt;video_ptrs.push_back(temp_here);之类的东西?
  • 您已在视频对象本身内放置了一个视频列表。这意味着每个视频都会有一个视频列表。如果这不是您想要的,您应该将列表移到课堂之外或将其设为static member,以便所有视频只有一个列表。此外,您应该始终确保您的程序运行的 deletes 与运行 news 的数量一样多,或者查找“智能指针”来为您处理 deletes。
  • 感谢大家的帮助。抱歉这个菜鸟问题。

标签: c++ stdvector


【解决方案1】:

video_ptrsVideo的成员,用你刚刚创建的对象调用它:

Video* temp_here = new Video(title, url, comment, length, rating);
temp_here->video_ptrs.push_back(temp_here);

虽然这会将指针 temp_here 添加到同一对象的 vector,但我不确定这是否是有意的。

【讨论】:

    【解决方案2】:

    video_ptrs 确实没有在 main.js 中声明。你应该使用

    temp_here->video_ptrs...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-20
      • 2016-08-09
      • 2019-02-17
      • 2021-01-07
      • 2016-10-27
      相关资源
      最近更新 更多