简体   繁体   中英

c++ how can I display the string attribute of an element at a particular position in a vector?

I am trying to print the value of a C++ variable in the console for debugging purposes. The line that I am using to try and do this is:

    printf("\n Value of lTempCoord in CSARCreepingLineItem::SteerStep(...): %s", rm_WPSequence[m_SAR_OrdinalNumber].m_FixName);

But the output that this line generates is unreadable. I am wondering if this is because I am trying to display it using a 'string' character %s but then passing something other than a simple string/ character array to it?

Breaking down the variable that I am passing to the printf() :

rm_WPSequence is a Vector, defined with:

vector< CUserWaypointListElement > rm_WPSequence;

m_SAR_OrdinalNumber is an int, and

m_FixName is a string, defined inside the CUserWaypoingListElement class with:

std::string m_FixName;

The output generated by the line is a few 'special' characters (not ones that you can see on the keyboard).

What am I doing wrong here? Is the output unreadable because I'm not accessing the member of the Vector correctly, or because the string is defined as part of the std library? Or is it something else completely?

Try this

std::cout << rm_WPSequence[m_SAR_OrdinalNumber].m_FixName  << std::endl;

printf is basically a C function, you should use c++ mechanism in your c++ programs ;). If you really want to use printf() , transform your std::string in char * by using the std::string.c_str() method

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