Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unknown class? C2143 syntax error: missing ";" before '*'

I get the error "C2143: syntax error: missing ';' before '*' in Track.h I believe this is due to a "missing" class definition.

These are the 3 header files:

Topics.h, the package-level header file, which #includes everything else:

#ifndef Topics_H
#define Topics_H

#include <oxf\oxf.h>
#include "Request.h"
#include "TDPoint.h"
#include "Track.h"
#include "TrackReport.h"

#endif

Then there's TDPoint (as in "3DPoint"), which simply defines a class with 3 long attributes:

#ifndef TDPoint_H
#define TDPoint_H

#include <oxf\oxf.h> // Just IBM Rational Rhapsody's Framework
#include "Topics.h"

class TDPoint {
    ////    Constructors and destructors    ////

public :

    TDPoint();

    ~TDPoint();

    ////    Additional operations    ////

long getX() const;    
void setX(long p_x);
long getY() const;    
void setY(long p_y);    
long getZ() const;
void setZ(long p_z);

    ////    Attributes    ////

protected :

    long x;    
    long y;    
    long z;};

#endif

But the problem lies here, in the marked line:

#ifndef Track_H
#define Track_H

#include <oxf\oxf.h> // Just IBM Rational Rhapsody's Framework
#include "Topics.h"
#include "TDPoint.h"

class Track {

public :

    ////    Operations     ////

    std::string getId() const;

    void setId(std::string p_id);

    TDPoint* getPosition() const; // <--- This line, the first line to use TDPoint, throws the error

    ////    Attributes    ////

protected :

    std::string id;   

    TDPoint position;

public :

     Track();
     ~Track();
};

#endif

My guess was that the compiler (MS VS2008/ MSVC9) simply didn't know the class "TDPoint." But even defining the class in the same header file as "Track", or using a forward declaration like "class TDPoint" (which then throws the error: undefined class) didn't help. The code was auto-generated from Rhapsody, if that makes any difference.

But maybe the error is something else entirely?

like image 788
unknown_user Avatar asked Nov 30 '22 02:11

unknown_user


2 Answers

Topics.h includes TDPoint.h and Track.h

TDPoint.h includes Topics.h

and Track.h includes both Topics.h and TDPoint.h

This feels like a circular include... You should either forward declare your classes to solve it or modify Topics.h to not to have circularity.

like image 160
Ferenc Deak Avatar answered Dec 04 '22 03:12

Ferenc Deak


You have circular inclusion: The file Track.h includes Topics.h which includes TDPoints.h which includes Topics.h which includes Track.h where the TDPoint class is not declared.

In fact, the TDPoint.h doesn't need any header files at all, it's completely independant (as per the code shown in your question).

The Track.h file only needs to include TDPoint.h, not Topics.h. (And possibly <string>.)

General hint: Include as few headers as possible in a header file.

like image 34
Some programmer dude Avatar answered Dec 04 '22 02:12

Some programmer dude