Hey just wondering if someone could help me please with the following:

board.h

public:
Piece* Player1Pieces;
Piece* Player2Pieces;

(a section showing the initialization of the arrays)

board.C:

Player1Pieces = new Piece[18];
Player2Pieces = new Piece[18];

char arrX[9] = {'a','b','c','d','e','f','g','h','i'};

for (int FootCount = 0; FootCount < 9; ++FootCount)
{
--->>	Player1Pieces[FootCount] = new FootSoldier(1,arrX[XCount],1); <<---
Player2Pieces[FootCount] = new FootSoldier(2,arrX[XCount],7)

(this is where I initialize the objects to the specific type I want)

The error:
(between the arrows)

Piece.h:13: note: candidates are: Piece& Piece::operator=(const Piece&)
board.C:28: error: expected `;' before ‘FootSoldier’
board.C:29: error: expected type-specifier before ‘FootSoldier’
board.C:29: error: no match for ‘operator=’ in ‘*(((Board*)this)->Board::Player2Pieces + ((unsigned int)(((unsigned int)FootCount) * 24u))) = (int*)operator new(4u)’

The pointer to the array of objects is thus specified in the .h file, and initialized in the .C file, but I get these weird errors?


Any help would be very much appreciated!

Recommended Answers

All 10 Replies

sorry that smiley is not supposed to be there!

>>sorry that smiley is not supposed to be there!
Use the [noparse]...[/noparse] tag to prevent things like that.

Your error is that you are attempting to use "new" to create the objects stored in your Piece arrays. The return from new is a pointer to an object, not an actual object. It creates the object, then returns the memory address of the object. Looking at your earlier code, you seem to understand this already.

To correct this, you'll have to declare your arrays as double pointers (pointer-to-pointer) then adjust your new statements to accommodate the extra level of indirection.

Thanks for the reply. I'll take a look at it now thanks!

board.h

public:
Piece** Player1Pieces;
Piece** Player2Pieces;

board.C

Player1Pieces = new Piece*[18];
Player2Pieces = new Piece*[18];
 
char arrX[9] = {'a','b','c','d','e','f','g','h','i'};
 
for (int FootCount = 0; FootCount < 9; ++FootCount)
{
--->>	Player1Pieces[FootCount] = new FootSoldier(1,arrX[XCount],1); <<---
Player2Pieces[FootCount] = new FootSoldier(2,arrX[XCount],7)

this is how I edited the code, though I still get exactly the same error. Am I missing something?

Okay, a couple things I'm seeing now.

  1. I just noticed that the name of the file is board.C. This is a C file not a C++ file which changes the behavior of the compiler. This behavioral change means the compiler simply can't understand alot of this code. Change the file name to board.CPP instead.
  2. I also see now that you are trying to assign a FootSoldier* to a Piece*. This will only work under certain conditions. I would like to verify that you have created proper polymorphic classes. Please post the definition of the FootSoldier class.

Just to have you understand better, the base class is

Piece.h :

#ifndef PIECE_H
#define PIECE_H

#include "Coordinate.h"
#include "Reach.h"
#include "SpellCaster.h"

#include <iostream>

using namespace std;

class Piece : public Spellcaster
{
	protected:
		int Life;
		int Side;
		char Symbol;
		Reach* Movements;
	
	public:
		Piece();
        	Piece(int,int,char);
        	~Piece();
		int getLife();
		int getIntX();
		Coordinate* Current;
		friend ostream &operator << (ostream& , const Piece& );
		virtual void CastSpell();
};

#endif

Footsoldier.h:

#ifndef FOOTSOLDIER_H
#define FOOTSOLDIER_H
#include "Piece.h"

class FootSoldier : public Piece
{
	public:
		FootSoldier(int,char,int);
};

#endif

The 'CastSpell()' function is really the only aspect that changes in the derived classes.Some can cast spells and others can't(like the FootSoldier).

Hmmm... So, Piece inherits from SpellCaster and FootSoldier inherits from Piece... This makes every FootSoldier a SpellCaster. Is this your intent?

Obviously, I don't know everything about your program/structure, but from a logical standpoint, I think you may have the inheritance structure for Piece and SpellCaster backward. This could cause problems because under the current structure a SpellCaster* can hold either a Piece* or a FootSoldier*, but a Piece* can't hold a SpellCaster* without some trickery.

What do the constructors for Piece look like? I see the declarations, but I'd like to see the implementations.

I can see some future memory issues coming as well. You have a pointer data member, but you don't have custom copy constructor, destructor, or assignment operator.

Yeah the program is still in it's early stages, so there are a few things that can fall away, like the inheritance Piece has from Spellcaster and functions like getLife().Sorry for the inconvenience on that.

Footsoldier.C

#include "FootSoldier.h"
#include "Piece.h"

FootSoldier::FootSoldier(int _side,char _x,int _y) : Piece(6,_side,'F')
{
	Movements = new Reach(Symbol);
	Current->X = _x;
	Current->Y = _y;
}

Piece.C

Piece::Piece(int _life,int _side,char _symbol)
{
	Side = _side;
	Life = _life;
	Symbol = _symbol;
}

Hey, our specific error was resolved, the #include "FootSoldier.h" had a spelling error!
Thanks for your trouble! And sorry for the inconvenience! :D

As I mentioned earlier, you need to change your file names from *.c to *.cpp. There is a big difference in the file type and how the compiler interprets the contents of the file.

I don't see anything in those that should be cause for concern. I think if you fix that inheritance SNAFU, and fix your file names, it will go a long way toward correcting things.

EDIT:
Oops, overlap. Glad you figured it out..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.