代码之家  ›  专栏  ›  技术社区  ›  Jake Wilson

C++不能创建矢量

  •  1
  • Jake Wilson  · 技术社区  · 14 年前

    这很奇怪。我在一个类中创建了一个向量,但在另一个类中无法创建它。他是我的代表:

    主.h

    #include <Windows.h>
    #include <ShellAPI.h>
    #include <vector>
    #include <string>
    #include <iostream>
    
    #include "taco.h"
    
    class MyClass
    {
    
    public:
        int someint;
        vector<int> myOrder;
    };
    

    #include <vector>
    
    class OtherClass
    {
    
    public:
        vector<int> otherOrder;
    };
    

    我得到了关于taco.h中向量声明的编译错误:

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

    我错过了什么?我可以取消注释第二个向量声明,它编译得很好。

    4 回复  |  直到 14 年前
        1
  •  12
  •   Community CDub    8 年前

    尝试:

    std::vector<int> otherOrder;
    

    vector 是这个计划的一部分 std 矢量 std:: 前缀。

    using namespace std; using 关键字,因为它会污染 include

    更详细地解释 using namespace ... ,请参见 this thread .

        2
  •  3
  •   Jacob    14 年前

    std::vector<int> . 你应该使用名称空间——我假设你有

    using namespace std;
    

    main.h 找个地方。关于为什么要使用 using 是坏习惯;我建议你去看看。

        3
  •  3
  •   kennytm    14 年前

    所有C++标准库对象都位于 std

    class MyClass
    {
    
    public:
        int someint;
        std::vector<int> myOrder;
    //  ^^^^^
    };
    
        4
  •  1
  •   a1ex07    14 年前
    std::vector<int> ?