tehtros
tehtros

Reputation: 179

How do I define which function to use?

I am pretty new to C++, and I am having an error with the CL_String8 function in ClanLib.

When I try to compile:

CL_String now() {
    CL_DateTime now = CL_DateTime::get_current_local_time();
    return CL_String8(now.get_hour()) + " " + CL_String8(now.get_minutes()) + " " + CL_String8(now.get_seconds());
}

I get this error (repeated 3 times):

src/utils.cpp: In function ‘CL_String now()’:
src/utils.cpp:15:34: error: call of overloaded ‘CL_String8(unsigned char)’ is ambiguous
src/utils.cpp:15:34: note: candidates are:
/usr/include/ClanLib-2.3/ClanLib/Core/Text/string8.h:74:2: note: CL_String8::CL_String8(const wchar_t*) <near match>
/usr/include/ClanLib-2.3/ClanLib/Core/Text/string8.h:74:2: note:   no known conversion for argument 1 from ‘unsigned char’ to ‘const wchar_t*’
/usr/include/ClanLib-2.3/ClanLib/Core/Text/string8.h:63:2: note: CL_String8::CL_String8(const char*) <near match>
/usr/include/ClanLib-2.3/ClanLib/Core/Text/string8.h:63:2: note:   no known conversion for argument 1 from ‘unsigned char’ to ‘const char*’
/usr/include/ClanLib-2.3/ClanLib/Core/Text/string8.h:55:2: note: CL_String8::CL_String8(const CL_String8&) <near match>
/usr/include/ClanLib-2.3/ClanLib/Core/Text/string8.h:55:2: note:   no known conversion for argument 1 from ‘unsigned char’ to ‘const CL_String8&’
/usr/include/ClanLib-2.3/ClanLib/Core/Text/string8.h:50:2: note: CL_String8::CL_String8(const string&) <near match>
/usr/include/ClanLib-2.3/ClanLib/Core/Text/string8.h:50:2: note:   no known conversion for argument 1 from ‘unsigned char’ to ‘const string& {aka const std::basic_string<char>&}’

How can I define which one of the functions I want to use?

Upvotes: 1

Views: 196

Answers (3)

sphair
sphair

Reputation: 1670

ClanLib has a formatting function you can use to avoid low-level C formatting code:

CL_DateTime now = CL_DateTime::get_current_local_time();
return cl_format("%1 %2 %3", now.get_hour(), now.get_minutes(), now.get_seconds());

cl_format accepts most basic types automatically.

Secondly, CL_DateTime has a to_long_time_string() that returns the time as hh:mm:ss, so you could use that unless you need your specific formatting:

return now.to_long_time_string();

Third, there is no need to use CL_String8 directly. Depending on unicode compilation settings CL_String will either use CL_String8 or CL_String16 internally. CL_String8 is not something you need to use explicitly in your code.

Upvotes: 2

ATaylor
ATaylor

Reputation: 2598

According to documentation, the functions 'get_hour()', etc. return a single unsigned character, which in turn means, that none of the function templates match your call.

It's like the function is made for apples, cherries or bananas, and you're giving it pears.

To solve your problem, I suggest to either use the C-Function 'sprintf', and run THAT through the CL_String8-Function...or use std::string.

Solution using sprintf:

#include <stdio.h>
char Buffer[50];
sprintf(Buffer, "%02d %02d %02d", now.get_hour(), now.get_minutes(), now.get_seconds());
return CL_String8(Buffer);

This will return the data in the format hh:mm:ss, with 0 for all values lower than 9.

Upvotes: 2

user123
user123

Reputation: 9071

now.get_minutes() and the like are rerturning unsigned chars, and I'm sure that CL_String8 does not have any overloaded version that takes such a parameter.

Pass your values to this function before passing them to CL_String8

#include <sstream>
#include <string> // not sure if necessary

const char* unsignedChar2string(unsigned char c) {
    stringstream stream;
    stream << static_cast<char>(c); // I'm not 100% sure if the cast is necessary
                                    // I'm also not sure whether it should be static, dynamic, or reinterpret.
    return stream.str().c_str();
}

Upvotes: 1

Related Questions