ページ 1 / 1
STL文字列の変換について
Posted: 2018年2月03日(土) 20:28
by Yakiniku
いつもお世話になっております。
DXライブラリで通信プログラムを作成していますが、jsonファイルを文字列に変換できず質問しました。
参考にしているプログラムは(
http://kagasu.hatenablog.com/entry/2017/10/07/190551)のⅣ.Get部分です。
現在使用しようとしているjson文字列変換することが可能なvalue::as_string メソッドですが、マイクロソフトのリファレンスによると、jsonから文字列に変換する時にC++ STL 文字列形式 を返すとあります。
(
https://msdn.microsoft.com/ja-jp/library/jj987912.aspx₎
つまり、
std::cout << C++ STL 文字列形式 << std::endl
というようには出力できるのですが、
printfDx("%s",C++ STL 文字列形式 );
と書こうとするとエラーでコンパイルできません。
エラー cannot pass object of non-trivial type 'const utility::string_t' (aka 'const basic_string<char>') through variadic function; call will abort at runtime [-Wnon-pod-varargs] test.NativeActivity C:\Users\misov\Desktop\test\test\test.NativeActivity\main.cpp 36
どうやら、const utility::string_t型をchar *型かstring型に変換すれば良いようです。
しかし、std::stringも型キャストできませんし、sprintfもエラーでできません。
何かやり方があるのでしょうか?
コード
► スポイラーを表示
コード:
#include <cpprest/http_client.h>
using namespace web;
using namespace web::http;
using namespace web::http::client;
#include "DxLib.h"
pplx::task<void> Get()
{
return pplx::create_task([]
{
// proxyあり
// auto config = http_client_config();
// config.set_proxy(web_proxy(utility::conversions::to_string_t("http://127.0.0.1:8080")));
// http_client client(L"https://jsonplaceholder.typicode.com/posts/1", config);
http_client client("https://jsonplaceholder.typicode.com/posts/1");
return client.request(methods::GET);
}).then([](http_response response)
{
if (response.status_code() == status_codes::OK)
{
// レスポンスを文字列として取得後、標準出力する
// auto body = response.extract_string();
// std::wcout << body.get().c_str() << std::endl;
// レスポンスをJSONとして解析する
return response.extract_json();
}
}).then([](json::value json)
{
// タイトルだけ取得する
std::cout << json["title"].as_string() << std::endl;// *これは問題なく出来る(しかし、出力結果が確認できない)
printfDx("%s", json["title"].as_string()); // *エラー箇所。STL文字列を変換する必要がある?
});
}
// プログラムは android_main から始まります
int android_main(void)
{
const ANativeActivity *NativeActivity;
NativeActivity = GetNativeActivity();
cpprest_init(NativeActivity->vm);
if (DxLib_Init() == -1) // DXライブラリ初期化処理
{
return -1; // エラーが起きたら直ちに終了
}
try
{
Get().wait();
}
catch (const std::exception &e)
{
std::cout << "Error " << e.what() << std::endl;
}
DrawBox(220, 140, 420, 340, GetColor(255, 255, 255), TRUE); // 四角形を描画する
WaitKey(); // キー入力待ち
DxLib_End(); // DXライブラリ使用の終了処理
return 0; // ソフトの終了
}
Re: STL文字列の変換について
Posted: 2018年2月03日(土) 21:35
by inemaru
手元で確認していませんが、
その string 型には、c_str() メソッドはありませんか?
ワイド文字でない限りは、対応できると思います。
コード:
printfDx("%s", json["title"].as_string().c_str()); // *エラー箇所。STL文字列を変換する必要がある?
Re: STL文字列の変換について
Posted: 2018年2月03日(土) 21:44
by naohiro19[ログオフ]
std::string/std::wstringクラスにはメンバー関数にc_strというものを持っています。
このメンバー関数は const char*ならびに const wchar_t*を返す関数となっています。
std::cout がうまくいくのは cout自体が ostreamの typedefになっているからです。
Re: STL文字列の変換について
Posted: 2018年2月04日(日) 00:01
by Yakiniku
inemaru様
naohiro19様
無事サイトからGETすることが出来ました!
ありがとうございます。
ただ、逆にわからない所が出てきたのですが、
>>std::string/std::wstringクラスにはメンバー関数にc_strというものを持っています。
c_strがstd::string/std::wstringクラスのメンバ関数ならば、
as_string()とは一体なんなのでしょうか?
as_string::c_str()
とは書けない訳ですから、少なくともクラス、名前空間のどちらでもないことは明白です。
というよりも、メソッドとリファレンスに記載してあるので、メンバ関数なのでしょうが、
自分の知るメソッドは、
class hoge
{
void hogeFunc();
};
のhogeFunc()くらいのものです。
メソッドは書き方次第でhogeFunc().hogeFunc2()のような書き方が出来るものなのでしょうか?
以下は解決済みのコードです。
► スポイラーを表示
コード:
#include <cpprest/http_client.h>
using namespace web;
using namespace web::http;
using namespace web::http::client;
#include "DxLib.h"
char *str;
pplx::task<void> Get()
{
return pplx::create_task([]
{
// proxyあり
// auto config = http_client_config();
// config.set_proxy(web_proxy(utility::conversions::to_string_t("http://127.0.0.1:8080")));
// http_client client(L"https://jsonplaceholder.typicode.com/posts/1", config);
http_client client("https://jsonplaceholder.typicode.com/posts/1");
return client.request(methods::GET);
}).then([](http_response response)
{
if (response.status_code() == status_codes::OK)
{
// レスポンスを文字列として取得後、標準出力する
// auto body = response.extract_string();
// std::wcout << body.get().c_str() << std::endl;
// レスポンスをJSONとして解析する
return response.extract_json();
}
}).then([](json::value json)
{
// タイトルだけ取得する
// この関数内でprintfDx,DrawFormatStringを使うと以降それらの関数が正常に使えなくなるため文字列の挿入のみとする(Get().wait()関数が原因?)
sprintf(str, "%s", json["title"].as_string().c_str());
//printfDx("\n\n\ntest3 %s", json["title"].as_string().c_str());
//DrawFormatString(0, 0, GetColor(255, 255, 255), "test3 %s", json["title"].as_string().c_str());
//DrawFormatString(0, 0, GetColor(128, 255, 255), "test ");
});
}
// プログラムは android_main から始まります
int android_main(void)
{
const ANativeActivity *NativeActivity;
NativeActivity = GetNativeActivity();
cpprest_init(NativeActivity->vm);
if (DxLib_Init() == -1) // DXライブラリ初期化処理
{
return -1; // エラーが起きたら直ちに終了
}
str = new char[1024]();
SetDrawScreen(DX_SCREEN_BACK);
int x = 220, y = 140;
try
{
Get().wait();
}
catch (const std::exception &e)
{
std::cout << "Error " << e.what() << std::endl;
}
while (ProcessMessage() == 0) {
ClearDrawScreen();
if (GetTouchInputNum() > 0) {
GetTouchInput(0, &x, &y);
}
DrawBox(x, y, x + 200, y + 200, GetColor(255, 255, 255), TRUE); // 四角形を描画する
clsDx();
printfDx("test");
DrawFormatString(0, 25, GetColor(255, 255, 255), "test2");
DrawFormatString(0, 50, GetColor(255, 255, 255), "test3 %s",str);
ScreenFlip();
WaitKey(); // キー入力待ち
}
//DrawBox(220, 140, 420, 340, GetColor(255, 255, 255), TRUE); // 四角形を描画する
delete str;
DxLib_End(); // DXライブラリ使用の終了処理
return 0; // ソフトの終了
}
Re: STL文字列の変換について
Posted: 2018年2月04日(日) 02:01
by inemaru
as_string は、メソッドで、戻り値が wstring 型
wstring 型は、c_str メソッドを持ちます。
※ wstring 型を扱う場合は、ロケールの設定が必要な場合があるので注意が必要です。
適宜調べてください。
コード:
#include <iostream>
#include <string>
struct Foo {
// string 型 として取得
std::wstring as_string() const {
return L"test";
}
};
int main()
{
Foo hoge;
std::wcout << hoge.as_string().c_str() << std::endl;
return 0;
}
Re: STL文字列の変換について
Posted: 2018年2月04日(日) 09:58
by Yakiniku
inemaru様
>>as_string は、メソッドで、戻り値が wstring 型
知識不足のため戻り値が型なのに、メソッドが存在することがピンと来なかったのですが、つまりこういう書き方が可能ということですね。
コード:
#include <stdio.h>
class son
{
public:
int hoge()
{
return 5;
}
};
class parent
{
public:
son musuko;
son hoge()
{
return musuko;
}
};
int main()
{
parent oya;
printf("%d\n", oya.hoge().hoge());
return 0;
}
オブジェクト指向ではクラスも型の一部なのですね。
書き方共に非常に勉強になりました。