简体   繁体   中英

Calling function without parameter, when default parameter is allready defined

It has been a long time since I wrote in C++, and I can't really find a solution to my problem to my question online, hence this question:

I have a class where I want to define a function with a default parameter :Run(par="default")

class.h contains:

class Test { public : void Run(QString par="default");};

class.cpp contains:

void Test::Run(QString par="default") { ... };

The issue I get is that when i try to call this function, with no paramater (I want to use the default value), the compiler complains about the nonexistance of a function run().

Test test;
test.Run()

7: error: undefined reference to `Test::Run()'

I would prefere not to use function overloading.

I tried to remove the default value only in the .cpp, but the compiler error remained.

What do I forget here? I am compiling in QT using GCC.

If you declare a default parameter, you only set it within your class declaration. In the definition, you leave it empty.

class Test { public : void Run(QString par="default");};
void Test::Run(QString par) { ... };

should be fine

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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