c++ - 在 C++ 中连接字符串和数字?

标签 c++ string

我正在尝试连接“(”+ mouseX +“,”+ mouseY“)”。但是,mouseX 和 mouseY 是整数,所以我尝试使用如下的字符串流:

std::stringstream pos;
pos << "(" <<  mouseX << ", " << mouseY << ")";
_glutBitmapString(GLUT_BITMAP_HELVETICA_12, pos.str());

而且它似乎不起作用。

我收到以下错误:

mouse.cpp:75: error: cannot convert std::basic_string<char, std::char_traits<char>, std::allocator<char> >' toconst char*' for argument 2' tovoid _glutBitmapString(void*, const char*)'

我在这个基本的字符串 + 整数连接中做错了什么?

最佳答案

glutBitmapString() 需要一个 char* 而您要向它发送一个字符串。像这样在字符串上使用 .c_str():

_glutBitmapString(GLUT_BITMAP_HELVETICA_12, pos.str().c_str());

关于c++ - 在 C++ 中连接字符串和数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/544397/

相关文章:

c++ - 如何将 vector (或不同的容器)与不可复制的类一起使用?

c++ - XCode 不会将子目录中的 c++ 源文件添加到编译源中

python - Value Pandas Dataframe 中的列内的项目计数,其中包含字符串列表作为值

python - 将 python 字典转换为逗号分隔键字符串和逗号分隔值字符串的优雅方法是什么?

c++ - 如何将 std::string 转换为 const char* 或 char*

c++ - 为什么C++标准库中没有transform_if?

c++ - 如何获取数据 IWbemClassObject **

c++ - 如何使用 C++ 模板参数来决定类中的成员类型

Java ME 双空格中的字符串替换

string - Swift 快速低级 String lastIndexOf