繁体   English   中英

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

[英]How to use a vector of structs in a vector structs in c++?

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

如果我在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;
};

你的问题可能是由于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;
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM