![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Expert Programmer
|
Declaration of ‘Class’ with no type
I am trying to write a chess game in C++ to get a better understanding of the language. I am getting an error that I cannot figure out:
Piece.h:11: error: ISO C++ forbids declaration of ‘Board’ with no type Piece.h:11: error: expected ‘;’ before ‘*’ token make: *** [Chess.o] Error 1 Piece.h: C++ Syntax (Toggle Plain Text)
Board.h: C++ Syntax (Toggle Plain Text)
My guess is that the problem is caused by both files including each other. Last edited by titaniumdecoy; Jun 30th, 2008 at 2:53 AM. |
|
|
|
|
|
#2 |
|
Expert Programmer
|
Re: Declaration of ‘Class’ with no type
I figured it out. I had to add a "forward declaration" for the other class at the beginning of each header file.
|
|
|
|
|
|
#3 |
|
Game engine designer
Join Date: May 2005
Location: Sweden
Posts: 301
Rep Power: 4
![]() |
Re: Declaration of ‘Class’ with no type
Great you solved it! What you got is called "Circular dependency".
__________________
http://www.klarre.se |
|
|
|
|
|
#4 |
|
Expert Programmer
|
Re: Declaration of ‘Class’ with no type
I have another problem. I have a pure virtual class Piece from which Pawn is derived. The Piece class contains a pointer to a Board object. When I define the virtual destructor for the class, I get this error:
g++ -c -Wall Piece.c Piece.c: In destructor ‘virtual ChessGame::Piece::~Piece()’: Piece.c:17: warning: possible problem detected in invocation of delete operator: Piece.c:17: warning: invalid use of undefined type ‘struct ChessGame::Board’ Piece.h:8: warning: forward declaration of ‘struct ChessGame::Board’ Piece.c:17: note: neither the destructor nor the class-specific operator delete will be called, even if they are declared when the class is defined. g++ -c -Wall Pawn.c g++ -o chess -Wall Chess.o Board.o Piece.o Pawn.o Is this message displayed because the class is abstract? Please advise. EDIT: Never mind. It turns out I had to #include "Board.h" in order to delete a Board object. |
|
|
|
|
|
#5 |
|
Expert Programmer
|
Re: Declaration of ‘Class’ with no type
Ok, another problem. I am trying to print a Piece (specifically a Pawn) to std::cout. In main(), I have no problem printing a pawn:
Pawn pawn(board); std::cout << pawn << std::endl; I have a 2D array of pointers to objects derived from the pure virtual class Pieces in class Board. I am trying to print a piece as follows (Note that I have made Board.pieces temporarily public while I test this): inline std::ostream& operator<< (std::ostream& os, const Board& board)
{
int i = 0, j = 0;
...
if (board.pieces[i][j] != NULL)
std::cout << *(board.pieces[i][j]) << " ";
...
return os;
}I get a huge unreadable printout, the beginning of which I have copied below: Board.h: In function ‘std::ostream& ChessGame::operator<<(std::ostream&, const ChessGame::Board&)’: Board.h:33: error: no match for ‘operator<<’ in ‘std::cout << *(ChessGame::Piece*)board->ChessGame::Board::pieces[i][j]’ /usr/include/c++/4.0.0/bits/ostream.tcc:67: note: candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>& (*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>] ... Board.h:26: note: std::ostream& ChessGame::operator<<(std::ostream&, const ChessGame::Board&) make: *** [Piece.o] Error 1 Help! Last edited by titaniumdecoy; Jun 30th, 2008 at 9:58 PM. |
|
|
|
|
|
#6 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4
![]() |
Re: Declaration of ‘Class’ with no type
It looks like you need to get rid of the pointer dereference (the *) when you access the specific piece on the board.
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS! |
|
|
|
|
|
#7 |
|
Expert Programmer
|
Re: Declaration of ‘Class’ with no type
When I do that, it just prints the address of the pointer...
|
|
|
|
|
|
#8 |
|
Expert Programmer
|
Re: Declaration of ‘Class’ with no type
Ok, so far I have been unable to solve the printing problem, but I have an even more annoying problem now:
Pawn.h:11: error: expected class-name before ‘{’ token
Pawn.h:13: error: expected `)' before ‘&’ token
make: *** [Piece.o] Error 1Pawn.h: C++ Syntax (Toggle Plain Text)
Help! |
|
|
|
|
|
#9 |
|
Expert Programmer
|
Re: Declaration of ‘Class’ with no type
I solved the problem in the post above. It turns out that I moved the definition,
std::ostream& operator<< (std::ostream& os, const ChessGame::Board& board) {From Board.h to Board.c, a change which the compiler did not approve of. I also had to remove a number of #include "xxx.h" in favor of forward class declarations. As for the problem printing a Pawn to std::cout, I am still stumped. EDIT: I finally figured it out. I had to #include "Piece.h" in Board.h. Last edited by titaniumdecoy; Jul 1st, 2008 at 12:40 AM. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Arrays with Generic Type | azurik | Java | 6 | Jun 14th, 2006 12:31 PM |
| Pointer return type,.. ??? | Mack1982 | C++ | 28 | May 26th, 2006 8:02 PM |
| base type has incomplete type | Eric the Red | C++ | 4 | Mar 3rd, 2006 8:04 PM |
| Defining a bool type | nnxion | C | 6 | Oct 7th, 2005 8:39 AM |
| Solve Declaration type | freddielj | C++ | 9 | Mar 29th, 2005 10:40 AM |