1

Still have the error even with a default constructor.

class Foo {
    public:
    Foo ( int x, int y, int type );
}

And in the .cpp file

Foo::Foo ( int x = 0, int y = 0, int type = 0 ) {

And yet, when I call it

Foo foo_array[5][5];

I get the error. Any reason why that may be?

James Hurley
  • 833
  • 1
  • 10
  • 33
  • 2
    see: http://stackoverflow.com/questions/13713916/am-i-using-default-arguments-incorrectly/13713944#13713944 – billz Feb 20 '13 at 23:31
  • 1
    if you use a `std::vector` instead of raw array, then you can specify a default value so that you don't need to have an otherwise unnecessary (and perhaps not very meaningful) default constructor. – Cheers and hth. - Alf Feb 20 '13 at 23:33

1 Answers1

6

Put the default arguments in the declaration of constructor. As it is, the compiler doesn't know about them when you try to create the array.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
  • How would you do that with arrays? I thought you couldn't. – James Hurley Feb 20 '13 at 23:36
  • 1
    @JimHurley You don't need to do anything to the array - change the declaration of the constructor. – Joseph Mansfield Feb 20 '13 at 23:43
  • I'm sorry, but can you give me an example on how to do this? I thought that the ctor was the where I was doing it. – James Hurley Feb 21 '13 at 00:03
  • 1
    Change the declaration in the header from `Foo(int x, int y, int type);` to `Foo(int x = 0, int y = 0, int type = 0);`. You also have to **remove** the default arguments from the .cpp file; the compiler will remind you if you forget. – Pete Becker Feb 21 '13 at 12:11