Sign in to follow this  
Mattman

Abstract class compile errors

Recommended Posts

For some reason I'm getting a whole bunch of errors all pointing to line 9 of my BaseBuffer.h class (marked with a comment in its respective code block below). Here are the compile-time errors I'm getting that I can't figure out what the problem is. BaseBuffer.h:
#ifndef _BASEBUFFER_H_
#define _BASEBUFFER_H_

#include "BaseGraphicsEngine.h"

class BaseBuffer {
public:
	virtual void setSize(unsigned int width, unsigned int height) = 0;
	virtual void clear() = 0;
};

#endif // _BASEBUFFER_H_
BaseGraphicsEngine.h:
#ifndef _BASEGRAPHICSENGINE_H_
#define _BASEGRAPHICSENGINE_H_

#include "BaseBuffer.h"

class BaseGraphicsEngine {
public:
	virtual bool render() = 0;
	virtual BaseBuffer* createBuffer(unsigned int width, unsigned int height) = 0; // THIS IS LINE 9 FROM ERROR MESSAGES.
};

#endif // _BASEGRAPHICSENGINE_H_
Compile errors:
BaseGraphicsEngine.h(9): error C2143: syntax error : missing ';' before '*'
BaseGraphicsEngine.h(9): error C2433: 'BaseGraphicsEngine::BaseBuffer' : 'virtual' not permitted on data declarations
BaseGraphicsEngine.h(9): error C2501: 'BaseGraphicsEngine::BaseBuffer' : missing storage-class or type specifiers
BaseGraphicsEngine.h(9): error C2501: 'BaseGraphicsEngine::createBuffer' : missing storage-class or type specifiers
BaseGraphicsEngine.h(9): warning C4183: 'createBuffer': missing return type; assumed to be a member function returning 'int'
BaseGraphicsEngine.h(9): error C2253: 'BaseGraphicsEngine::createBuffer' : pure specifier only applies to virtual function - specifier ignored

Share this post


Link to post
Share on other sites
You've got a dependency cycle. Each header tries to include the other, the include guards correctly prevent that. Otherwise your preprocessor would get stuck in an infinite loop (at least until it bails out for trying to nest too many #includes).

Hint: The compiler does not recognize "BaseBuffer" as an existing type (see the warning).

Do both class really need to know all about each other in their definition. Particularly, each other's size?

Hinte: You don't need a full class definition to declare a function that returns a pointer to that class.

Read Kylotan's article.

Share this post


Link to post
Share on other sites
Quote:
Original post by Fruny
You've got a dependency cycle. Each header tries to include the other, the include guards correctly prevent that.

Quote:
Fruny
it looks like you're giving the compiler a circular reference

Good call guys, I totally missed this. Thanks!
Quote:
Original post by Fruny
Note that you don't need a full class definition to declare a function that returns a pointer to that class.

I've done this previously, I just wasn't even realizing that I had that circular dependency. Thanks again! (Rating++ all around!)

Share this post


Link to post
Share on other sites
Yeah you dont look like your using BaseGraphicsEngine class in that class anyway but if you change things around or if your just getting started (these look like just concept classes anyway) and latter you do need those 2 to include each other you sould do a forward decleration thats where you place a line like this into your BaseBuffer.h class

class BaseGraphicsEngine; //yeah thats it

then you have to make a seprate .cpp file for all the function defenitions and #include "BaseGraphicsEngine.h" in that file (because .cpp files dont include anything but their .h file, or at least they shouldnt, you wont get a circular refrence)
you would of course have to do the same thing with both of them and when you start using templates this method might get a little messy =p

hope i helped (if your still not clear on exactly what to do PM me and i can give you an example)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this