1
votes

So I tried to make a vector which elements are pairs of a struct pointer and an int, and I'm beginning to think that this may be impossible..

Suppose I have the following struct:

struct node{
  string str;
  int size;
  node *child[3];
  node(string str1):str(str1){ size = 0;}
};

and a vector:

vector< pair<node*,int> > nodvector;

Then I tried to make a new node and put it in the vector, and apparently it's not working:

int main(){
   node* nod1 = new node("HELLO");
   node* nod2 = new node("WORLD");
   nodevector.push_back(pair<nod1,3>);
   delete nod1;
   delete nod2;
}

The compiler barks at me saying: nod1 cannot appear in a constant-expression error: template argument 1 is invalid error: type/value mismatch at argument 2 in template parameter list for template struct std::pair

Is this just something impossible? What could be an alternate solution to this? Your input will be so much appreciated!

7
You have to read some tutorial of c++. - Mihran Hovsepyan
try not to store bare pointers in a container. It is hard to do correct memory management this way. - kennytm
when creating the pair to add it in the vector, you should give back the template arguments like this pair<node*,int>(nod1,3) Usually, you do a typedef of it - gastush
Besides your the problem you asked about, and all the other (pointer related) problems that were raised in the answers your constructor should take a const string& instead of a mutable string. (In the general case this will save a copy, and more importantly it is more correct, robust and easier to reasohn about) - Fabio Fracassi

7 Answers

8
votes

When you create new pairs, use make_pair instead.

So this works: nodevector.push_back(make_pair(nod1, 3));

3
votes

nodevector.push_back(pair<node*,int>(nod1, 3));

1
votes

The problem is you are using nod1 and 3 as the template parameters to pair in:

nodevector.push_back(pair<nod1,3>);

Instead, try:

nodevector.push_back(pair<node, int>(nod1,3));

Or better yet:

nodevector.push_back(make_pair(nod1,3));
1
votes

All the above answers are correct.

Your code is trying to push a "type" into a vector. That is why the compiler is angry:).

You are doing something like

int i = 10;
vector<int> v;
v.push_back(int);

Obviously, you want

v.push_back(i);

Hope that helps.

0
votes

Create an object of pair. You haven't created it yet.

Do you think "pair" is right syntax ?

Correct it.

0
votes

Try something like this you are confusion you template argument for the accrual class to be operated on.

vector< pair<node*,int> > nodvector;

node* nod1 = new node("HELLO"); node* nod2 = new node("WORLD"); nodvector.push_back(pair(nod1,4)); delete nod1; delete nod2; }

0
votes

As of C++11, a much cleaner alternative to nodevector.push_back(pair<node*,int>(nod1, 3)); or even nodevector.push_back(make_pair(nod1, 3)); is using emplace_back. std::vector<T>::emplace_back will take the same arguments as T's constructor(s), so you can enter the parameters directly:

nodevector.emplace_back(nod1, 3);

If you still feel the need to use push_back, as of C++17 class template arguments can be deduced, so you don't have to use make_pair:

nodevector.push_back(std::pair{nod1, 3});