Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [C++] Help with classes  (Read 5754 times)

0 Members and 1 Guest are viewing this topic.

YellowShadow

  • Newbie
  • *
  • Posts: 29
    • AOL Instant Messenger - ehwatsupdoc21
    • View Profile
[C++] Help with classes
« on: January 15, 2010, 02:33:14 am »
I'm trying to write some classes for my game, but I keep running into some stupid errors.

Here is my Board.h file:
Code: [Select]

#ifndef BOARD_H
#define BOARD_H

#define PIECE_WIDTH 65
#define PIECE_HEIGHT 67
#define BOARDSIZE 8

#include <SFML/Graphics.hpp>
#include <list>

enum BoardState { INTERACTIVE, FALLING_JEWELS, SWAPPING_JEWELS };

class Board
{
public:
Board();
Board(sf::Randomizer rand);
~Board();

int TileSize() { return tileSize; }
int CurrentlyMovingPieces;
sf::Vector2f TopLeft;

protected:
int tileSize;
BoardState boardState;
bool isActive;
bool canSwap;

sf::Sprite pieceSet;
sf::Sprite pieceHighlight;
sf::Sprite selectionSprite;
sf::Sprite bonusBarSprite;

//Piece pieceMap[BOARDSIZE][BOARDSIZE];
int mouseTileX, mouseTileY;
//Piece highlighedPiece;
float hilightAlpha;
int currenltyMOvingJewels;
float delayDrop;

float swapAlpha;
//SwapInfo swappingInfo;
bool validateSwap;

bool needToDropJewels;

sf::Randomizer rand;

//std::list<Piece> pieceRemains;
//std::list<Piece> hintPieces;
float hintTimer;
float hintDelay;

int baseScore;
int bonusLevel;
int removedPieces;
float removedPieceCounter;
int nextLevelLimit;
int totalRemovedPieces;
bool aboutToChangeLevel;
int longestChain;

float bonusFlashAlpha;

float timeLeft;
float maxTimeLeft;
float timePerJewel;
float timeReductionSpeed;
float timeLeftCounter;
float noMovesAlpha;

bool populatingPieces;
bool populatePieceCounterX;
bool populatePieceCounterY;
int populatePieceCursorDir;
float delayPerPiece;
};

#endif


and here is my Piece.h file:
Code: [Select]

#ifndef PIECE_H
#define PIECE_H

#include <SFML/Graphics.hpp>
#include "Board.h"

enum Direction { NONE, UP, DOWN, LEFT, RIGHT };
enum Type { YELLOW = 0, BLUE, CYAN, GREEN };

class Piece
{
public:
Piece();
Piece(Board parent);
Piece(Board parent, int x, int y, Type type);

void StartMoving(Direction dir, int howManySlots);
void StartMoving(Direction dir);
void Die();

void Update(float elapsedTime);
void Draw(sf::RenderWindow &App, sf::Color color, sf::Sprite sprite);

private:
sf::Vector2f position;
Type pieceType;
Board parentBoard;

bool moving;
int movedSlots;
bool reportedMove;
Direction movingDir;
float movingCounter;
sf::Vector2f targetPosition;
sf::Vector2f startPosition;
sf::Vector2f moveDirVector;
float movingAccel;

bool isDying;
float deathCounter;
bool readyToGo;
};

#endif


If I uncomment the Piece highlightedPiece in the Board.h file, or add the line:
Code: [Select]
#include "Piece.h" to my Board.H file, I get a ton of compiler errors, I don't know how to fix.

Can anyone help me?

Here is the compiler log when I added
Code: [Select]
#include "Piece.h" and uncommented the Piece highlitedPiece line in my Board.h file:

Quote
1>------ Build started: Project: TreasureHunt, Configuration: Debug Win32 ------
1>Compiling...
1>Board.cpp
1>c:\dev\treasurehunt\treasurehunt\src\piece.h(14) : error C2061: syntax error : identifier 'Board'
1>c:\dev\treasurehunt\treasurehunt\src\piece.h(14) : error C2535: 'Piece::Piece(void)' : member function already defined or declared
1>        c:\dev\treasurehunt\treasurehunt\src\piece.h(13) : see declaration of 'Piece::Piece'
1>c:\dev\treasurehunt\treasurehunt\src\piece.h(15) : error C2061: syntax error : identifier 'Board'
1>c:\dev\treasurehunt\treasurehunt\src\piece.h(15) : error C2535: 'Piece::Piece(void)' : member function already defined or declared
1>        c:\dev\treasurehunt\treasurehunt\src\piece.h(13) : see declaration of 'Piece::Piece'
1>c:\dev\treasurehunt\treasurehunt\src\piece.h(27) : error C2146: syntax error : missing ';' before identifier 'parentBoard'
1>c:\dev\treasurehunt\treasurehunt\src\piece.h(27) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\dev\treasurehunt\treasurehunt\src\piece.h(27) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\dev\treasurehunt\treasurehunt\src\board.cpp(10) : warning C4482: nonstandard extension used: enum 'BoardState' used in qualified name
1>Main.cpp
1>c:\dev\treasurehunt\treasurehunt\src\piece.h(14) : error C2061: syntax error : identifier 'Board'
1>c:\dev\treasurehunt\treasurehunt\src\piece.h(14) : error C2535: 'Piece::Piece(void)' : member function already defined or declared
1>        c:\dev\treasurehunt\treasurehunt\src\piece.h(13) : see declaration of 'Piece::Piece'
1>c:\dev\treasurehunt\treasurehunt\src\piece.h(15) : error C2061: syntax error : identifier 'Board'
1>c:\dev\treasurehunt\treasurehunt\src\piece.h(15) : error C2535: 'Piece::Piece(void)' : member function already defined or declared
1>        c:\dev\treasurehunt\treasurehunt\src\piece.h(13) : see declaration of 'Piece::Piece'
1>c:\dev\treasurehunt\treasurehunt\src\piece.h(27) : error C2146: syntax error : missing ';' before identifier 'parentBoard'
1>c:\dev\treasurehunt\treasurehunt\src\piece.h(27) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\dev\treasurehunt\treasurehunt\src\piece.h(27) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>Piece.cpp
1>c:\dev\treasurehunt\treasurehunt\src\board.h(39) : error C2146: syntax error : missing ';' before identifier 'highlighedPiece'
1>c:\dev\treasurehunt\treasurehunt\src\board.h(39) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\dev\treasurehunt\treasurehunt\src\board.h(39) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\dev\treasurehunt\treasurehunt\src\piece.cpp(27) : warning C4482: nonstandard extension used: enum 'Direction' used in qualified name
1>c:\dev\treasurehunt\treasurehunt\src\piece.cpp(37) : warning C4482: nonstandard extension used: enum 'Direction' used in qualified name
1>c:\dev\treasurehunt\treasurehunt\src\piece.cpp(40) : warning C4482: nonstandard extension used: enum 'Direction' used in qualified name
1>c:\dev\treasurehunt\treasurehunt\src\piece.cpp(43) : warning C4482: nonstandard extension used: enum 'Direction' used in qualified name
1>c:\dev\treasurehunt\treasurehunt\src\piece.cpp(46) : warning C4482: nonstandard extension used: enum 'Direction' used in qualified name
1>Generating Code...
1>Build log was saved at "file://c:\Dev\TreasureHunt\TreasureHunt\Obj\Debug\BuildLog.htm"
1>TreasureHunt - 17 error(s), 6 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


If anyone could help me that would be great!
Thanks :)

JollyRoger

  • Newbie
  • *
  • Posts: 17
    • View Profile
[C++] Help with classes
« Reply #1 on: January 15, 2010, 02:42:40 am »
What happens when you try to include "Piece.h" in your "Board.h" file, is that both files are trying to include each other at the same time.  This is called a circular dependency.  It's basically a chicken or the egg scenario.  To fix this, instead of including "Piece.h", add
Code: [Select]
class Piece;
at the top of your "Board.h".  This is called a forward declaration.  It tells the compiler that you have a class named Piece, but it doesn't actually need to know the contents of Piece in the header file.  Then include "Piece.h" wherever you have your implementation of Board's functions (probably "Board.cpp").

YellowShadow

  • Newbie
  • *
  • Posts: 29
    • AOL Instant Messenger - ehwatsupdoc21
    • View Profile
[C++] Help with classes
« Reply #2 on: January 15, 2010, 03:47:18 am »
I tried that, but I got some new errors:

Quote

1>------ Build started: Project: TreasureHunt, Configuration: Debug Win32 ------
1>Compiling...
1>Board.cpp
1>c:\dev\treasurehunt\treasurehunt\src\board.h(39) : error C2079: 'Board::highlighedPiece' uses undefined class 'Piece'
1>c:\dev\treasurehunt\treasurehunt\src\board.cpp(11) : warning C4482: nonstandard extension used: enum 'BoardState' used in qualified name
1>Main.cpp
1>c:\dev\treasurehunt\treasurehunt\src\board.h(39) : error C2079: 'Board::highlighedPiece' uses undefined class 'Piece'
1>Piece.cpp
1>c:\dev\treasurehunt\treasurehunt\src\board.h(39) : error C2079: 'Board::highlighedPiece' uses undefined class 'Piece'
1>c:\dev\treasurehunt\treasurehunt\src\piece.cpp(27) : warning C4482: nonstandard extension used: enum 'Direction' used in qualified name
1>c:\dev\treasurehunt\treasurehunt\src\piece.cpp(37) : warning C4482: nonstandard extension used: enum 'Direction' used in qualified name
1>c:\dev\treasurehunt\treasurehunt\src\piece.cpp(40) : warning C4482: nonstandard extension used: enum 'Direction' used in qualified name
1>c:\dev\treasurehunt\treasurehunt\src\piece.cpp(43) : warning C4482: nonstandard extension used: enum 'Direction' used in qualified name
1>c:\dev\treasurehunt\treasurehunt\src\piece.cpp(46) : warning C4482: nonstandard extension used: enum 'Direction' used in qualified name
1>Generating Code...
1>Build log was saved at "file://c:\Dev\TreasureHunt\TreasureHunt\Obj\Debug\BuildLog.htm"
1>TreasureHunt - 3 error(s), 6 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


How come it says Piece is undefined?

JollyRoger

  • Newbie
  • *
  • Posts: 17
    • View Profile
[C++] Help with classes
« Reply #3 on: January 15, 2010, 05:15:20 am »
Where did you put the "class Piece;"?

YellowShadow

  • Newbie
  • *
  • Posts: 29
    • AOL Instant Messenger - ehwatsupdoc21
    • View Profile
[C++] Help with classes
« Reply #4 on: January 15, 2010, 06:41:00 am »
Right after the includes in the Board.h file.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[C++] Help with classes
« Reply #5 on: January 15, 2010, 08:30:04 am »
After a forward declaration, as the compiler doesn't know the size and contents of the classe, you can only declare a pointer or reference to that class. Declaring an instance or using it requires the class header to be included.

The problem here is that highlighedPiece should be a pointer to a piece belonging to pieceMap, not a separate instance.

Same for parentBoard in Piece: it should be a pointer (or reference) to the actual bord, not a separate copy of it.

This way you can forward-declare Board in Piece.h, which breaks the circular dependency. You still have to include Piece.h in Board.h, because you declare a static array of Pieces.
Laurent Gomila - SFML developer