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

3

6

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?

Fourteenth answered 12/3, 2014 at 12:28 Comment(1)
You have void setId(std::string p_id);, yet I don't see an include for <string>.Riyal
B
16

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.

Breakup answered 12/3, 2014 at 12:33 Comment(1)
Thanks, i had a suspicion that it was something as obvious as that. I thought the include guards would resolve this. Now i learned the difference between multiple and circular includes - and that include guards don't protect against the latter. Still sad to see auto-generated code from Rhapsody that makes a simple mistake as this. Thanks!Fourteenth
S
5

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.

Stillage answered 12/3, 2014 at 12:34 Comment(1)
Thanks, sadly i can only choose 1 answer - yours being identical, i chose the first one.Fourteenth
B
2

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
Bristle answered 7/8, 2019 at 9:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.