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.

Johnnylame
  • 43
  • 2
  • Welcome to Stack Overflow. Please take the time to read [The Tour](http://stackoverflow.com/tour) and refer to the material from the [Help Center](http://stackoverflow.com/help/asking) what and how you can ask here. – πάντα ῥεῖ May 31 '17 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 [MCVE]. – πάντα ῥεῖ May 31 '17 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? –  May 31 '17 at 19:43
  • What is your error? – Jayson Boubin May 31 '17 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) – Johnnylame May 31 '17 at 19:46
  • please add all relevant information into the question by clicking the [edit] button. – bolov May 31 '17 at 19:47
  • well, you obviously try to initialize it with three ints. Use Vector({1, 2, 3}) – The Techel May 31 '17 at 19:47
  • Try `Vector p{{ 15, 16, 25} };` –  May 31 '17 at 19:48
  • Thanks Neil, it does work. Is there also a way, so i don't have to write another pair of curly brackets? Maybe overloading the operator{}? – Johnnylame May 31 '17 at 19:51
  • @Johnnylame There is no `operator{}` and c++ does not allow you to define new operators. – François Andrieux May 31 '17 at 19:52

2 Answers2

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.

François Andrieux
  • 28,148
  • 6
  • 56
  • 87
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.

Jayson Boubin
  • 1,504
  • 2
  • 11
  • 19