C ++ How to print the value of a String attribute of a variable of a different type?
I want to display a C ++ value String
in the console, but this particular one is String
not defined independently - it is an attribute of a variable of a different type ... The line I am currently trying to display its value is:
printf("\n CSARSixSectorItem.cpp line 530. rm_WPSequence[liSARIndex -1]: %s", rm_WPSequence[liSARIndex-1].rm_RefPointDB->m_Name);
Outside of the quotes, I pass the variable: rm_WPSequence[liSARIndex-1].rm_RefPointDB->m_Name
in %s
to printf
.
m_Name
is a variable of type String, defined with:
std::string m_Name;
rm_RefPointDB
is a pointer to CGenericRefPoint
, defined with:
CGenericRefPoint * rm_RefPointDB;
and rm_WPSequence is vector
defined with:
vector< CUserWaypointListElement > rm_WPSequence;
However, although the actual variable I am trying to display is String
when the line is printed to the console, I am not given the contents of the line, but some unreadable characters such as L,%
... The displayed characters change every time the line is printed. I am wondering if this is because String
memeber is another variable? If so, how can I display the String value? I don't really want to know anything about its parent variables, but is there something else I need to do in order to access the String myself?
source to share
Use the c_str () function of std to jump to% s:
printf("\n CSARSixSectorItem.cpp line 530. rm_WPSequence[liSARIndex -1]: %s", rm_WPSequence[liSARIndex-1].rm_RefPointDB->m_Name.c_str());
Explanation:% s expects a char * to display. You are passing a std :: string, which is the type of the class.
c_str () returns a pointer to the char * buffer contained in std :: string.
source to share
Decision
When printing std::string
using format printf
%s
, you should use the method c_str()
std::string
like this:
rm_WPSequence[liSARIndex-1].rm_RefPointDB->m_Name.c_str()
You are using the C-style output method printf
, so the C-style parameter must be used to print the line.
c_str()
is a member function std::string
. It returns a pointer to an array that contains a numm-terminated character sequence representing the current value of the string object. This is the actual array char
that contains the name string character. This is the C-style line,
Pay attention to compiler warnings or errors
For this problem, the latest stable compiler should give you an error. Please note that this can save you a lot of time.
For the following test code, both clang ++ and g ++ will throw an error. They do not allow passing an object non-POD
like a ...
function call 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 ++ exit
$ 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++ 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 did you see the output of meaningless characters
your object rm_WPSequence[liSARIndex-1].rm_RefPointDB->m_Name
is a class object std::string
. For the format printf
%s
, what it accepts is just an array char
, or it will try to explain your object std::string
in memory as char *
, which is why you saw the output of meaningless characters.
source to share