gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:31:16 24 4
gpt4 key购买 nike

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

#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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com