Question

I know this is a commonly asked question about C++ but following advice of other answers and such, I am still unable to get my seemingly simple code to work. My problem is the following code gives "error: redefinition of 'class Communicator'":

global.h

#ifndef GLOBAL_H 
#define GLOBAL_H

class object_payload;
class pending_frame;

class Communicator {
private:
    map<string,object_payload*> local_objects;
    map<string,pending_frame*> remote_tasks;
    bool listening;

public:
    Communicator();
    void stop_listening();
    void add_to_remote_tasks(string name, pending_frame* pfr);
    void listen();
    void distributed_release(string task_name);

};

extern Communicator communicator;

#endif

global.cpp

#include "global.h"

class Communicator {

private:
    map<string,object_payload*> local_objects;
    map<string,pending_frame*> remote_tasks;

    bool listening;

public:

    Communicator(){
        // implementation
    }

    void stop_listening(){
        // implementation
    }

    void add_to_remote_tasks(string name, pending_frame* pfr){
        // implementation
    }

    void listen(){
        // implementation
    }

    void distributed_release(string task_name){
        // implementation
    }
};

Communicator communicator;

Does anyone know why this would be giving this error? The .cpp includes the header. I have other .cpp files that also include the header, but with the guard I don't see why that would matter.

Thanks for any help on this, much appreciated.

EDIT: Also, my runner.cpp file (with main) includes global.h in order to access the communicator global object.

Was it helpful?

Solution

You must have only one definition of a class. Currently you get one from #include and another in the file.

You shall not repeat the class itself, just implement the functions out of class, like

Communicator::Communicator(){
    // implementation
}

OTHER TIPS

That's not how you do the separation. The class (i.e. the declaration) goes into the header; the CPP file should have method implementations, like this:

Communicator::Communicator() {
    ...
}
void Communicator::stop_listening() { 
    ...
}

and so on. Note the Communicator:: part of the fully qualified name: this is what tells the compiler that the function that you are defining belongs to the Communicator class.

In your cpp file you only need to define the functions that were declared but not defined in the header and scope them to your class:

Communicator::Communicator(){
    // implementation
}

void Communicator::stop_listening(){
    // implementation
}

void Communicator::add_to_remote_tasks(string name, pending_frame* pfr){
    // implementation
}

void Communicator::listen(){
    // implementation
}

void Communicator::distributed_release(string task_name){
    // implementation
}

You define the class Communicator in the header file, and then try to add to it in the .cpp file. You can't do that in C++ - all the parts of the class definition must be in the same place.

Your header file should probably contain all the member definitions and function declarations, and your .cpp should proceed to define the member functions like:

void Communicator::stop_listening() { ... }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top