我是 OOP 的新手,我不明白如何将参数传递给类。
类的声明:
class Soldier
{
Soldier(int SetHealth, int SetStrength);
private:
int health;
int strength;
public:
void attacked();
void healed();
int getHealth();
int getStrength();
};
构造函数的定义:
Soldier::Soldier(int SetHealth, int SetStrength):
health(SetHealth),
strength(SetStrength)
{
}
当我尝试将参数传递给类时,它会说:
1>------ Build started: Project: ConsoleApplication6, Configuration: Debug Win32 ------
1> ConsoleApplication6.cpp
1>c:\users\user\documents\visual studio 2012\projects\consoleapplication6\consoleapplication6\consoleapplication6.cpp(11): error C2248: 'Soldier::Soldier' : cannot access private member declared in class 'Soldier'
1> c:\users\user\documents\visual studio 2012\projects\consoleapplication6\consoleapplication6\soldier.h(7) : see declaration of 'Soldier::Soldier'
1> c:\users\user\documents\visual studio 2012\projects\consoleapplication6\consoleapplication6\soldier.h(6) : see declaration of 'Soldier'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========