3

i'm new to C++ and programming in general, plus english isn't my first language so I might have trouble phrasing my question correctly for you to understand.

I wrote the following, working, program :

#include<iostream> #include<vector> using namespace std; class Vector{ vector<int>numbers; public: Vector(vector<int> const& v) : numbers(v) {} void print(){ for (const auto& elem : numbers){ cout << elem << " "; } } }; int main(){ Vector p{ vector < int > {15, 16, 25} }; p.print(); } 

Now if I try to create an object writing:

Vector p{ 15, 16, 25 }; 

it doesn't work. My question is what do I have to do in order for it to work? Help would be much appreciated! Thanks in advance.

10
  • Welcome to Stack Overflow. Please take the time to read The Tour and refer to the material from the Help Center what and how you can ask here. Commented May 31, 2017 at 19:41
  • 1
    "it doesn't work. My question is what do I have to do in order for it to work?" Isn't a clear problem description. What exactly doesn't work? Post all error message in your question verbatim please. Ideally a minimal reproducible example. Commented May 31, 2017 at 19:43
  • "it doesn't work" - In what sense? What error messages do you get? What compiler are you using? What version of the compiler? Commented May 31, 2017 at 19:43
  • What is your error? Commented May 31, 2017 at 19:45
  • Using visual studio 2013 express, im getting following error message. no instance of constructor "Vector::Vector" matches the argument list argument types are: (int, int, int) Commented May 31, 2017 at 19:46

2 Answers 2

4

The easiest way to get what you want is to use an additional set of braces to indicate that the arguments provided, together, form the first argument of the constructor. Try :

int main() { Vector p{ {15, 16, 25} }; p.print(); } 

Another way, if you are trying to make it work with main as it current is, is to implement an initializer list constructors.

#include <initializer_list> #include <iostream> #include <vector> using std::vector; using std::cout; class Vector { vector<int>numbers; public: Vector(vector<int> const& v) : numbers(v) {} // Implement initializer list constructor Vector(std::initializer_list<int> args) : numbers(args.begin(), args.end()) {} void print() { for (const auto& elem : numbers) { cout << elem << " "; } } }; int main() { Vector p{ 15, 16, 25 }; p.print(); } 

Note that you can pass the initializer list directory to std::vector's constructor (Vector(std::initializer_list<int> args) : numbers(args) {} would work), but this would incur an additional copy of the list.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that answered my question. Sorry for not really explaining my issue.
0

You need to add a constructor which takes a std::initializer_list like this:

Vector(std::initializer_list<int> v) : numbers(v){} 

add that, along side your other constructor, and that should work.

2 Comments

If you really wanted to avoid that, you could change it so that v is an r-value. That would allow for the specific implementation OP requested.
Many thanks. Much appreciated

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.