未声明的标识符C++

Undeclared identifier C++

本文关键字:C++ 标识符 未声明      更新时间:2023-10-16

我正在制作一个用于锻炼和你吃什么的计算器。我需要预先列出五种食物的清单,但清单上一直写着"未申报的标识符"。我几天前在它工作的地方工作过,但现在我没有了。

主功能中的Insert();出现问题insert(head, "Carrot", 100);

#include <iostream>
#include <iostream>
using namespace std;
struct node
{
    string name;
    int eatCalories;
    int number;
    node *next;
};
bool isEmpty(node *head);
char menu();
void insertAsFirstElement(node *&head, node *&last, string name, int eatCalories);
void insert(node *&head, node *&last, string name, int eatCalories);
void showList(node *current);

bool isEmpty(node *head)
{
    if(head == NULL)
        return true;
    else
        return false;
}
char menu()
{
    char choice;
    cout << "Menun";
    cout << "1. Tilføj madvarer til listenn";
    cout << "2. Vis listen af madvarern";
    cout << "3. Opdater vægtn";
    cout << "4. Indtast madvarern";
    cout << "5. Indtast dagens motionn";
    cout << "6. Exit program n";
    cin >> choice;
    return choice;
}
void insertAsFirstElement(node *&head, node *&last, string name, int eatCalories)
{
    node *temp = new node;
    temp->name = name;
    temp->eatCalories = eatCalories;
    temp->next = NULL;
    head = temp;
    last = temp;
}
void insert(node *&head, node *&last, string name, int eatCalories)
{
    if(isEmpty(head))
        insertAsFirstElement(head, last, name, eatCalories);
    else
    {
        node *temp = new node;
        temp->name = name;
        temp->eatCalories = eatCalories;
        temp->next = NULL;
        last->next = temp;
        last = temp;
    }
}
void showList(node *current)
{
    if(isEmpty(current))
        cout << "The list of foods is emptyn";
    else
    {
        cout << "The list of foods contain: n";
        int  number = 0;
        while(current != NULL)
        {
            ++number;
            cout << number << " " << current->name << " " << current->eatCalories << " calories" << endl;
            current = current->next;

        }
    }

}
node* subtractCalories(node *head)
{
    int choice;
    showList(head);
    cout << "Vælg nummer fra listenn";
    cin >> choice;
    int number = 1;
    node *current = head;
    while (current != NULL && choice != number)
    {
        ++number;
        current = current->next;
    }
    cout << "Du har valgt: " << current->name << endl;
    return current;
}


int main(int argc, const char * argv[]) {
    insert(head, "Carrot", 100);
    insert(head, "Banana", 50);
    insert(head, "Apple", 20);
    insert(head, "Pineapple", 30);
    insert(head, "Biscuit", 40);

    string motion;
    double vaegt;
    double kalorie;
    int answer = 1;
    float minutesExerciseInHour;
    int quotes = 0;
    float caloriesBurned;
    cout << "Velkommen til kalorieberegneren!n";
    cout << "Indtast dit kalorieindtag for i dag, så vi kan komme igang med at regne på detn";
    cin >> kalorie;
    cout << "Hvad er din nuvaerende veagt?n";
    cin >> vaegt;
    node *head = NULL;
    node *last = NULL;
    node *current;
    char choice;
    int eatCalories;
    string name;
    do{
        choice = menu();
        switch(choice)
        {
            case '1':
                cout << "Indtast navn:";
                cin >> name;
                cout << "Indtast nummer:";
                cin >> eatCalories;
                insert(head, last, name, eatCalories);
                break;
            case '2': showList(head);
                break;
            case '3': cout << "Hvad er din vægt?n";
                cin >> vaegt;
                cout << "Din vægt er blevet opdateret til: n";
                cout << vaegt << endl;
                break;
            case '4':
                cout << endl << "You need to consume " << kalorie << " calories today!" << endl << endl;
                current = subtractCalories(head);
                kalorie = kalorie-current->eatCalories;
                cout << endl << "You need to eat " << kalorie<< " calories more today!" << endl;
                break;
            case '5':
                do
                {
                    cout << "Hvor mange minutter motion? (angiv i timer)n";
                    cin >> minutesExerciseInHour;
                    cout << "Hvilken type motion?" <<endl;
                    cout << "1. Løb" <<endl;
                    cout << "2. Svømning" <<endl;
                    cout << "3. Cykling" <<endl;
                    cin >> quotes;
                    switch(quotes)
                    {
                        case 1:
                            caloriesBurned = vaegt*7.5*minutesExerciseInHour;
                            cout << "Du har forbrændt: " << caloriesBurned << "kaloriern";
                            kalorie = kalorie + caloriesBurned;
                            cout << "Du har: " << kalorie << " tilbage for i dagn";
                            break;
                        case 2:
                            caloriesBurned = vaegt*7*minutesExerciseInHour;
                            cout << "Du har forbrændt: " << caloriesBurned << "kaloriern";
                            kalorie = kalorie + caloriesBurned;
                            cout << "Du har: " << kalorie << " tilbage for i dagn";
                            break;
                        case 3:
                            caloriesBurned = vaegt*6*minutesExerciseInHour;
                            cout << "Du har forbrændt: " << caloriesBurned << "kaloriern";
                            kalorie = kalorie + caloriesBurned;
                            cout << "Du har: " << kalorie << " tilbage for i dagn";
                            break;
                        default: cout << "What is that?";
                    }
                }
                while (answer != 1);
            default: cout << "System exitn";
        }
    }while(choice != '5');

    return 0;
}

您正试图在声明之前使用标识符head

int main(int argc, const char * argv[]) {
    insert(head, "Carrot", 100);
    insert(head, "Banana", 50);
    insert(head, "Apple", 20);
    insert(head, "Pineapple", 30);
    insert(head, "Biscuit", 40);
//...

    node *head = NULL;
    node *last = NULL;
    node *current;
    char choice;
    int eatCalories;
    string name;

因此,当函数insert在main的一开始被调用时,编译器还不知道什么是head

还要考虑到用四个参数声明的函数

void insert(node *&head, node *&last, string name, int eatCalories);

但你主要用三个论点来称呼它

    insert(head, "Carrot", 100);
    insert(head, "Banana", 50);
    insert(head, "Apple", 20);
    insert(head, "Pineapple", 30);
    insert(head, "Biscuit", 40);

感谢大家的帮助。该解由πάΓτα给出ῥεῖ和来自莫斯科的弗拉德。我只给出了三个参数,需要四个。