Der*_*erp 0 c++ pointers constants default-value
转换时遇到问题.我不能在这个程序中使用字符串所以我必须使用char's - 我得到错误:
error C2664: 'printText' : cannot convert parameter 1 from 'const char [21]' to 'char'
1> There is no context in which this conversion is possible
Run Code Online (Sandbox Code Playgroud)
我已经尝试将其转换为const指针:
void printText(const char* text[100] = "notextgiven"...
Run Code Online (Sandbox Code Playgroud)
但它似乎没有帮助,给了我更多的错误.
我的计划:
#include <iostream>
using namespace std;
void printText(char, char, int);
int main(){
printText("I hear and I forget.", "*", 15);
}
void printText(char text[100] = "notextgiven", char symbol = ' ', int repeat = 10){
int temp = 0;
while(temp < repeat){
cout << symbol;
temp++;
}
cout << text;
temp = 0;
while(temp < repeat){
cout << symbol << endl;
temp++
}
}
Run Code Online (Sandbox Code Playgroud)
这不是为您要执行的操作定义函数的正确方法
void printText(char text[100] = "notextgiven", char symbol = ' ', int repeat = 10)
Run Code Online (Sandbox Code Playgroud)
试试这个
void printText(const char* text, char symbol, int repeat)
Run Code Online (Sandbox Code Playgroud)
这应该允许你的程序编译.也改变这一行
printText("I hear and I forget.", "*", 15);
Run Code Online (Sandbox Code Playgroud)
至
printText("I hear and I forget.", '*', 15);
^ ^
Run Code Online (Sandbox Code Playgroud)
单引号用于单个char变量,双引号将其视为字符串文字.由于这是家庭作业,您可能会被要求专门使用,char*但由于您通常使用C++,因此使用起来会好得多std::string