有关函数定义的错误,但已定义

Error regarding function-definition, but it's already been defined

本文关键字:定义 错误 函数      更新时间:2023-10-16

首先,我真的很挣扎在C 上,我很难抓住一些事情。我目前的许多工作都是我在网上找到的东西,并试图制定适合我的程序。话虽如此,我是一名学生,所以我正在尝试学习,而不仅仅是得到答案。我也将其用作警告,因为我的代码可能充满了错误和没有意义的事情。任何关于其他方面的指针都将不胜感激(由于列出的问题,我还没有试用试验测试)。显然仍然是一个wip。

无论如何,我的问题是我当前在第102行(void sort)上遇到一个错误,说:"在'{'token'之前,在这里不允许使用功能定义,但是我已经在开始时定义了它在main()之前我的程序。我更改了订单,所以现在是我的排序功能引起了我的悲伤,而在搜索功能给出问题之前。

我将包含整个代码,因为正如我所说的那样,事情似乎有效。

//Write a program that store names to an array. 
//The program ends when “quit” is entered.
//The program includes a menu having these choices : 
//getNames, sortNames
, displayNames, findName, removeName.
#include <iostream>
#include <string>
using namespace std;
void getName (string nameEntries[]);
void remove (string nameEntries[]);
void sort (string nameEntries[]);
void search (string nameEntries[]);
void display (string nameEntries[]);
int main()
{
    const int MAXSTRINGS = 20; //Max number of names to be entered
    string nameEntries[MAXSTRINGS];
    string input;
    cout << "Please enter an option from the following: " << endl;
    cout << "'Get' - Allows entry of names to list (Max 20)." << endl;
    cout << "'Display' - Shows current entries of list." << endl;
    cout << "'Sort' - Alphabetizes entries of list." << endl;
    cout << "'Search' - Finds name on current list." << endl;
    cout << "'Remove' - Removes an entry from the list." << endl;
    cout << "'Quit' - Ends the program." << endl;
    cin >> input;
    if (input == "Get" || "get" ) //Runs function to add names to list.
        {
            getName(nameEntries);
        }
    else if (input == "Remove" || "remove" ) //Runs function to remove specified name from list.
        {
            remove(nameEntries);
        }
    else if (input == "Sort" || "sort" ) //Runs function to sort list alphabetically.
        {
            sort(nameEntries);
        }
    else if (input == "Search" || "search" ) //Runs function to search list for a specified name.
        {
            search(nameEntries);
        }
    else if (input ==  "Display" || "display" ) //Runs function to display current entries.
        {
            display(nameEntries);
        }
    else if (input == "Quit" || "quit" ) //Exits program.
        {
            return 0;
        }
    while  (input != "Display" && "display" && "Find" && "find" && "Remove" && "remove" && "Sort" && "sort" && "Get" && "get" && "Quit" && "quit")
        {
            cout << "Please enter a menu option: ";
            cin >> input;
        }
}
//Allows continued entry of names to array
void getName(string nameEntries[], const int MAXSTRINGS)
{
    int i;
    for (i=0; i < MAXSTRINGS; i++)
    {
        cout << "Enter a name for storage or 'Menu' for more options: ";
        cin >> nameEntries[MAXSTRINGS];
        if (cin == "Menu" || "menu")
        {
            return;
        }
    }
    return;
}
//Remove specified previously entered name from array       
void remove (string nameEntries[], const int MAXSTRINGS)
{
    string Empty;
    string nameRemove;
    cout << "Please enter the name to remove from the list: ";
    cin >> nameRemove;
    for(int i = 0; i < MAXSTRINGS; i++)
    {
        if(nameEntries[i] == nameRemove)
        {
             nameEntries[i] = Empty;             //previous constant to represent your "empty" cell
        }
        else if(i == MAXSTRINGS - 1)    //on last loop, tell the user you could not find it.
        {
            cout << "Could not find that name to remove.";
        }
    return; 
}
//Alphabetize inputted names in array 
void sort (string nameEntries[], int size)
{
    bool flag;
    do
    {
        flag = 0;
        for (int count = 0; count < size-1; count++)
        {
            if (nameEntries[count] > nameEntries[count+1])
            {
                nameEntries[count].swap(nameEntries[count+1]);
                flag =1;
            }
        }
    }
    while (flag==1);
    return;
}
//Search array for desired name entry
void search (string nameEntries[], const int MAXSTRINGS)
{
    string name;
    cout<<"Please enter a name to search for or Quit to return:";
    cin >> name;
    if (name == "Quit" || "quit") return;
    int first = 0;
    int last = size - 1;
    bool found = false;
    while (!found && first <= last)
    {
        int middle = (first + last) / 2;
        if (nameEntries[middle] == name)
        {
            found = true;
            cout<<array[middle]<<" is on the list."<<endl;
        }
        else if (array[middle] > name)
            last = middle - 1;
        else
            first = middle + 1;
        }
    return;
}
//Show current entries in array
void display (string nameEntries[], const int MAXSTRINGS)
{
    cout << nameEntries[MAXSTRINGS];
    return;
}

2您的代码问题:

void remove (string nameEntries[], const int MAXSTRINGS)
{
    string Empty;
    string nameRemove;
    cout << "Please enter the name to remove from the list: ";
    cin >> nameRemove;
    for(int i = 0; i < MAXSTRINGS; i++)
    {
        if(nameEntries[i] == nameRemove)
        {
             nameEntries[i] = Empty;             //previous constant to represent your "empty" cell
        }
        else if(i == MAXSTRINGS - 1)    //on last loop, tell the user you could not find it.
        {
            cout << "Could not find that name to remove.";
        }
    // Missing a closing curly brace here <---
    return; 
}

首先是,您的前循环缺少闭合的卷曲支架。这意味着,编译器将函数remove()的闭合卷发架视为前循环的关闭卷曲支架。因此,编译器认为您正在尝试定义remove()功能内的下一个函数,即sort(),因为尚未达到remove()功能的闭合卷发。在C 中不允许这样做,因此编译器吐出了错误:

在'{'token

之前都不允许使用功能定义

第二,

void getName (string nameEntries[]);

这是程序开始时的函数声明。

void getName(string nameEntries[], const int MAXSTRINGS)

这是稍后在程序中定义函数时的功能标题。

编译器在此处看到2个不同的功能。即使它们具有相同的名称,编译器也可以通过其不同的论点来区分这两个函数。第一个只有一个参数,第二个参数有2个论点。但是,第一个函数从未定义,第二个功能从未在int main()上方声明。因此,如果您尝试将第一个功能插入第一个函数,则会出现一个错误,说该功能是未定义的(错误消息不会确切说明),如果您尝试实现第二个功能,您将获得一个错误函数蛋白没有被宣布为JN此范围。这个问题在您的所有功能中。

要解决此问题,请添加/删除所需/未缴纳的参数,以使函数声明和函数定义匹配。