【发布时间】:2020-08-23 09:14:40
【问题描述】:
编程新手。我正在编写一个简单的函数来打印 n 次消息,但我不断收到此错误 C2664: cannot convert argument 1 from 'const char [14]' to 'char'。我无法理解它。
#include <iostream>
#include<string>
using namespace std;
void printNTimes(char msg, int n)// I also tried char *msg
{
for (int i = 1; i < n; i++)
{
cout << msg;
}
}
int main()
{
// char word = "Hello";
printNTimes("Hello, World.", 5);
}
【问题讨论】:
-
char* msg不起作用,因为"Hello, World."是您无权修改的文字,即您不允许使用非常量指针指向它。你需要一个const限定符,就像迈克的回答一样。