简体   繁体   中英

error C4430: missing type specifier / error C2143: syntax error : missing ';' before '*'

I am getting both errors on the same line. Bridge *first in the Lan class. What am i missing?

#include <stdio.h>
#include <stdlib.h>
#include <iostream>

using namespace std;


class Lan{
    Bridge *first;
    Bridge *second;
    Host hostList[10];
    int id;
};

class Bridge{
    Lan lanList[5];
};




class Host{
    Lan * lan;
    int id;
public:
    Host(int newId)
    {
        id=newId;
    }
};



void main(){

return;
}

Declare Bridge before Lan

#include <stdio.h>
#include <stdlib.h>
#include <iostream>

using namespace std;

class Bridge;

class Lan{
    Bridge *first;
    Bridge *second;
    Host hostList[10];
    int id;
};

class Bridge{
    Lan lanList[5];
};

You are missing the forward declaration for Bridge . Otherwise when compiling Lan class compiler doesn't know what Bridge* is. You should tell the compiler that Bridge is a class which you are going to define later. Forward declare it as class Bridge; before class Lan

Just put a class Bridge; before the declaration of the Lan class.

Bridge is not defined at the moment it is used.

you need a forward declaration so that the compiler knows that Bridge is a valid class name. before the Lan class, write:

class Bridge;

Bridge doesn't exist until after the Lan declaration. you should forward-declare Bridge. besides that, Lan won't compile because Host is not known either, and forward declaration won't help, because the compiler needs to know Host's size.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM