【发布时间】:2014-06-19 00:44:17
【问题描述】:
我正在尝试使用数组来跟踪不同类型项目的总数(最多 50 种类型)。当我想打印总计时,我收到一条错误消息,提示“'+' 无法添加两个指针”。我认为问题出在我的总计数组上,但我无法弄清楚。以下是我的代码示例:
string printSolution()
{
int totals[50];
string printableSolution = "";
for (int k = 0; k < itemTypeCount; k++)
{
totals[k] = 0;
}
for (int i = 0; i < itemCount; i++)
{
totals[items[i].typeCode]++;
}
for (int a = 0; a < itemTypeCount; a++)
{
printableSolution.append("There are " + totals[a] + " of Item type " + (a + 1) + ". \n");
}
}
【问题讨论】:
-
用
std::string包裹"There are"或使用std::stringstream。现在,您正在添加char*和int。 -
当您使用 C 风格的字符串时,您必须使用
strcat()来连接它们。+仅适用于std::string。
标签: c++ string visual-c++