-2

I want to design a function so that the parameter is optional and default constructed if left out. The parameter is a struct that I've defined in the same header file. The tried to use the following function declaration:

void publish(MarkerStyle style=MarkerStyle());

When I try to compile I get the following error message:

error: no matching function for call to ‘bob::MarkerStyle::MarkerStyle(bob::MarkerStyle)’
  void publish(MarkerStyle style=MarkerStyle());

Is there some way to invoke the default constructor of MarkerStyle as an optional parameter?

EDIT:

Here is my header file.

struct MarkerStyle()
{
    double alpha;
    std::auto_ptr<Color> color;

    MarkerStyle() :
    alpha(100) 
    {}
};

class Publisher
{
   void publish(MakerStyle style=MarkerStyle());
};

I understand Bo Perreson's answer. Now my question is why doesn't my code generate a copy constructor for MarkerStyle?

EDIT:

I figured out the problem. auto_ptr prevented copy constructor generation.

user2445507
  • 443
  • 3
  • 13

1 Answers1

2

If you pass MarkerStyle by value, it will get copied and you need a copy constructor.

If you don't want or need a copy, pass it by const reference, like

void publish(MarkerStyle const& style=MarkerStyle());
Bo Persson
  • 90,663
  • 31
  • 146
  • 203
  • 2
    Although there isn't anything to suggest the class is not copy-constructable. – juanchopanza Oct 22 '15 at 19:59
  • Thanks. I didn't realize the copy constructor would be invoked. I made an edit. If you look at my header, can you see why there would not be an auto-generated copy-constructor? I may go with const reference anyways. – user2445507 Oct 22 '15 at 20:03
  • I realize now that auto_ptr prevented copy constructor generation. Or at least I think that's it. Thanks – user2445507 Oct 22 '15 at 20:06