0

I want to create a vector of trains, where each train needs a vector of pairs.

If I run the code outside of main(), I get these errors:

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

Inside main(), I get these errors:

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

Here is the code:

struct pair {
    int index;
    int value;
};

struct trains {
    string label;
    vector<pair> pairs;
};
3
  • 1
    Did you #include <vector>? And are you using namespace std? Commented Apr 15, 2017 at 0:41
  • And, if you have using namespace std, you are asking for trouble if you ever #include <utility> since there is a templated std::pair. Even worse, with some implementations, some standard header files include each other even when the standard doesn't require it. Commented Apr 15, 2017 at 0:47
  • Don't spam tags! C is not C++ is not C! Commented Apr 15, 2017 at 1:08

2 Answers 2

3

You're problem is probably due to using namespace std;.

There is a std::pair type in the standard library.

Try this:

#include <string>
#include <vector>

struct pair {
    int index;
    int value;
};

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

int main()
{
    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

2

Without a full program example to play with, all I can really point out is that your local pair declaration is likely getting confused with std::pair. Change your definition of struct pair to be struct mypair.

3 Comments

struct mypair and mypair will be equivalent in C++.
@Peter - Is that true? Oops! Just tried that out here and you are indeed correct. Thanks for the correction. Will update
yep, it's second solution :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.