Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error c2664: cannot convert from class X to char *, ?!

Status
Not open for further replies.

batian

MIS
Dec 10, 2001
2
US
I get this error...

error C2664: '__thiscall Warrior::Warrior(char *,int,int,char *,int)' : cannot convert parameter 1 from 'const class Character' to 'char *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

... on the code below and cannot trace any threads that indicate how to resolve this in the class contructor, if that's where it should be fixed. Any ideas? The relevant code is included below. I can provide the rest of it if needed.

//define base class
class Character
{
protected:

char * name;
int hit_pts;
int armor_class;

public:

Character(char* n="Anonymous", int h = 0, int a = 0);
Character(const Character&);
~Character(void);
virtual bool isalive(void);
virtual operator const char*(void) const;
virtual operator int(void);
//operator
Character& operator =(const Character&);
};

//define base class constructor
Character::Character(char* n, int intHitPts, int intArmorClass):hit_pts(intHitPts), armor_class(intArmorClass)
{
name = new char[strlen(n)+1];
strcpy(name, n);
}

//define base class copy constructor
Character::Character(const Character& Ch):hit_pts(Ch.hit_pts), armor_class(Ch.armor_class)
{
name = new char[strlen(Ch.name)+1];
strcpy(name,Ch.name);
}

//define base class destructor
Character::~Character(void)
{
delete []name;
}

//define member function. returns hit_pts value as true/false
bool Character::isalive(void)
{
return hit_pts>0?true:false;
}

//create cast operator to return object name
Character::eek:perator const char *(void) const
{
return name;
//return static_cast <Character> name;
}

//create cast operator to return armor_class
Character::eek:perator int(void)
{
return armor_class;
}

//create copy operator for class
Character& Character::eek:perator =( const Character& c)
{
if (&c!=this)
{
delete []name;
name = new char[strlen(c.name)+1];
strcpy(name, c.name);
hit_pts = c.hit_pts;
armor_class = c.armor_class;
}
return *this;
}

//create/define warrior class
class Warrior: virtual public Character
{
protected:
char * weapon;
int damage_pts;

public:
Warrior(char* n=&quot;Anonymous Warrior&quot;,int h=0, int a= 0, char* w=&quot;n/a&quot;, int d=0);
Warrior(const Warrior&);
//Warrior(const Character&);
Warrior& operator=(const Warrior&);
~Warrior(void);

int strikes(void);

};

//create warrior class constructor
Warrior::Warrior(char* n, int d, int a, char* w, int h)
:Character(n,h,a),weapon(w), damage_pts(d)
{
weapon = new char[strlen(w)+1];
strcpy(weapon,w);
}
 
I don't get the error in the code you provided. What compiler (version? service pack?) are you using?

Shyan
 
I'm using Microsoft Visual C++ 6.0
msvcrt.dll 6.00.8337.0
 
I have 6.00.8797.0. Do you have SP5 installed?

Shyan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top