1

I want to make a constructor with array and array size so I can call the object using this: Multime m1 = Multime({1, 2, 3}, 3); Or should I use std::vector instead?

class Multime
{
private:
    int elemente[100];
    int size;
public:
    Multime(){}
    Multime(int el[50], int s){
        this -> size = s;
        for(int i = 0; i < this -> size; i++)
            this -> elemente[i] = el[i];
    }
};

int main()
{
    Multime m1 = Multime({1, 2, 3}, 3);
    return 0;
}

And i'm getting No matching constructor for initialization of 'Multime'

7
  • Is elemente supposed to always contains 100 elements ? Commented Jul 5, 2019 at 13:15
  • 1
    How about using std::vector or std::initializer_list as argument? Commented Jul 5, 2019 at 13:16
  • 3
    Are you sure {1, 2, 3} evaluates to an array of integers? Commented Jul 5, 2019 at 13:16
  • You cannot pass arrays as parameters. For more information, see the chapter in your C++ book that explains how function parameters types get decayed. In post-C++11 word you can declare a std::initializer_list parameter and avoid the need to pass an explicit array size parameter, and this will work; but this is a fairly advanced topic, see your C++ book for the details. Commented Jul 5, 2019 at 13:17
  • By the way, as mentioned you can't pass C-style arrays as arguments as simply as that. Like in C an argument declaration like int el[] (size is irrelevant) will be treated as int* el, in other words the function expects a pointer as argument. Your compiler should have mentioned it in the complete error message (though it might have been a little indirect). Commented Jul 5, 2019 at 13:19

2 Answers 2

2

Or should I use std::vector instead?

That's a great idea.

class Multime
{
private:
    std::vector<int> elemente;
public:
    Multime() {}
    Multime(std::vector<int> el) : elemente(std::move(el)) {}
};

int main()
{
    Multime m1 = Multime({ 1, 2, 3 });
    return 0;
}
Sign up to request clarification or add additional context in comments.

6 Comments

It would have been even better if you used a constructor initializer list. ;)
@Someprogrammerdude good idea. I keep forgetting about this feature way too often.
Better: elemente(std::move(el)).
Thanks you, I decided it is better to use std::vector but instead of move, I used push_back Multime(vector<int> el, int s){ this -> size = s; for(int i = 0; i < this -> size; i++) this -> elemente.push_back(el[i]);
@Andrei, don't do it. 1) You take el by copy, but you don't store that copy inside your object using a move-constructor. Copying is redundant: take el by const-reference. 2) push_backing in a loop causes redundant reallocations: preallocate space with elemente.reserve(s); before the loop. 3) Don't use int for a vector's size, use std::size_t and be consistent with the standard library.
|
0

If you want your class to be able to contain a varying number of elements (Defined at compile time), you can try:

#include <array>

template <size_t _Size>
class Multime
{
private:
    int elemente[_Size];
public:
    Multime() {}

    Multime(const std::array<int, _Size>& p_elements)
    {
        for (int i = 0; i < _Size; ++i)
            this->elemente[i] = p_elements[i];
    }
};

int main()
{
    Multime<3> m1({1, 2, 3});
    return 0;
}

You can also directly store an std::array into your class, so the construction is cleaner

#include <array>

template <size_t _Size>
class Multime
{
private:
    std::array<int, _Size> elemente;
public:
    Multime() {}
    Multime(const std::array<int, _Size>& p_elements) : elemente(p_elements) {}
};

int main()
{
    Multime<3> m1({1, 2, 3});
    return 0;
}

3 Comments

Using std::array member also would simplify constructor too.
It's up to him, I didn't want to break his logic
The name _Size is reserved. Change it to _size or Size, any name that starts with an underscore followed by a capital is reseved to the standard

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.