簡體   English   中英

c ++如何打印另一種類型變量的String屬性的值?

[英]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_Nameprintf%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_Namestd::string類的對象。 對於printf%s格式,它接受的只是一個char數組,或者它會嘗試將內存中的std::string對象解釋為char * ,這就是為什么你看到一些無意義的字符輸出。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM