"Cannot overload functions by return type alone"错误

"Cannot overload functions by return type alone" error

本文关键字:type alone 错误 return by Cannot overload functions      更新时间:2023-10-16

当涉及到我的函数时,我一直得到"不能重载仅由返回类型区分的函数"字符串进程打开(字符名称)我不知道为什么。我已经将程序削减为基本的外壳,但仍然没有运气。任何帮助将不胜感激。

编译过程中的实际视觉工作室错误显示"缺少类型说明符 - 假设为 int。注意:C++不支持默认整数。

#include <iostream>
#include <string>
#include <fstream>

char openCommand();
string processOpen(char entryReturn);
bool logIn(string name);
bool addNewMember(string name);
void processQuit();

//Global Variables
string memberlist = "memberlist.txt";                                 
string checkedOutList = "checkedoutbooks.txt";
using namespace std;
int main() {
char entryReturn = ' ';                                         
string name;                                                    
while (entryReturn != 'q') {
    entryReturn = openCommand();
    name = processOpen(entryReturn);
}
return 0;
 }

该函数看起来像

string processOpen(char entryReturn) {
bool allReadyThere = false;                                     
string name = " ";
//This will process the <log in> selection
if (entryReturn == 'a')
{
    cout << "Enter your first and last name" << endl;
    cin.ignore();
    getline(cin, name);
    allReadyThere = logIn(name);
    if (allReadyThere == false)
    {
        cout << "You need to register 
       as you don't have an account"        <<        endl;
    }
 }
//This will process the <register> selection
else if (entryReturn == 'b')
{
    cout << "Enter your first and last name" << endl;
    cin.ignore();
    getline(cin, name);
    allReadyThere = addNewMember(name);
    if (allReadyThere == true) {
        cout << "you already have an account" << endl;
    }
}
else if (entryReturn == 'q') {
    processQuit();
}
else
{
    cout << "This is a non working command";
}
return name;
}

善待代码 我还是有点新。提前谢谢。

在行:

string processOpen(char entryReturn);

编译器不知道string是什么意思。你应该在这里写std::string

显然,您的编译器猜到您拼写错误int.(现在编译器很聪明,对吧?

然后,后来你写了using namespace std;,然后是string processOpen(char entryReturn) {.此时,编译器在 namespace std 中找到 string ,它看到您定义了两个具有相同名称和参数的函数,但一个返回 int 和一个返回 std::string ,这是不允许的。

这是一个很好的例子,说明为什么您应该将注意力集中在编译器的第一个输出消息上(无论是"错误"还是"警告",实际上都没有区别)。 遇到错误后,编译器会猜测您的意思并尝试继续编译,但显然任何后续消息都会受到此猜测结果的影响,如果它猜对了。

修复第一条消息后,重新编译以查看其他"级联"消息是否也消失。