1

What I am trying to achieve is create a template of a struct myVector which would contain two vectors i.e. vector_a and vector_b. But i am quite new to templates. I mean, I know why and when one would prefer using Templates in certain situations but I am not quite sure how to tackle this problem. What I have written is:

'''

#include<iostream>
#include<stddef.h>
#include<vector>


template <typename T> struct myVector {

    std::vector<T> vector_a;
    std::vector<T> vector_b;

};


int main() {

    myVector<int> z1(5);
    myVector<int> z2(6);
}

''' I end up getting errors like no matching function for call to ‘VectorXY::VectorXY(int)’ for the vector VectorXY z2(6);

Therefore, I would really appreciate your help. Thanks in advance.

y_1234
  • 61
  • 1
  • 7
  • There is no `VectorXY` in your shown code, but it is referenced in the error message. Please provide an actual [repro] and the error message that exact example generates. – walnut Mar 28 '20 at 20:43
  • That aside, the error message is pretty clear. What constructor of `myVector` do you expect to be used for `myVector z1(5);`? You haven't declared any constructor. It has nothing to do with templates, but just with how classes work. – walnut Mar 28 '20 at 20:44

1 Answers1

1

Your error has nothing to do with templates. Consider the following code

struct Int { int i; };

int main()
{
  Int z(1); // doesn't compile
}

and you get the error, no matching constructor Int(int).

Classes are an abstraction over something else. It might seem obvious that an int and an Int in this case are the same thing, and constructing one should be like constructing the other. But the compiler doesn't know that, so you need to provide a constructor that passes the argument onto the member.

struct Int 
{ 
  int i; 
  Int(int n) : i(n) {}
};

Note that in C++20, the compiler will actually be able to figure out that you want to use each constructor argument to initialize the corresponding member of your class, so even without a provided constructor, the Int will work like an int.

However, even in that case, your code would not work, since you need to use more complicated rules to construct your members. In your case, you'll need something like

myVector(int n) : vector_a(n), vector_b(n) {} 

Assuming that you want the internal vectors to be constructed with the value passed into the constructor. Based on your calling code, it seems that's what you want, but you can write any logic in the constructor.

cigien
  • 57,834
  • 11
  • 73
  • 112
  • Quick note: Your first example will compile in C++20 due to new aggregate initialization rules, but OP's still won't. – walnut Mar 28 '20 at 21:42
  • @yashrunwal If you have trouble fitting what you want to say in a comment, try to edit the new information into the question (but don't overwrite what is already there). You can edit your question by clicking "edit" under it or [here](https://stackoverflow.com/posts/60906610/edit). – walnut Mar 29 '20 at 06:18
  • So what i am tried to achieve is write a struct with 2 vectors. As you suggested @walnut I added a constructor to the code. In the main when I am calling the function, I want to pass a vector to my struct template which would then create a vector for me. Currently in the main, 'myVector vec_a(1), vec_b(2)' and after this when I try to print the vector 'std::cout << vec_a << std::endl;' I get following error **no match for ‘operator<<’**. What is this error? – y_1234 Mar 29 '20 at 06:19
  • @yashrunwal That is a new question that has nothing to do with this one. But it already has many answers on this side, e.g. [here](https://stackoverflow.com/questions/4421706) and [here](https://stackoverflow.com/questions/2981836). You need to overload the `<<` operator for your class. This stuff should also be explained in your textbook and if you aren't following a textbook yet, then you should start that, e.g. with [one of the recommended books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – walnut Mar 29 '20 at 06:24