Header file headache
Im trying to learn how to use header files with classes. Im using a book but I cant figure out why I always get this error
error C2653: ''BoardClass'' : is not a class or namespace name
Error executing cl.exe.
Here is the code from my main:
#include
#include "Board.h"
int main() {
BoardClass GameBoard;
GameBoard.DrawBoard();
return(0);
}
from the header file:
#ifndef _BOARDCLASS_
#define _BOARDCLASS_
#include
class BoardClass {
public:
void DrawBoard();
};
#include "Board.cpp"
#endif
and board.cpp:
#include
void BoardClass::DrawBoard() { //error here
cout << "Draw Board!";
}
I can''t figure out for the life of me why this happens, I have a decent sized project in school I did using header files, but I dont get the error there and I can''t figure out why. This is killing me, any help would be greatly appreciated, thanks.
Just trying to get the basics down but ther
You should never #include a .cpp file
It should look like this;
//board.h
#ifndef BOARD_CLASS
#define BOARD_CLASS
class BoardClass {
public:
void DrawBoard();
};
#endif
//board.cpp
#include "board.h"
void BoardClass::DrawBoard()
{
//do something
}
//main.cpp
#include "Board.h"
int main() {
BoardClass GameBoard;
GameBoard.DrawBoard();
return(0);
}
////////////////////////
I don''t know where you''re getting the "#include" part by itself - that''s not needed or used. Also, like I said, never include .cpp files.
Hope that helps
Clay
It should look like this;
//board.h
#ifndef BOARD_CLASS
#define BOARD_CLASS
class BoardClass {
public:
void DrawBoard();
};
#endif
//board.cpp
#include "board.h"
void BoardClass::DrawBoard()
{
//do something
}
//main.cpp
#include "Board.h"
int main() {
BoardClass GameBoard;
GameBoard.DrawBoard();
return(0);
}
////////////////////////
I don''t know where you''re getting the "#include" part by itself - that''s not needed or used. Also, like I said, never include .cpp files.
Hope that helps
Clay
Clay LarabieLead DeveloperTacendia.com
It looks like you are trying to include board.cpp in board.h... you should be doing the opposite. Board.h should be included in board.cpp like so ''#include "board.h"''. Another thing which may or may not help, but is something to try if the above does not help, is your
#ifndef _BOARDCLASS_
#define _BOARDCLASS_
statements... I always write mine like
#ifndef BOARDCLASS_H
#define BOARDCLASS_H
I hope one of these ideas helps out... post back if they dont.
#ifndef _BOARDCLASS_
#define _BOARDCLASS_
statements... I always write mine like
#ifndef BOARDCLASS_H
#define BOARDCLASS_H
I hope one of these ideas helps out... post back if they dont.
There is no spoon.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement