c++ - 在头文件中声明 vector 时出错

标签 c++ visual-studio-2008 vector sfml

当我尝试在 header 中声明 vector 时,我不断收到一长串错误。我环顾四周,但找不到解决方案。

错误如下:

1>Compiling... 1>game.cpp 1>c:\users\legacyblade\documents\visual studio 2008\projects\fourswords\fourswords\input.h(38) : error C2143: syntax error : missing ';' before '<' 1>c:\users\legacyblade\documents\visual studio 2008\projects\fourswords\fourswords\input.h(38) : error C2071: 'input::vector' : illegal storage class 1>c:\users\legacyblade\documents\visual studio 2008\projects\fourswords\fourswords\input.h(38) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 1>c:\users\legacyblade\documents\visual studio 2008\projects\fourswords\fourswords\input.h(38) : error C2238: unexpected token(s) preceding ';' 1>main.cpp 1>c:\users\legacyblade\documents\visual studio 2008\projects\fourswords\fourswords\input.h(38) : error C2143: syntax error : missing ';' before '<' 1>c:\users\legacyblade\documents\visual studio 2008\projects\fourswords\fourswords\input.h(38) : error C2071: 'input::vector' : illegal storage class 1>c:\users\legacyblade\documents\visual studio 2008\projects\fourswords\fourswords\input.h(38) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 1>c:\users\legacyblade\documents\visual studio 2008\projects\fourswords\fourswords\input.h(38) : error C2238: unexpected token(s) preceding ';' 1>input.cpp 1>c:\users\legacyblade\documents\visual studio 2008\projects\fourswords\fourswords\input.h(38) : error C2143: syntax error : missing ';' before '<' 1>c:\users\legacyblade\documents\visual studio 2008\projects\fourswords\fourswords\input.h(38) : error C2071: 'input::vector' : illegal storage class 1>c:\users\legacyblade\documents\visual studio 2008\projects\fourswords\fourswords\input.h(38) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 1>c:\users\legacyblade\documents\visual studio 2008\projects\fourswords\fourswords\input.h(38) : error C2238: unexpected token(s) preceding ';'

这是源代码:

#include <vector>
#include <SFML/Graphics.hpp>

#ifndef _input_h
#define _input_h

class input
{
public:
      input();
     void update();

//----input keys----//

    // Directions
    bool upPress;
    bool downPress;
    bool leftPress;
    bool rightPress;

    // Actions
    bool aPress;
    bool bPress;
    bool jumpPress;
    bool shieldPress;

    // Menu
    bool startPress;
    bool screenshotPress;
    bool fullscreenPress;

//------------------//

private:
    extern vector<sf::Keyboard::Key> keyBindings;

};

#endif

无论是否使用 extern,它都会给我同样的错误,即使我更改了 vector 内的事物类型(甚至是 int)也是如此。

非常感谢您的阅读。如果有人能提供帮助,那就太好了。我需要 vector 来做我想做的事。不知道为什么它给我这样的麻烦。同一位置的任何其他类型的变量都不会导致错误。只有 vector 。

最佳答案

补充一下,您需要在声明中包含命名空间,因为我们通常不想使用“using namespace std”来膨胀头文件。因此,如果您看到在其他地方使用的 vector 前面没有 std::,则命名空间可能是在其他地方声明的。

关于c++ - 在头文件中声明 vector 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12063231/

相关文章:

c++ - 类外的类函数模板定义

c++ - 贪心算法练习无法正常工作

c++ - 为什么在 C++ 中使用 shell 链接查找快捷方式的目标路径时它指的是 windows\installer 文件夹

c - 使用 Microsoft Visual Studio '08 编译 libpng 时如何避免 "error LNK2005:"(已定义的 stdlib 函数)?

c# - 需要为 C++/CLI 创建什么类型的项目?

c++ - 静态库是否避免了名称重整问题?

c++ - 成员函数无法访问私有(private)变量未在范围类中声明

c++ - 在制作 map 的情况下是否值得使用 vector

algorithm - 如何找到矢量路径的方向

python - 值错误 : operands could not be broadcast together with shapes (7, ) (6,) (7,)