【发布时间】:2017-05-07 04:53:19
【问题描述】:
这对于普通的 C++ 用户来说可能看起来微不足道,但我目前正在重新学习 C++。我有一个接受int& 的函数,我想将该值添加到打印语句的字符串中。所以,我有这样的事情:
std::string getTest(void* dataGroup, int& xWidth, int& yHeight)
{
std::string results = "";
results += "\nxWidth is: " + xWidth;
return results;
}
当我调用它时它无法运行。将传递引用的 int& 值转换为可以添加到字符串的形式的正确方法是什么?
【问题讨论】:
-
你不能这样做。使用例如:
results += "\nxWidth is: " + std::to_string(xWidth);。请考虑在此处发布之前进行更多研究。 ` -
+运算符的一侧必须是std::string类型才能工作。你有const char const *和int&。它们是不兼容的类型。您是否查看了编译器应该抛出的错误消息?
标签: c++ string int pass-by-reference