Game Development Reference
In-Depth Information
There are several implications to this error:
All classes must have a constructor.
The compiler will create a default constructor if you do not specify one. This
default constructor calls the default constructors of all member functions if they
have them. Built-in types such as int do not have default constructors and are
therefore uninitialized. Default constructors do not take any parameters.
The compiler will not generate a default constructor if you specify a constructor.
Our class no longer has a default constructor once we specified the constructor that takes the
string parameter, which means that we cannot create a class without passing a string to player .
The following line is the offending code:
Player player;
You can fix this in two ways, either by passing a string parameter:
Player player("defaultName");
or by adding your own default constructor to the Player class, as shown in Listing 8-6.
Listing 8-6. Adding a Default Constructor to Player
class Player
{
private:
std::string m_name;
public:
Player()
{
}
Player(const std::string& name)
: m_name(name)
{
}
void SetName(const std::string& name)
{
m_name = name;
}
const std::string& GetName() const
{
return m_name;
}
};
 
Search WWH ::




Custom Search