B McDowell

Hello, I am currently having two errors that I just can't seem to figure out. The first is error code C2504 " 'Expr' base class undefined" and the second is error code c2248 " 'Expr' :: 'Expr' cannot access private member declared in class 'Expr'."

My program uses a hierarchy of classes to store mathematical expressions, which can be evaluated using a visitor class. Expr is a virtual class that holds all expressions, literal, binary, and unary. Binary is a subclass of Expr, and holds all binary operations, such as addition, subtraction, and multiplication. Add is a subclass of Binary, and uses pointers _left and _right to hold its operands. Each class is also located in its own .h file.

I have pasted the code below, and noted where the error occurs. I would greatly appreciate any help. I've been stuck on this one for at least 8 hours, and I know the solution can't be that difficult

//code for Expr.h

#ifndef EXPR

#define EXPR

class Visitor;

#include "Visitor.h"

class Expr

{

public:

virtual void Accept(Visitor&) = 0;

~Expr();

private:

Expr();

Expr(Expr&);

};

#endif

//code for Binary.h

#ifndef BINARY

#define BINARY

#include "Expr.h"

class Binary: public Expr

{

public:

virtual void Accept(Visitor&) = 0;

Expr* getright();

Expr* getleft();

~Binary();

protected:

Binary(Expr* left, Expr* right): _left(left), _right(right){};

private:

Expr* _left;

Expr* _right;

Binary();

Binary(Binary&);

};

#endif

//code for Add.h

#ifndef ADD

#define ADD

#include "Expr.h"

#include "Binary.h"

class Add: public Binary

{

public:

Add(Expr* left, Expr* right): _left(left), _right(right){};

Expr* getright()

{

return _right;

}

Expr* getleft()

{

return _left;

}

~Add();

Add();

virtual void Accept(Visitor& v) {v.visitadd(this);}

private:

Expr* _left;

Expr* _right;

Add(Add&);

};

#endif



Re: Visual C++ Language Difficulties with error codes C2504 and C2248

Jonathan Caves - MSFT

You haven't indicated where the errors occur. One minor thing I see is that in add.h

Code Block

#include "Expr.h"

#include "Binary.h"

should be just:

Code Block
#include
"Binary.h"

As binary.h already includes expr.h






Re: Visual C++ Language Difficulties with error codes C2504 and C2248

B McDowell

Ah, sorry about that. I've now marked the lines in which the errors occur with red comments.

I've also noticed that all direct subclasses of Expr are giving me error C2504.

//code for Expr.h

#ifndef EXPR

#define EXPR

class Visitor;

#include "Visitor.h"

class Expr

{

public:

virtual void Accept(Visitor&) = 0;

~Expr();

private:

Expr();

Expr(Expr&);

};

#endif

//code for Binary.h

#ifndef BINARY

#define BINARY

#include "Expr.h"

class Binary: public Expr

{ //Error 2504

public:

virtual void Accept(Visitor&) = 0;

Expr* getright();

Expr* getleft();

~Binary();

protected:

Binary(Expr* left, Expr* right): _left(left), _right(right){}; //Error C2248

private:

Expr* _left;

Expr* _right;

Binary();

Binary(Binary&);

};

#endif

//code for Add.h

#ifndef ADD

#define ADD

#include "Expr.h"

#include "Binary.h"

class Add: public Binary

{

public:

Add(Expr* left, Expr* right): _left(left), _right(right){};

Expr* getright()

{

return _right;

}

Expr* getleft()

{

return _left;

}

~Add();

Add();

virtual void Accept(Visitor& v) {v.visitadd(this);}

private:

Expr* _left;

Expr* _right;

Add(Add&);

};

#endif





Re: Visual C++ Language Difficulties with error codes C2504 and C2248

Viorel.

Do you have lines like #include "stdafx.h" in your CPP files and can you confirm that your H files are included after stdafx.h

And can you remove #include "Visitor.h" from Expr.h





Re: Visual C++ Language Difficulties with error codes C2504 and C2248

B McDowell

yes, each of my cpp files have inclusions that fit that format, if that is what you're asking.

I also removed #include "visitor.h" from Expr.h, and that appears to have fixed my error code C2504. C2248 is still there though.





Re: Visual C++ Language Difficulties with error codes C2504 and C2248

Viorel.

B McDowell wrote:

[...] that appears to have fixed my error code C2504. C2248 is still there though.

Try the following declaration:

Code Block

class Expr

{

. . .

protected:

Expr();

};

I hope it works.





Re: Visual C++ Language Difficulties with error codes C2504 and C2248

B McDowell

I just tried it, and unfortunately, that actually caused more errors. After changing the default constructor of Expr from private to protected, the visitor class was no longer recognized, and all of my pointers to Expr failed to work.

I'm beginning to think my code my be cursed.