有关功能定义的错误,但已经定义

时间:2017-03-14 00:14:34

标签: c++

首先,我真的在与C ++斗争,而且我在掌握一些事情时遇到了麻烦。我目前的很多工作都是我在网上找到的,并试图塑造我的计划。话虽如此,我是一名学生,所以我试图学习,而不只是得到答案。我也使用它作为预警,因为我的代码可能充满了错误和不合理的事情。任何关于其他方面的指针都会非常感激(由于列出的问题,我还没能进行试错测试)。它显然仍然是一个WIP。

无论如何,我的问题是我目前在第102行(无效排序)上收到错误说,"在此之前不允许使用功能定义' {'令牌"但我已经在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;
}

1 个答案:

答案 0 :(得分:0)

您的代码有两个问题:

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; 
}

首先,你的for循环缺少一个结束大括号。这意味着函数remove()的结束大括号被编译器视为for循环的结束大括号。因此,编译器发现您正在尝试在sort()函数内定义下一个函数remove(),因为尚未达到remove()函数的结束大括号。这在c ++中是不允许的,因此编译器会发出错误:

  

在'{'token

之前不允许使用函数定义

<强>其次

void getName (string nameEntries[]);

这是程序开头的函数声明。

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

这是稍后在程序中定义函数时的函数头。

编译器在这里看到2个不同的函数。即使它们具有相同的名称,编译器也可以通过它们的不同参数来区分这两个函数。第一个只有一个参数,第二个有2个参数。但是,第一个函数从未定义,第二个函数永远不会在int main()之上声明。因此,如果您尝试实现第一个函数,您将收到一个错误,指出该函数未定义(错误消息不会完全说明),如果您尝试实现第二个函数,您将收到一个错误,函数未在此范围内声明。这个问题出现在你的所有功能中。

要解决此问题,请添加/删除必需/不需要的参数,以便函数声明和函数定义匹配。