简体   繁体   English

c ++如何打印另一种类型变量的String属性的值?

[英]c++ How do I print the value of a String attribute of a variable of another type?

I want to display the value of a C++ String in the console- but that particular String is not defined independently- it is an attribute of a variable of another type... The line that I am currently trying to use to display its value is: 我想在控制台中显示C ++ String的值 - 但是该特定的String没有独立定义 - 它是另一种类型的变量的属性...我当前尝试用来显示其值的行是:

printf("\n CSARSixSectorItem.cpp line 530. rm_WPSequence[liSARIndex -1]: %s", rm_WPSequence[liSARIndex-1].rm_RefPointDB->m_Name);

Outside the quotes, I am passing the variable: rm_WPSequence[liSARIndex-1].rm_RefPointDB->m_Name to the %s in the printf . 在引号之外,我将变量: rm_WPSequence[liSARIndex-1].rm_RefPointDB->m_Nameprintf%s

m_Name is a variable of type String, defined with: m_Name是String类型的变量,定义为:

std::string m_Name;

rm_RefPointDB is a pointer to a CGenericRefPoint , defined with: rm_RefPointDB是一个指向CGenericRefPoint ,具有限定:

CGenericRefPoint * rm_RefPointDB;

and rm_WPSequence is a vector defined with: 和rm_WPSequence是一个定义的vector

vector< CUserWaypointListElement > rm_WPSequence;

However, although the actual variable that I am trying to display is a string , when the line is printed in the console, I am not given the contents of the string, but some unreadable characters, such as L,% ... The characters displayed change every time the line is printed. 但是,虽然我试图显示的实际变量是一个string ,但是当在控制台中打印该行时,我没有给出字符串的内容,但是一些不可读的字符,例如L,% ......字符每次打印行时显示更改。 I am wondering if this is because the String is a memeber of another variable? 我想知道这是否因为String是另一个变量的成员? If so, how can I display the value of the String? 如果是这样,我如何显示String的值? I don't really want to know anything about its parent variables, but is there something else I need to do to access the String on its own? 我真的不想知道它的父变量,但是我还需要做些什么才能自己访问String?

Use the c_str() function of std string to pass to %s : 使用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());

Explanation : %s is expecting a char* type to display. 说明:%s期望显示char *类型。 You are passing a std::string which is Class Type . 您正在传递一个类型为类型的std :: string。

c_str() returns you a pointer to the char* buffer contained by std::string. c_str()返回一个指向std :: string包含的char *缓冲区的指针。

The solution 解决方案

When print a std::string , using printf 's %s format, you should use c_str() method of std::string as following: 使用printf%s格式打印std::string ,应该使用std::string c_str()方法,如下所示:

rm_WPSequence[liSARIndex-1].rm_RefPointDB->m_Name.c_str()

You are using a C-style output way, the printf , therefore a C-style parameter should be used to print string. 您使用的是C风格的输出方式printf ,因此应使用C风格的参数来打印字符串。

c_str() is an member function of std::string . c_str()std::string的成员函数。 It returns a pointer to an array which contains a numm terminated sequence of characters representing the current value of the string object. 它返回一个指向数组的指针,该数组包含一个numm终止的字符序列,表示字符串对象的当前值。 That is the actual char array which hold the character of the name string. 这是保存名称字符串字符的实际char数组。 This is the C-style of the string, 这是字符串的C风格,

Pay attention to compiler's warnings or errors 注意编译器的警告或错误

For this problem, latest stable version of compiler should give you an error. 对于这个问题,最新的稳定版本的编译器应该会给你一个错误。 Pay attention to these could save a lot of your time. 注意这些可以节省你的大量时间。

For the following test code code, both clang++ and g++ will give an error. 对于以下测试代码代码,clang ++和g ++都会出错。 They do not allow pass non-POD object as ... for printf function call. 它们不允许将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++ output 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++ output 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=] 

Why you saw meaningless chars output 为什么你看到无意义的字符输出

your object, rm_WPSequence[liSARIndex-1].rm_RefPointDB->m_Name is an object of std::string class. 你的对象, rm_WPSequence[liSARIndex-1].rm_RefPointDB->m_Namestd::string类的对象。 For printf 's %s format, what it accept is only an array of char , or it will try to explain your std::string object in memory as char * , that is why you saw some meaningless chars output. 对于printf%s格式,它接受的只是一个char数组,或者它会尝试将内存中的std::string对象解释为char * ,这就是为什么你看到一些无意义的字符输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM