Dalton Moore
Dalton Moore

Reputation: 69

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. The type is Button. I just want to construct a button object within this TransferTable class.

Upvotes: 1

Views: 110

Answers (2)

eerorika
eerorika

Reputation: 238291

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:

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

Or, if you cannot rely on C++11, you must use the member initialization list instead.

Upvotes: 4

ForemanBob
ForemanBob

Reputation: 138

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

  }
};

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

  }
private:
  Button a;
};

Upvotes: 1

Related Questions