c++ - 在c++中用vector编译错误

标签 c++ compiler-construction

我卡住了!我有这个非常简单的测试代码,但我无法编译它!我以前多次使用相同的代码,但现在它不起作用!
我有这个简单的程序

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

然后是 rswap.cpp...

#include <vector>
#include "Rswap.h"
Rswap::Rswap(){
    V.push_back(9);
};

然后是 rswap.h...

#ifndef Rswap_h
#define Rswap_h


class Rswap{
public:
  vector<int>V;
  Rswap();
};
  #endif

我正在使用 Visual studio 2008。是否有明显的错误而我遗漏了,或者可能是什么!正如我所说,我已经在几个不同的场合使用过这个片段,但我找不到它和那些有效的片段之间的任何区别...... 我都试过了 vector < int > V ;和 vector <int> V;没有任何运气

我已经盲目地盯着这个看了一段时间,所以我觉得最好在这里问一下!

rswap.h(7) : error C2143: syntax error : missing ';' before '<'
    rswap.h(7) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    rswap.h(7) : error C2238: unexpected token(s) preceding ';'

最佳答案

此时你#include "Rswap.h" , 你还没有声明 using namespace std;然而,所以引用vector在 Rswap.h 中必须使用 namespace 进行限定。只需使用 namespace 前缀声明 vector 。

class Rswap{
public:
  std::vector<int>V;
  Rswap();
};

另外,我建议你#include <vector>从 Rswap.h 而不是依赖使用 Rswap.h 的人来获取 #include正确订购。

关于c++ - 在c++中用vector编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3334285/

相关文章:

c++ - dll依赖版本冲突

compiler-construction - 在创建 DFA 后实现识别 token 的词法分析器

java - "Error during linking"表示用 Ocaml 编写的 Java 编译器

c# - 是否可以在 C# 中为 Android 编写应用程序?

c++ - 未使用的功能会被优化吗?

sql - 正在寻找编译器,有什么想法吗?

c++ - 使用合并的#includes 转储 C++ 源代码

c++ - 如何在C++中访问类中的私有(private)数组

c++ - C/C++ 中 3D 平面的首选表示

c++ - 具有类型转换的多态复制构造函数