我有一个Board类,其中构造函数将板的尺寸作为参数.我还有一个Puzzle包含片段的类,我希望它有一个Board作为数据成员.我希望它像这样,所以当我创建一个实例时Puzzle,我将Board创建我的实例,所以我不必作为用户创建单独的实例.但是,当我在我的Puzzle.h文件中声明该板时,它需要Board构造函数的实际数字:
// Puzzle.h file
private:
Board theBoard(int height, int width); // Yells at me for not having numbers
Run Code Online (Sandbox Code Playgroud)
如果尚未创建该对象,是否有办法让类的对象成为另一个类的数据成员?
如果我理解正确,问题是您需要正确实例化您的电路板:
class Puzzle {
public:
Board theBoard;
Puzzle(int height, int width) : theBoard(height, width) // Pass this into the constructor here...
{
};
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
285 次 |
| 最近记录: |