In C ++ 11 I have a problem using interfaces. error: no matching function for call to 'MyClass :: MyClass (Observee (&) ())'

1

I am programming a listener (Observer) in C ++ version 11 and if I do a new project I do not have problems doing some simple classes. But when I incorporate these classes into my main project, it gives me some errors that I do not know how to solve.

I have tried to make the examples that people put on the internet, and I have simplified them as much as possible.

My environment is linux mint and IDE netbeans with g ++

I'll give you the code and the error to see if you can help me.

main.cpp

#include "Observer.h"
#include "Observee.h"
#include "Myclass.h"
int main(int argc, char** argv) {
    Observee observable();
    // AQUI es donde me da el error. he intentado pasarlo con * y con &.
    MyClass obj = new MyClass(observable);
    MyClass obj2(observable);
    observable.Trigger();  

Error

src/main.cpp: In function ‘int main(int, char**)’:
src/main.cpp:64:41: error: no matching function for call to ‘MyClass::MyClass(Observee (&)())’
    MyClass obj = new MyClass(observable);
                                        ^
In file included from src/main.cpp:39:0:
src/Myclass.h:26:8: note: candidate: MyClass::MyClass(Observee&)
        MyClass(Observee& observable1): observable(observable1) {
        ^
src/Myclass.h:26:8: note:   no known conversion for argument 1 from ‘Observee()’ to ‘Observee&’
src/Myclass.h:21:7: note: candidate: MyClass::MyClass(const MyClass&)
class MyClass : public Observer {
    ^
src/Myclass.h:21:7: note:   no known conversion for argument 1 from ‘Observee()’ to ‘const MyClass&’
nbproject/Makefile-Debug.mk:123: fallo en las instrucciones para el objetivo 'build/Debug/GNU-Linux/src/main.o'

Observer.h

#include "Observee.h"

class Observer {
public:
    virtual void Notify()=0;
//    virtual void Notify(Observee* observee)=0;
};

Observee.h

class Observer;

class Observee {
public:
    virtual ~Observee();
    bool AddObserver(Observer& observer);
    bool RemoveObserver(Observer& observer);
    bool NotifyObservers();
    void Trigger();
// protected:
    Observee(){};
private:
    std::set<Observer*> observers;
    // Observee(const Observee& orig);
    // Observee& operator=(const Observee& yRef);
};

Observee.cpp

#include "Observee.h"
#include "Observer.h"
#include <iostream>
#include <set>


Observee::~Observee() {
}

bool Observee::AddObserver( Observer& observer )
{
    if(observers.find(&observer) != observers.end())
        return false;
    observers.insert(&observer);
    return true;
}

//This method removes an observer from the vector
bool Observee::RemoveObserver( Observer& observer )
{
    if(observers.find(&observer) == observers.end())
        return false;
    return observers.erase(&observer) == 1;
}

//This Method is very important, it triggers all Notify() methods of all observers.
//The specific code in each class that inherits from observer will be executed
bool Observee::NotifyObservers()
{
    std::set<Observer*>::iterator itr;
    for ( itr = observers.begin();
        itr != observers.end(); itr++ )
    (*itr)->Notify();
    return (observers.size() > 0);
}

void Observee::Trigger()
{
    NotifyObservers();
}

MyClass.h

#include "Observer.h"
#include "Observee.h"
#include <iostream>

class MyClass : public Observer {

        Observee observable;

    public:
    MyClass(Observee& observable1): observable(observable1) {
        observable.AddObserver(*this);
    }

    ~MyClass() {
        observable.RemoveObserver(*this);
    }

    void Notify() {
            std::cout << "Received a change event" << std::endl;
    }
};
    
asked by Seni 27.10.2017 в 11:52
source

2 answers

2

With this line

Observee observable();

You are declaring a function (although it seems the opposite to you and it shows the error message:

src/main.cpp:64:41: error: no matching function for call to ‘MyClass::MyClass(Observee (&)())’
MyClass obj = new MyClass(observable);

Where Observee (&)() is a reference to a function.

To call the constructor by default, avoid the parentheses:

Observee observable;

Or, as you are in c ++ 11, you can also use the keys:

Observee observable{};

On the other hand note that here:

MyClass obj = new MyClass(observable);

obj is not a pointer, then you should not use new to instantiate it.

    
answered by 27.10.2017 / 14:01
source
3

Here:

Observee observable();

You are not creating an object but you are declaring an "observable" name function that does not take parameters and that returns an object of type Observee.

It's what Scott Meyers called "most vexing parse" years ago, saying (what I translate as) "If something can be the declaration of a function, it will be like that". You can see link

    
answered by 27.10.2017 в 13:50