如何在c ++中使用向量结构中的结构向量?

时间:2017-04-15 00:38:21

标签: c++ c++11

我想创建一个列车矢量,每列火车都需要一对矢量。

如果我在main()之外运行代码,我会收到以下错误:

naive-bayes.cpp:17:15: error: template argument 1 is invalid
    vector<pair> pairs;

naive-bayes.cpp:17:15: error: template argument 2 is invalid

main()内,我收到了以下错误:

naive-bayes.cpp:22:15: error: template argument for 'template<class>
class std::allocator' uses local type 'main()::pair'
    vector<pair> pairs;

naive-bayes.cpp:22:15: error:   trying to instantiate 'template<class> class std::allocator'

naive-bayes.cpp:22:15: error: template argument 2 is invalid

以下是代码:

struct pair {
    int index;
    int value;
};

struct trains {
    string label;
    vector<pair> pairs;
};

2 个答案:

答案 0 :(得分:3)

您的问题可能归因于using namespace std;

标准库中有std::pair类型。

试试这个:

#include <string>
#include <vector>

struct pair {
    int index;
    int value;
};

struct trains {
    std::string label;
    std::vector<pair> pairs;
};

int main()
{
    return 0;
}

答案 1 :(得分:2)

如果没有完整的程序示例,我可以真正指出的是,您的本地pair声明可能会与std::pair混淆。将struct pair的定义更改为struct mypair

相关问题