Tim Mierzejewski wrote:
I'm having trouble assigning a string to a character array. Here's the
applicable code:
#include <iostream>
#include "Creature.hpp" // Includes Creature class
using namespace std;
char CreatureName[16];
void EarthElemental(Creature &Player);
void Slime(Creature &Player);
int main()
{
Creature *Play1 = new Creature
int Selection;
cin << Selection;
switch (Selection)
{
case (1):
EarthElemental(*Play1);
break;
case (2):
Slime(*Play1);
break;
}
return 0;
}
FYI: Parenthesis are not required for the case label.
Use "case 1:" instead of "case (1):".
FYI: Don't use "magic numbers", prefere named constants:
const unsigned int EARTH_ELEMENTAL_ID = 1;
const unsigned int SLIME_ID = 2;
//...
case EARTH_ELEMENTAL_ID:
void EarthElemental(Creature &Player)
{
Player->SetValue(2);
CreatureName = "Earth Elemental"; // Line 247
}
void Slime(Creature &Player)
{
Player->SetValue(3);
CreatureName = "Slime"; // Line 254
}
--
The errors I get are:
(247) : error C2106: '=' : left operand must be l-value
(254) : error C2440: '=' : cannot convert from 'char [6]' to 'char [16]'
--
Sorry for bugging you again with a probably simple question, but I'm really
new at this. Thanks.
Tim M.
Arrays cannot be copied using the assignment operator.
Since you are dealing with text, I suggest you use the std::string
type:
#include <string>
using std::string
string CreatureName;
void EarthElemental(Creature &Player)
{
Player->SetValue(2);
CreatureName = "Earth Elemental";
// or to be explicit:
// CreatureName = string("Earth Elemental")'
}
By the way, you may want to read up on polymorphism.
class Creature
{
protected:
string name_; // Every creature has a name.
public:
void print_name(ostream& out) const
{ out << name_ << endl;}
};
class EarthElemental
: public Creature
{
public:
EarthElemental()
{name_ = "Earth Elemental"};
};
class Slime
: public Creature
{
public:
Slime()
{name_ = "Slime";}
};
int main(void)
{
Creature * Player1(NULL);
// The following code is one implementation of
// of the Factory design pattern. The Factory
// creates a creature based on the User's selection.
unsigned int Selection;
cin << Selection;
switch (Selection)
{
case 1:
// Set Player 1 to an Earth Elemental.
Player1 = new EarthElemental
break;
case 2:
// Set Player 1 to a Slime.
Player1 = new Slime;
break;
}
// Test the factory.
if (Player1 != NULL)
Player1->print_name(cout);
else
cout << "Invalid selection: " << Selection << endl;
return 0;
}
In the above example, every creature has-a name.
name is an attribute of the base class Creature.
There is also a method for printing out the name.
So, after a creature is created, the name is printed
out regardless of which instance was created by the
factory. The print method is a method of the base
class and doesn't depend on the type of the child
classes.
The base class Creature should contain members and
methods common to all creatures.
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq:
http://www.parashift.com/c++-faq-lite
C Faq:
http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book