Domanda

I have a small issue with my C++ code.

class Command {
public:
    virtual void start(CommandDesc userinput, Converter* convertobj) = 0;
    virtual void help(int option) = 0;
};


struct CommandDesc
{
    std::string name;
    std::string description;
    Command* comobj;   //Issue is here
};

If I define the structure before the class, I won't be able to define a member

Command* comobj; 

If I define after the class, I wont be able to pass instance of struct to method with

virtual void start(CommandDesc userinput, Converter* convertobj) = 0;

What can you suggest? Is there any way to first declare the structure, than define it separately?

È stato utile?

Soluzione

Ok, if I define the structure before the class, I won't be able to define a member

Command* comobj;

Since comobj is a pointer, you can forward declare Command to resolve this.

You'd do this:

class Command;

struct CommandDesc
{
    std::string name;
    std::string description;
    Command* comobj; 
};

class Command {
public:
    virtual void start(CommandDesc userinput, Converter* convertobj) = 0;
    virtual void help(int option) = 0;
};

Altri suggerimenti

Yes - simple - forward declaration

Just put

class Command;

struct CommandDesc
{

 ....

}

class Command {
 As before

};
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top