简体   繁体   中英

QT C++ function with optional parameter for strings

I'm having problem with the writing a toString function on QT C++.

For example, I have this in my header file QString toString(QString delimiter = " : "); and I get the following error

error: default argument for parameter of type 'QString' has type 'const char [3]'

The otpional parameters worked fine for other types, but it seems that the compiler interprets " : " as type char and as such gives me the following error (i thought chars use ' ', turns out i was wrong).

I googled this stuff, but couldn't find anything useless, i'm pretty there must be an easy fix to it, but I'm new to the language.

Thank you

The compiler error is saying that the paramater is of type QString but the default value you've given is of type const char[3] (a const character array) because it is a string literal. QString does have a constructor that takes a const char* (and remember arrays decay to pointers) so normally it would be able to perform an implicit conversion to from const char* to QString however it seems that constructor has been declared explicit and so it must be called explicitly.

Edit: It's not explicit, the header just needs to be included :)

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