开发者

error C2027 and error C2227

开发者 https://www.devze.com 2022-12-12 19:52 出处:网络
I get error C2027: use of undefined type \'Bridge\' and error C2227: left of \'->receive\' must point to class/struct/union/generic type

I get error C2027: use of undefined type 'Bridge'

and

error C2227: left of '->receive' must point to class/struct/union/generic type

on line *connection1->receive(newMessage,2);

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#define MAXHOST 10
#define MAXPORT 5
#define MAXLAN 8
#define MAXBRIDGE 5
#define MAXLANBRIDGECON 2

using namespace std;
class Bridge;
class Host;
class Message;
class Simulation;
class Lan;


class Message{
//////////////

};


class Host{
    Lan * lan1;
    int id;

    int nextMessageTime;
public:
    /////////
};

class Lan{

    Bridge *connection1, *connection2;
    int bridgeConnection;
    Host hostList[MAXHOST];
    int id;
    int hostCount;
public:
    void connect(Bridge  * const newBridge)
    {
        if(bridgeConnection==0)
        {
            connection开发者_如何学JAVA1 = newBridge;


        }
        if(bridgeConnection==1)
        {
            connection2 = newBridge;

        }
        bridgeConnection++;
    }
    void receive(Message newMessage){

        *connection1->receive(newMessage,2);


    }


};

class Bridge{
/////////////////////
};
void main(){
    Simulation newSim;
    newSim.create();

return;
}


All the posts before are right, forward declaring is used to prevent circular includes in header files. The Bridge class is forward declared, so that you can specify pointers of that type within your class definition of LAN. Since pointers all have the same size this is ok.

When it comes to using this class the compiler has to know more about the Bridge class, at least its size. But there is no information other than that there is a class named Bridge.

The solution would be either to include the header where Bridge is defined (delete the class Bridge definition if you do this), or to move the implementation of LAN::connect() and LAN::receive() in its own implementation file LAN.cpp and include the Bridge header there which is probably the clean solution.


Use

connection1->receive(newMessage,2);


The line that is getting you in trouble is the following

*connection1->receive(newMessage,2);

In this case connection1 is of type Bridge*. It is OK to have a pointer to a variable before the type is defined. But you cannot use a direct reference to the type before it's fully defined. The dereference operator (*) causes the type to be used directly. Since it's not defined, you get the appropriate error message.

Try moving the Bridge class up above this class or moving the method to a .cpp file.


You're attempting to use the class Bridge, by calling receive, without actually defining what Bridge is. You've forward-declared it, but the compiler can't tell that it implements a method called receive.

Also, it's just connection1->receive(newMessage,2).


The problem is when you write the statement *connection1->receive(newMessage,2); compiler has to make sure that receive method exists in Bridge class. With just forward declaration of Bridge it can not get this information. You need to provide the class definition of Bridge before using it.


The compiler needs to see the definition of class Bridge before evaluating the first line which makes substantive use of the class. The forward declaration suffices for declaring a pointer to an instance of the class because this does not require any knowledge of the class structure. However, the class definition is required for the compiler to generate code for invoking a method against the class.

0

精彩评论

暂无评论...
验证码 换一张
取 消