• Advertisement

Archived

This topic is now archived and is closed to further replies.

Header file headache

This topic is 6453 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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







Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

  • Advertisement