未声明的标识符C++
Undeclared identifier C++
我正在制作一个用于锻炼和你吃什么的计算器。我需要预先列出五种食物的清单,但清单上一直写着"未申报的标识符"。我几天前在它工作的地方工作过,但现在我没有了。
主功能中的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);
感谢大家的帮助。该解由πάΓτα给出ῥεῖ和来自莫斯科的弗拉德。我只给出了三个参数,需要四个。
相关文章:
- 在C++中,是否可以基于给定的标识符创建基类的新实例,反之亦然
- #ifdef和未声明的标识符
- f2、f3、f4标识符未找到
- 设置 Visual Studio for MPI: 找不到标识符错误
- 未声明的标识符编译暗黑破坏神 2 程序"muleview"
- 在顶点着色器中使用 OpenGl 的未声明标识符,我在顶点着色器中绘制三角形时遇到问题
- Google protobuf 时间戳未声明标识符,在 Windows 上具有C++
- 如何让 GCC/Clang 在保留标识符上出错
- 错误 C2760:语法错误:映射迭代器上意外的标记"标识符",预期的";"
- 当简单捕获中的标识符显示为参数的声明符 ID 时,没有编译器诊断
- 使用说明符 extern 声明的C++中的标识符链接
- C++:枚举:错误:应使用标识符而不是"}"
- 为什么我们不能重复使用具有不同模板参数的别名模板标识符?
- Visual Studio C++ PlaySound 标识符未定义
- 为什么我们不编写可以处理C++标识符的汇编器和链接器?
- 使用未声明的标识符"sqrt"?
- 找不到标识符合并
- 在模板化函数中重新定义(忽略)的 SWIG 标识符
- 在 c++ 中找不到此函数的标识符
- 对类 (C++) 中的私有变量使用未声明的标识符