[英]c++ How do I print the value of a String attribute of a variable of another type?
我想在控制台中顯示C ++ String
的值 - 但是該特定的String
沒有獨立定義 - 它是另一種類型的變量的屬性...我當前嘗試用來顯示其值的行是:
printf("\n CSARSixSectorItem.cpp line 530. rm_WPSequence[liSARIndex -1]: %s", rm_WPSequence[liSARIndex-1].rm_RefPointDB->m_Name);
在引號之外,我將變量: rm_WPSequence[liSARIndex-1].rm_RefPointDB->m_Name
給printf
的%s
。
m_Name
是String類型的變量,定義為:
std::string m_Name;
rm_RefPointDB
是一個指向CGenericRefPoint
,具有限定:
CGenericRefPoint * rm_RefPointDB;
和rm_WPSequence是一個定義的vector
:
vector< CUserWaypointListElement > rm_WPSequence;
但是,雖然我試圖顯示的實際變量是一個string
,但是當在控制台中打印該行時,我沒有給出字符串的內容,但是一些不可讀的字符,例如L,%
......字符每次打印行時顯示更改。 我想知道這是否因為String
是另一個變量的成員? 如果是這樣,我如何顯示String的值? 我真的不想知道它的父變量,但是我還需要做些什么才能自己訪問String?
使用std string的c_str()函數傳遞給%s:
printf("\n CSARSixSectorItem.cpp line 530. rm_WPSequence[liSARIndex -1]: %s", rm_WPSequence[liSARIndex-1].rm_RefPointDB->m_Name.c_str());
說明:%s期望顯示char *類型。 您正在傳遞一個類型為類型的std :: string。
c_str()返回一個指向std :: string包含的char *緩沖區的指針。
使用printf
的%s
格式打印std::string
,應該使用std::string
c_str()
方法,如下所示:
rm_WPSequence[liSARIndex-1].rm_RefPointDB->m_Name.c_str()
您使用的是C風格的輸出方式printf
,因此應使用C風格的參數來打印字符串。
c_str()
是std::string
的成員函數。 它返回一個指向數組的指針,該數組包含一個numm終止的字符序列,表示字符串對象的當前值。 這是保存名稱字符串字符的實際char
數組。 這是字符串的C風格,
對於這個問題,最新的穩定版本的編譯器應該會給你一個錯誤。 注意這些可以節省你的大量時間。
對於以下測試代碼代碼,clang ++和g ++都會出錯。 它們不允許將non-POD
對象傳遞為...
用於printf
函數調用。
#include <string>
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
string a("This is a string");
printf("0x%x, 0x%s, 0x%x\n", a, a , &a);
cout << a << endl;
return 0;
}
Clang ++輸出
$ clang++ Stringcstring.cpp Stringcstring.cpp:26:32: error: cannot pass non-POD object of type 'string' (aka 'basic_string<char>') to variadic function; expected type from format string was 'unsigned int' [-Wnon-pod-varargs] printf("0x%x, 0x%s, 0x%x\\n", a, a , &a); ~~ ^ Stringcstring.cpp:26:35: error: cannot pass non-POD object of type 'string' (aka 'basic_string<char>') to variadic function; expected type from format string was 'char *' [-Wnon-pod-varargs] printf("0x%x, 0x%s, 0x%x\\n", a, a , &a); ~~ ^ Stringcstring.cpp:26:35: note: did you mean to call the c_str() method? printf("0x%x, 0x%s, 0x%x\\n", a, a , &a); ^ .c_str() Stringcstring.cpp:26:39: warning: format specifies type 'unsigned int' but the argument has type 'string *' (aka 'basic_string<char> *') [-Wformat] printf("0x%x, 0x%s, 0x%x\\n", a, a , &a); ~~ ^~ 1 warning and 2 errors generated.
g ++輸出
$ g++ Stringcstring.cpp Stringcstring.cpp: In function 'int main()': Stringcstring.cpp:26:41: error: cannot pass objects of non-trivially-copyable type 'std::string {aka class std::basic_string<char>}' through '...' printf("0x%x, 0x%s, 0x%x\\n", a, a , &a); ^ Stringcstring.cpp:26:41: error: cannot pass objects of non-trivially-copyable type 'std::string {aka class std::basic_string<char>}' through '...' Stringcstring.cpp:26:41: warning: format '%x' expects argument of type 'unsigned int', but argument 4 has type 'std::string* {aka std::basic_string<char>*}' [-Wformat=]
你的對象, rm_WPSequence[liSARIndex-1].rm_RefPointDB->m_Name
是std::string
類的對象。 對於printf
的%s
格式,它接受的只是一個char
數組,或者它會嘗試將內存中的std::string
對象解釋為char *
,這就是為什么你看到一些無意義的字符輸出。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.