简体   繁体   English

当已经定义了默认参数时,调用不带参数的函数

[英]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: 我用C ++编写已经很长时间了,我无法在网上找到问题的解决方案,因此这个问题:

I have a class where I want to define a function with a default parameter :Run(par="default") 我有一个类,我想用默认参数定义一个函数:Run(par =“default”)

class.h contains: class.h包含:

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

class.cpp contains: class.cpp包含:

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(). 我得到的问题是,当我尝试调用此函数时,没有参数(我想使用默认值),编译器抱怨函数run()的不存在。

Test test;
test.Run()

7: error: undefined reference to `Test::Run()' 7:错误:未定义引用`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. 我试图仅在.cpp中删除默认值,但编译器错误仍然存​​在。

What do I forget here? 我在这忘记了什么? I am compiling in QT using GCC. 我正在使用GCC编译QT。

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 应该没事

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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