gpt4 book ai didi

c++ - 为什么使用文字数字的引用调用会出现 "no matching function"错误?

转载 作者:行者123 更新时间:2023-12-03 20:23:15 25 4
gpt4 key购买 nike

该问题要求创建一个程序,要求用户输入一些文本,并且该文本将根据屏幕的宽度被星号包围,例如,如果用户输入“Hello world”,则输出应为:

****************
* Hello World! *
****************

我已尝试创建函数,但由于显示的最少代码出现编译器错误,我被卡住了。

问题:为什么它告诉我 no matching function for within_width(text, 80)

我的部分代码如下:

#include <iostream>
#include <string>

void display_header (std::string &header) {
std::string text;
header = text;
}

bool within_width (std::string& text, unsigned short int& max_width) {


}

int main() {
std::string text;
std::cout << "Please enter header text: ";
std::getline(std::cin, text);

if (within_width(text, 80)) {

// call the display_header function and pass in the text
// inputted by the user
} else {
std::cout << text;
}
return 0;
}

最佳答案

这个函数的声明

bool within_width (std::string& text, unsigned short int& max_width)

要求一个unsigned short int 变量,因为它有一个引用参数,见第二个&

为了满足它,你需要将值 80 放入一个变量中,并将该变量作为参数。

unsigned short int MyWidth=80;
if (within_width(text, MyWidth))

或者(但我假设您不被允许)您可以使用按值参数调用

bool within_width (std::string& text, unsigned short int max_width)

然后你就可以调用了。

关于c++ - 为什么使用文字数字的引用调用会出现 "no matching function"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58297914/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com