-2

I am aware this has been asked many times, but I didn't find anything useful to solve my problem. My header file is something like:

#include Myclass2.h
class Myclass{
public:
    Myclass2 instance;
   ... 
    Myclass(Myclass2 instance, param1, ecc.);

While the implementation file, among the other things, just implements the constructor:

Myclass::Myclass(Myclass2 instance,...){
this->instance = instance;

Basically, I would want to access the instance object from the class, but I fear this is not the right way, because it won't compile with error ''no matching function to call for Myclass2::Myclass2() and then it complains that Myclass2 takes multiple arguments (as it is) and I provide it 0.

But I am not constructing it into the class, I just want to take an already created object into the constructor!

Any ideas on how to solve the error, or the problem? Thanks!

EDIT

class Myclass2{
public:
    Myclass2(param1, param2, param3, param4);
 } 
madt1m
  • 15
  • 5
  • 3
    `Myclass::Myclass(Myclass2 instance) : instance(instance) {}` . It would also be less confusing if you give the parameter a name different from that of the member variable. – Igor Tandetnik Dec 20 '17 at 00:42
  • Did you try using constructor initializer list? Does that not solve the problem? – Algirdas Preidžius Dec 20 '17 at 00:42
  • Myclass::Myclass(Myclass2& instance,...) may fix it. – lakeweb Dec 20 '17 at 00:44
  • @lakeweb Why should it fix it? It isn't the problem with copying `Myclass2` instance. – Algirdas Preidžius Dec 20 '17 at 00:46
  • You have not posted `Myclass2` so I don't know. But it looks like there is no copy constructor. The `&` will have it pass a reference. And what R Sahu says. If you posted the code in question... – lakeweb Dec 20 '17 at 00:48
  • Welcome to Stack Overflow. Please take the time to go through the [The Tour](http://stackoverflow.com/tour) and refer to the material from the [Help Center](http://stackoverflow.com/help/asking) on what and how you can ask here.It's specially important to post a [mcve]. – R Sahu Dec 20 '17 at 00:48
  • @lakeweb The error is explicitly mentioned in the question itself, which mentions that there doesn't exist a constructor taking no parameters ("_no matching function to call for Myclass2::Myclass2()_"). Having such information, how can you deduce, that they problem is with copying? In addition: "_If you posted the code in question_" I am not the author of the question.. – Algirdas Preidžius Dec 20 '17 at 00:54
  • @Algirdas Preidžius you are right. His error is at `Myclass2 instance;` There it would expect a no param constructor. – lakeweb Dec 20 '17 at 01:08

1 Answers1

0

As others said in the comments, there is probably no default constructor and copy constructor for Myclass2 and Myclass. We don't see the actual code, but I think this example would help:

class Myclass2 {
public:
    Myclass2() = default;
    Myclass2(int const& x) : x(x) {}
    Myclass2(Myclass2 const& myclass2) : Myclass2(myclass2.x) {}

    int x;
};

class Myclass{
public:
    Myclass() : instance() {}
    Myclass(Myclass2 const& instance) : instance(instance) {}
    Myclass(Myclass const& myclass) : Myclass(myclass.instance) {}

    Myclass2 instance;
};

In some cases the explicitly generated default constructors are disabled (like when we declare our own constructors, more info http://en.cppreference.com/w/cpp/language/default_constructor), so we have to write them again. We need to create constructor Myclass2() : ... so we can default initialize like for exampleMyclass2 myclass; If all the members of class are default constructible, we can let it be automatically generated by declaring Myclass2() = default;(C++11)

The line in your code this->instance = instance; seem like something to be in member initialization list - it initializes members even "before" the constructor, so you can have const members that you would be otherwise unable to initialize. The syntax is:

Myclass(Myclass2 instance) : instance(instance) {
// the rest of constructor, where you can do what you want with already initialized instance
}

Don't worry about the instance(instance) syntax, it recognizes what is what (although some people think the same names are a little confusing). Also as others pointed out, if you are not taking Myclass2 instance as reference, you are creating temporary copy and there is no copy constructor for Myclass2.

Copy constructor (for Myclass that would be the one with Myclass const& instance argument only) is what we use when we want to initialize class like Myclass myclass(otherMyclass);.

It's pretty common to delegate constructors by Myclass(something) : Myclass() {} for example.

(Information about when you need to create which ones are available on http://en.cppreference.com/w/)

Sometimes we also may need to create copy assignment operator (http://en.cppreference.com/w/cpp/language/copy_assignment), so we can do things like:

Myclass m1;
Myclass m2;
m2 = m1;

but the explicit one is probably enough, if it's not you definitely wanna read What is The Rule of Three? and What is the Rule of Four (and a half)?.

ValentaTomas
  • 260
  • 3
  • 11
  • thanks! I understand the question was not conform to the guidelines, but nevertheless this solved the problem and I understood why! – madt1m Dec 20 '17 at 11:05