myvector没有命名类型

时间:2012-06-09 21:24:25

标签: c++ vector

的main.cpp

#include <vector>
#include <iostream>
#include "normal.h"
using namespace std;
int main()
{
  return 0;
}

normal.h

#ifndef NORMAL_H
     #define NORMAL_H
#include <vector>
#include <iostream>
using namespace std;
vector < int > myvector;
myvector.push_back(12);//does not name a type
#endif

我知道我需要在vector<int> myvector中以某种方式包含main.cpp,但无法想办法。我查看过以前的程序,不需要在main.cpp中包含任何内容。

1 个答案:

答案 0 :(得分:2)

问题在于代码 myvector.push_back(12);不在任何函数内。在函数之外,您只能声明(并可能初始化)变量,您不能放置其他代码。

因此,即使你可以在.h文件中声明你的向量(可能在许多文件中都有它),你应该将这一行移到main()或其他一些函数中。