1

私は C++ の初心者で、ClanLib の CL_String8 関数でエラーが発生しています。

コンパイルしようとすると:

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());
}

このエラーが発生します(3回繰り返されます):

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>&}’

どの関数を使用したいかを定義するにはどうすればよいですか?

4

3 に答える 3

2

ドキュメントによると、関数 'get_hour()' などは単一の符号なし文字を返します。これは、呼び出しに一致する関数テンプレートがないことを意味します。

リンゴ、サクランボ、またはバナナ用に作成された関数にナシを与えているようなものです。

あなたの問題を解決するには、C 関数 'sprintf' を使用し、CL_String8 関数を介して THAT を実行するか、std::string を使用することをお勧めします。

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);

これにより、hh:mm:ss の形式でデータが返され、9 未満の値はすべて 0 になります。

于 2012-09-17T18:52:41.613 に答える
2

ClanLib には、低レベルの C フォーマット コードを回避するために使用できるフォーマット関数があります。

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ほとんどの基本的な型を自動的に受け入れます。

次に、時刻をhh:mm:ssとして返す があるCL_DateTimeため、特定のフォーマットが必要でない限り、それを使用できます。to_long_time_string()

return now.to_long_time_string();

CL_String8第三に、直接使用する必要はありません。Unicode コンパイル設定に応じて、または内部的CL_Stringに使用されます。コードで明示的に使用する必要があるものではありません。CL_String8CL_String16CL_String8

于 2012-09-17T19:06:57.790 に答える
1

now.get_minutes() などは unsigned char を返していますが、CL_String8 にはそのようなパラメーターを取るオーバーロードされたバージョンはないと確信しています。

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();
}
于 2012-09-17T18:54:00.933 に答える