Advertisement

About to Pull my hair out. Lot's of code posted. Anyone help???

Started by September 03, 2002 10:39 PM
9 comments, last by Radagar 22 years, 2 months ago
I''m working on a Console Arena game, and I''ve run into a problem... I have these files in my project.

CItem.H        CItem.CPP
CCreature.H    CCreature.CPP 
Globals.H      Globals.CPP
BM.H           BM.CPP
EQ_List.H      EQ_List.CPP
Function.H     Function.CPP
 
The CItem Files are the ones causing me problems. Class CItem is a BASE class with CWeapon, CArmor, and CShield derived from it. For now I''m just working with CWeapon. You can ignore the other two. I know the code below isn''t the most efficient way to do what I''m doing, but I want to learn it this way for now. So bare with me. CItem and CWeapon Declaration
  
class CItem
{
public:
	CItem();
	CItem(char* a_name, int a_weight, int a_value);    
	char* name;
	int weight;  
	int value;
};
 

class CWeapon : public CItem
{  
public:
	CWeapon();
	CWeapon(char* a_name, int a_weight, int a_value, int a_dmg, int a_chancetohit);  
	int dmg;  
	int chancetohit;  
};

//

//AND THE FUNCTIONS FROM THE .CPP

//


CItem::CItem(char* a_name, int a_weight, int a_value)
{  
	name = a_name; 
	weight = a_weight; 
	value = a_value;
}

CItem::CItem()
{}

CWeapon::CWeapon()
{}

CWeapon::CWeapon(char* a_name, int a_weight, int a_value, int a_dmg, int a_chancetohit)
{  
	CItem::CItem(a_name, a_weight, a_value);  
	dmg = a_dmg; 
	chancetohit = a_chancetohit;
}
  
Now for the Problem... I''m wanting to Initialize my Weapons from a list, and I have it setup like this. This is from EQ_List.H and EQ_List.CPP
  
//EQ_LIST.H

//Defines Extern Global

#include "globals.h"

extern CWeapon	*WeaponList;
//END


//EQ_LIST.CPP

//Sets up the Equipment Lists

#include "CCreature.h"
#include "CItem.h"
#include "eq_list.h"


CWeapon *WeaponList = new CWeapon[5];
WeaponList[0] = CWeapon("Rusty Dagger",			1,	1,	3,	0);
WeaponList[1] = CWeapon("Dagger",				1,	5,	4,	0);
WeaponList[2] = CWeapon("Chipped Shortsword",	3,	10,	5,	0);
WeaponList[3] = CWeapon("Shortsword",			3,	25,	6,	0);
WeaponList[4] = CWeapon("Tarnished Longsword",	5,	50,	7,	0);

delete [] WeaponList; //Wouldn''t normally be here, but I''m just testing.

  
Now, when I try to compile this with the rest of my program, I get a bunch of errors. See Below...

C:\My Documents\Cpp\Battle Masters\eq_list.cpp(12) : error C2466: cannot allocate an array of constant size 0
C:\My Documents\Cpp\Battle Masters\eq_list.cpp(12) : error C2501: ''WeaponList'' : missing storage-class or type specifiers
C:\My Documents\Cpp\Battle Masters\eq_list.cpp(12) : error C2372: ''WeaponList'' : redefinition; different types of indirection
        c:\my documents\cpp\battle masters\eq_list.h(7) : see declaration of ''WeaponList''
C:\My Documents\Cpp\Battle Masters\eq_list.cpp(12) : error C2440: ''initializing'' : cannot convert from ''class CWeapon'' to ''int [1]''
        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
C:\My Documents\Cpp\Battle Masters\eq_list.cpp(14) : error C2466: cannot allocate an array of constant size 0
 
There are more, but they are mostly repeats of this. The one that REALLY throws me is the first. "Cannot allocate an array of constant size 0"... I looked this up on MSDN and for some reason it thinks that WeaponList[0] = .... is trying to redefine an array of size zero, when actually I''m trying to set array member 0 to the Cweapon as above.. Gah, I hope this at least makes a little sense. I set this up in a different file and it compiled but when I went back to my original program and went through it line by line it just doesn''t work.... Any Suggestions? ~~~~~~~~~~~ Chris Vogel ~~~~~~~~~~~
WyrmSlayer RPG - In Early Development
fer your ferst one, it needs to be in a function... like void InitWeaponList()
If you were MEANT to understand it, we wouldn't have called it 'code'
Advertisement
fer your ferst one, it needs to be in a function... like void InitWeaponList()
If you were MEANT to understand it, we wouldn't have called it 'code'
i believe the following lines should be placed into a function. They cannot be outside a function. May be just put them in some initilaze method. Hope it helps.

WeaponList[0] = CWeapon("Rusty Dagger", 1, 1, 3, 0);
WeaponList[1] = CWeapon("Dagger", 1, 5, 4, 0);
WeaponList[2] = CWeapon("Chipped Shortsword", 3, 10, 5, 0);
WeaponList[3] = CWeapon("Shortsword", 3, 25, 6, 0);
WeaponList[4] = CWeapon("Tarnished Longsword", 5, 50, 7, 0);
oops sorry Baloogan, i should have hit refresh before i posted a reply.
GAHH!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!11

Ok, I''m going to bed now =(

I''ve been fighting with that ALL DAY!

*pounds forehead on desk*



~~~~~~~~~~~
Chris Vogel
~~~~~~~~~~~
WyrmSlayer RPG - In Early Development
Advertisement
I do actually have another question though.

As is probably painfully obvious, I''m new to C++ classes and creating items using ''new'' and ''delete''. Where would I actually Delete these objects normally? Just in a function before my program closes?

~~~~~~~~~~~
Chris Vogel
~~~~~~~~~~~
WyrmSlayer RPG - In Early Development
you have the code declared
extern CWeapon *WeaponsList;
then you have
CWeapon *WeaponsList = new CWeapon(...)

you ARE trying to re-declare the Array Size, eather re-name it or
do this

  extern CWeapon *WeaponsList;if(WeaponsList) delete [] WeaponsList;WeaponsList = NULL;WeaponsList = new CWeapon[5];...  

got it?
Declare "extern CWeapon" AFTER you Include "CItem.h"

k, that should fix it ALL up
good luck
contact me if it still aint workn'
/silvermace

[edited by - silvermace on September 3, 2002 12:09:38 AM]
"I am a donut! Ask not how many tris/batch, but rather how many batches/frame!" -- Matthias Wloka & Richard Huddy, (GDC, DirectX 9 Performance)

http://www.silvermace.com/ -- My personal website
Silvermace,

WeaponList was defined as EXTERN in my global file so that all parts of my code could access it. the EXTERN keyword tells it that the variable will be defined later outside of the current file.

so...


  //Global.hextern CWeapon* WeaponList;;;//Global.cppCWeapon WeaponList = new CWeapon[TOTALWEAPS];  


Is perfectly valid.

I am very new to this however, and thats how I think it works. If I''m wrong, inform me.

But this code is working now that I stuck it inside a function. Can''t believe I missed that =)



~~~~~~~~~~~
Chris Vogel
~~~~~~~~~~~
WyrmSlayer RPG - In Early Development

    // I believe the problem is with how you have declared your array of items and how you initialize that list.// 1st) Create an assignment operator within your base andderived class. You can not perform the assignment as you havethem stated below. This will not work as it is unknown how totreat the type CWeapon. There is no conversion operator definedto perform that action.// exampleCItem& CItem::operator=(const CItem& Item){    weight = Item.weight;    value = Item.value;    // Assuming that you are dynamically allocating space forthis stored string, and that you are going to use standard CLibraries for string manipulation, and you want a deep copy ofthe data.    // Also recommend you switch over to using the standardstring class std::string.    if( name != 0 )    {        delete [] name;    }    name = new char[strlen(Item.name)];    strcpy(name, Item.name);    return (*this);}or// 2nd) Create a method called Create which will take in the sameparameters as the class constructor. Then, instead of doingsomething like WeaponList[0] = CWeapon(yada, yada, yada)you would do WeaponList[0]->Create(yada, yada, yada);// 3rd) I would recommend you read up on how to use standard template containers, ie std::map, std::vector or std::list. CWeapon *WeaponList = new CWeapon[5];WeaponList[0] = CWeapon("Rusty Dagger",			1,	1,	3,	0);WeaponList[1] = CWeapon("Dagger",				1,	5,	4,	0);WeaponList[2] = CWeapon("Chipped Shortsword",	3,	10,	5,	0);WeaponList[3] = CWeapon("Shortsword",			3,	25,	6,	0);WeaponList[4] = CWeapon("Tarnished Longsword",	5,	50,	7,	0);    


good luck, not sure if I helped.

[edited by - AlekM on September 3, 2002 12:20:34 AM]

This topic is closed to new replies.

Advertisement