简体   繁体   中英

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:

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 .

m_Name is a variable of type String, defined with:

std::string m_Name;

rm_RefPointDB is a pointer to a CGenericRefPoint , defined with:

CGenericRefPoint * rm_RefPointDB;

and rm_WPSequence is a vector defined with:

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. I am wondering if this is because the String is a memeber of another variable? If so, how can I display the value of the 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?

Use the c_str() function of std string to pass 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 is expecting a char* type to display. You are passing a std::string which is Class Type .

c_str() returns you a pointer to the char* buffer contained by std::string.

The solution

When print a std::string , using printf 's %s format, you should use c_str() method of std::string as following:

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_str() is an member function of 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. That is the actual char array which hold the character of the name string. This is the C-style of the string,

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. They do not allow pass non-POD object as ... for printf function call.

#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++ 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 you saw meaningless chars output

your object, rm_WPSequence[liSARIndex-1].rm_RefPointDB->m_Name is an object of std::string class. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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