-1

I have a constructor that looks like:

scacfg(const QString &caption, QWidget *parent);

scacfg::scacfg(const QString &caption = "Click Me", QWidget *parent = nullptr) : QWidget(parent)
               checkBox(new QCheckBox("Enabled")),
               button(new QPushButton(caption)) {....}

and I'm trying to call it like this:

QStringList groups = sttngs->childGroups();
foreach (const QString grp, groups) {
    scacfg *config = new scacfg(grp);

But the compiler complains about error: no matching function for call to ‘scacfg::scacfg(const QString&)’ scacfg *config = new scacfg(grp);

and I'm not sure why this is as defaults are provided in the prototype. If someone could explain, how I best resolve this, that would be appreciated!

stdcerr
  • 13,725
  • 25
  • 71
  • 128
  • 1) change to `foreach (const QString & grp, groups)` – eyllanesc Aug 07 '19 at 04:25
  • 1
    2) change to `scacfg(const QString &caption = "Click Me", QWidget *parent = nullptr)` in .h – eyllanesc Aug 07 '19 at 04:30
  • 1
    3) change `scacfg::scacfg(const QString &caption, QWidget *parent): ....` in .cpp – eyllanesc Aug 07 '19 at 04:31
  • 1
    The default values are used in the declaration of the methods or functions, and not in the implementation, you have done it the other way around so you should have an error there too but it seems strange that you do not mention it, another typo? – eyllanesc Aug 07 '19 at 04:32
  • @eyllanesc lol, I found that question too and was editing just before you marked as a dupe. –  Aug 07 '19 at 04:44
  • I didn't know that the problem I stated was due to the mixup in the default parameters. The `gcc` error didn't really suggest that... Thanks for pointing it out in any case! :) – stdcerr Aug 07 '19 at 04:46
  • @Chipster These types of problems are typical when you are a beginner and it is presumably that you have already had that problem and have received a lot of help so instead of responding leave a comment for the OP to solve your problem and I started looking for the duplicate, maybe you could do the same on the following occasions :-) – eyllanesc Aug 07 '19 at 04:46
  • @stdcerr gcc in C++? I recommend you migrate to g++ or clang – eyllanesc Aug 07 '19 at 04:47
  • @eyllanesc Noted. I'll keep that in mind. –  Aug 07 '19 at 04:49
  • @eyllanesc I'm trying to say I'm not the OP, which is what I think the comment `In addition to taking a C++ and OOP course` was meant for, as it logically follows `@stdcerr gcc in C++? I recommend you migrate to g++ or clang` and not `@Chipster These types of problems are typical when you are a beginner and it is presumably that you have already had that problem and have received a lot of help...` –  Aug 07 '19 at 04:58
  • @eyllanesc I'm using a good IDE, it's a great allrounder and called `vim`, I'll stick with it (I'm open to hearing about great plugins though!) – stdcerr Aug 07 '19 at 04:59
  • @Chipster my bad.... it's the fault of the dream :-) – eyllanesc Aug 07 '19 at 05:01
  • @stdcerr VIM is not an IDE, it is a Text Editor. – eyllanesc Aug 07 '19 at 05:01

1 Answers1

1

You're using default arguments wrong. As stated by Geeks for Geeks (emphasis mine):

A default argument is a value provided in a function declaration that is automatically assigned by the compiler if the caller of the function doesn’t provide a value for the argument with a default value.

In other words, you have the default part in the wrong place. Try this instead:

scacfg(const QString &caption = "Click Me", QWidget *parent = nullptr);

scacfg::scacfg(const QString &caption, QWidget *parent) : QWidget(parent)
               checkBox(new QCheckBox("Enabled")),
               button(new QPushButton(caption)) {....}


Note: after doing more research, it seems you can actually put it in either, but particularly, if you put the implementation in a different file, you will run into this type of problem.