简体   繁体   English

在我的C ++类中创建对象

[英]Creating an object within my class c++

class Button {
public:
    Button(int pin, int debounce)
    {
    }
};
class TransferTable {
private:
    Button a(1, 1);
public:
    TransferTable()
    {
    }
};

The above code gives me an error of "expected identifier before numeric constant" in reference to the "Button a(1,1)" line. 上面的代码给我一个关于“按钮a(1,1)”行的“数值常数之前的预期标识符”的错误。 The type is Button. 类型是按钮。 I just want to construct a button object within this TransferTable class. 我只想在此TransferTable类中构造一个按钮对象。

The default member initializer syntax requires the use of curly braces: 默认成员初始化器语法要求使用花括号:

private:
Button a{1,1};

Or, you can use the "equal-syntax" to do the same thing, as pointed out by juanchopanza: 或者,如juanchopanza所指出的,您可以使用“等于语法”执行相同的操作:

private:
Button a = Button(1, 2);

Or, if you cannot rely on C++11, you must use the member initialization list instead. 或者,如果您不能依赖C ++ 11,则必须改用成员初始化列表。

class Button
{
public:
  Button(int pin, int debounce)
  {

  }
};

class TransferTable
{
public:
  TransferTable() : a(1, 1)
  {

  }
private:
  Button a;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM