Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

The other answers are correct, but I would like to add few things for completeness.

1. Cause: your project have circular including, specifically, when you compile "TDPoint.cpp", the compiler will do the following

#include "TDPoint.h" //start compiling TDPoint.h
#include "Topics.h" //start compiling Topics.h
#include "TDPoint.h" //compilation of TDPoint.h skipped because it's guarded
#include "Track.h" //start compiling Track.h
#include "Topics.h" //compilation of Topics.h skipped because it's guarded
 //resume compiling Track.h 
... 
TDPoint* getPosition() const; //=> error TDPoint is not defined

=>C2143: syntax error: missing ';' before '*' 

2. Counter measure: replace including in header by forward declaration to remove circle of including, and use including in .cpp files. Specifically, forward declaration means: (in Topics.h)

#ifndef Topics_H
#define Topics_H
#include <oxf\oxf.h>
#include "Request.h"
class TDPoint;  //Forward declaration to replace #include "TDPoint.h"
class Track; //Forward declaration to replace #include "Track.h"
#include "TrackReport.h"
#endif
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top