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_Name
传递给printf
中的%s
。
m_Name
是String类型的变量,定义为:
std::string m_Name;
rm_RefPointDB
是指向CGenericRefPoint
的指针,定义为:
CGenericRefPoint * rm_RefPointDB;
并且rm_WPSequence是用定义的CCD_ 9
vector< CUserWaypointListElement > rm_WPSequence;
然而,尽管我试图显示的实际变量是string
,但当在控制台中打印该行时,我没有得到字符串的内容,而是得到了一些不可读的字符,例如L,%
。。。每次打印行时,显示的字符都会发生变化。我想知道这是否是因为String
是另一个变量的模因?如果是,我如何显示字符串的值?我真的不想知道任何关于它的父变量的信息,但我还需要做其他事情来单独访问String吗?
使用std字符串的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::字符串,它是Class Type。
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样式参数来打印字符串。
CCD_ 19是CCD_。它返回一个指向数组的指针,该数组包含一个以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%xn", 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%xn", 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%xn", a, a , &a);
~~ ^
Stringcstring.cpp:26:35: note: did you mean to call the c_str() method?
printf("0x%x, 0x%s, 0x%xn", 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%xn", 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%xn", 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 *
,这就是为什么您看到一些无意义的字符输出。
- 从模板创建通用打印函数,以打印基元数据类型变量的值
- 指针数据类型变量如何包含对象?
- 将 static_cast<int>(-15) 分配给静态常量字符类型变量
- 将内置类型变量传递给只有一个类类型参数的"+"运算符函数时自动类型转换的构造函数
- 如何为结构类型变量赋值
- 如何将字符类型变量附加到字符串中?
- 共享或私有 openmp 代码中的结构化类型变量
- 有没有办法在 c++ 中制作类型变量?
- 从其他类声明类类型变量会给出 C2143 错误(在"*"之前缺少";")
- 如何检查类中定义的枚举类型变量是否被分配给?
- 将子类实例保存在父类型变量中并通过父变量使用 Child 函数?
- 是否可以在没有结构的情况下创建新的类型变量?
- 为什么类型变量;不调用默认 CTR
- 如何使用CIN将输入整数值获取到char类型变量中
- 如何为 char** 类型变量提供适当的值,以便将其分配给字符串变量
- 在C++标准中哪里说必须初始化 const 内置类型变量的定义
- 为什么可以将其他类型变量用作C 中常量参考参数参数的参数
- Eclipse CDT 打开声明的自动类型变量
- 如何计算长长类型变量中的位数
- 将自动类型变量初始化为零.这种类型安全吗?