Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 30th, 2008, 2:25 AM   #1
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 856
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
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)
  1. #ifndef PIECE_H
  2. #define PIECE_H
  3.  
  4. #include "Board.h"
  5.  
  6. namespace ChessGame {
  7.  
  8. class Piece
  9. {
  10. protected:
  11. Board *board; // this line is indicated in the error message
  12.  
  13. ...

Board.h:

C++ Syntax (Toggle Plain Text)
  1. #ifndef BOARD_H
  2. #define BOARD_H
  3.  
  4. #include <iostream>
  5. #include "Piece.h"
  6.  
  7. namespace ChessGame {
  8.  
  9. class Board
  10. {
  11. private:
  12. Piece *pieces[8][8];
  13.  
  14. ...

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.
titaniumdecoy is offline   Reply With Quote
Old Jun 30th, 2008, 2:42 AM   #2
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 856
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
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.
titaniumdecoy is offline   Reply With Quote
Old Jun 30th, 2008, 6:16 AM   #3
Klarre
Game engine designer
 
Klarre's Avatar
 
Join Date: May 2005
Location: Sweden
Posts: 301
Rep Power: 4 Klarre is on a distinguished road
Re: Declaration of ‘Class’ with no type

Great you solved it! What you got is called "Circular dependency".
__________________
http://www.klarre.se
Klarre is online now   Reply With Quote
Old Jun 30th, 2008, 7:58 PM   #4
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 856
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
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.
titaniumdecoy is offline   Reply With Quote
Old Jun 30th, 2008, 9:46 PM   #5
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 856
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
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.
titaniumdecoy is offline   Reply With Quote
Old Jun 30th, 2008, 10:10 PM   #6
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4 Jessehk is on a distinguished road
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!
Jessehk is offline   Reply With Quote
Old Jun 30th, 2008, 10:16 PM   #7
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 856
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Re: Declaration of ‘Class’ with no type

When I do that, it just prints the address of the pointer...
titaniumdecoy is offline   Reply With Quote
Old Jun 30th, 2008, 10:44 PM   #8
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 856
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
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 1

Pawn.h:

C++ Syntax (Toggle Plain Text)
  1. #ifndef PAWN_H
  2. #define PAWN_H
  3.  
  4. #include <iostream>
  5. #include "Piece.h"
  6. #include "Board.h"
  7.  
  8. namespace ChessGame {
  9.  
  10. class Pawn : public Piece
  11. { // line 11
  12. public:
  13. Pawn(Board& board); // line 13
  14.  
  15. ...

Help!
titaniumdecoy is offline   Reply With Quote
Old Jul 1st, 2008, 12:26 AM   #9
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 856
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
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.
titaniumdecoy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:01 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC