C++ 的 main.cpp 中的结构声明问题
Posted
技术标签:
【中文标题】C++ 的 main.cpp 中的结构声明问题【英文标题】:Struct Declaration Issue in main.cpp for C++ 【发布时间】:2013-11-29 05:42:38 【问题描述】:所以我一直在构建一个可以用作成绩簿的程序,但 main.cpp 中的特定函数存在问题。 这是代码(顺便说一下,这是 C++):
#include <iostream>
#include <string>
using namespace std;
struct Classes
double accousticGuitarEnsemble;
double biology;
double english;
double enteringAKehillah;
double geometry;
double hebrew;
double worldHistory;
;
void gradeEditor()
cout << "GradeBook 1.0" << endl;
newGrade:
cout << "Which grade are you entering today? (Use the following format: exampleFormat): ";
string classBeingEntered;
getline(cin, classBeingEntered);
Classes Eitan;
cout << "Enter the new grade: ";
double grade;
cin >> grade;
cout << "Grade entered." << endl;
if (classBeingEntered == "accousticGuitarEnsemble")
Eitan.accousticGuitarEnsemble = grade;
else if (classBeingEntered == "biology")
Eitan.biology = grade;
else if (classBeingEntered == "english")
Eitan.english = grade;
else if (classBeingEntered == "enteringAKehillah")
Eitan.enteringAKehillah = grade;
else if (classBeingEntered == "geometry")
Eitan.geometry = grade;
else if (classBeingEntered == "hebrew")
Eitan.hebrew = grade;
else if (classBeingEntered == "worldHistory")
Eitan.worldHistory = grade;
else
cout << "Invalid class name. Try again." << endl;
goto newGrade;
void choice()
choiceBack:
cout << "Do you want to edit another grade? Press Y or N: ";
char chChoice;
cin >> chChoice;
switch (chChoice)
case 'Y':
cout << "Alright then!" << endl;
do
gradeEditor();
goto choiceBack;
while (chChoice == 'Y');
case 'N':
cout << "Printing grades..." << endl;
break;
case 'y':
cout << "Alright then!" << endl;
do
gradeEditor();
goto choiceBack;
while (chChoice == 'y');
case 'n':
cout << "Printing grades..." << endl;
break;
void printGrades(Classes Eitan)
cout << "Accoustic Guitar Ensemble: " << Eitan.accousticGuitarEnsemble << endl;
cout << "Biology: " << Eitan.biology << endl;
cout << "English: " << Eitan.english << endl;
cout << "Entering a Kehillah: " << Eitan.enteringAKehillah << endl;
cout << "Geometry: " << Eitan.geometry << endl;
cout << "Hebrew: " << Eitan.hebrew << endl;
cout << "World History: " << Eitan.worldHistory << endl;
system("PAUSE");
int main()
gradeEditor();
choice();
printGrades();
但是,对于 printGrades(),我收到此错误: 错误 C2660:'printGrades':函数不接受 0 个参数 IntelliSense:函数调用中的参数太少 这两个错误都发生在第 92 行,其中在 main 中调用了 printGrades。 无论我在 printGrades 括号中放什么,都会出现未声明的标识符错误。 有谁知道如何解决这一问题?另外,有没有人觉得这段代码还有什么问题?
更新:我已经修复了代码(有点)。它编译并运行,这就是它现在的样子:
#include <iostream>
#include <string>
using namespace std;
struct Classes
double acousticGuitarEnsemble;
double biology;
double english;
double enteringAKehillah;
double geometry;
double hebrew;
double worldHistory;
;
Classes gradeEditor()
Classes eitan;
cout << "GradeBook 1.0" << endl;
newGrade:
cout << "Which grade are you entering today? (Use the following format: exampleFormat): ";
string classBeingEntered;
getline(cin, classBeingEntered);
cout << "Enter the new grade: ";
double grade;
cin >> grade;
cout << "Grade entered." << endl;
if (classBeingEntered == "acousticGuitarEnsemble")
eitan.acousticGuitarEnsemble = grade;
else if (classBeingEntered == "biology")
eitan.biology = grade;
else if (classBeingEntered == "english")
eitan.english = grade;
else if (classBeingEntered == "enteringAKehillah")
eitan.enteringAKehillah = grade;
else if (classBeingEntered == "geometry")
eitan.geometry = grade;
else if (classBeingEntered == "hebrew")
eitan.hebrew = grade;
else if (classBeingEntered == "worldHistory")
eitan.worldHistory = grade;
else
cout << "Invalid class name. Try again." << endl;
goto newGrade;
void choice()
choiceBack:
cout << "Do you want to edit another grade? Press Y or N: ";
char chChoice;
cin >> chChoice;
switch (chChoice)
case 'Y':
cout << "Alright then!" << endl;
do
gradeEditor();
goto choiceBack;
while (chChoice == 'Y');
case 'N':
cout << "Printing grades..." << endl;
break;
case 'y':
cout << "Alright then!" << endl;
do
gradeEditor();
goto choiceBack;
while (chChoice == 'y');
case 'n':
cout << "Printing grades..." << endl;
break;
void printGrades(Classes eitan)
cout << "Acoustic Guitar Ensemble: " << eitan.acousticGuitarEnsemble << endl;
cout << "Biology: " << eitan.biology << endl;
cout << "English: " << eitan.english << endl;
cout << "Entering a Kehillah: " << eitan.enteringAKehillah << endl;
cout << "Geometry: " << eitan.geometry << endl;
cout << "Hebrew: " << eitan.hebrew << endl;
cout << "World History: " << eitan.worldHistory << endl;
system("PAUSE");
int main()
Classes eitan = gradeEditor();
choice();
printGrades(eitan);
但是,当我运行程序时,我可以进入一个等级,但整个过程“中断”并变得无法修复。 如果有人可以进一步帮助我,请运行我的程序并在下面发表评论。
【问题讨论】:
因为它困扰着我:“声学”,而不是“声学”。 使类全局化,或者通过引用或地址传递给每个函数; (Classes &classes) 或 (Classes *classes)。正如 John3136 所提到的,gradeEditor 中的本地实例实际上是无用的。此外,在gradeEditor 的最后“else”子句中的两个语句周围添加花括号。 你的代码有很多错误。你应该从更简单的东西开始,一旦你测试了它的工作原理,就可以扩展它。 【参考方案1】:您的 printGrades()
方法需要一个 Classes
类型的参数,但您没有传递它。
还建议让您的变量名称/参数以小写字母开头,这样它们看起来不像类型。即void printGrades(Classes Eitan)
应该是void printGrades(Classes eitan)
Classes
的唯一实例是 gradeEditor()
中的本地实例,因此没有任何内容可以“存储在任何地方”。
最后但同样重要的是:
您正在使用 GOTO
【讨论】:
不是op使用goto,而是他如何使用goto。 @bvj 没有。在我的书中,他使用的是goto
然而它是语言的一部分。尽管如此,GSD 仍占主导地位。
@John3136 我使用 goto 主要是为了跳回到同一代码块中的某个位置。没有更好的方法可以做到这一点,对吧?此外,在实现 printGrades(Classes eitan) 的参数后,我收到以下错误:“错误 C2059:语法错误:')'”“错误 C2146:语法错误:在标识符 'eitan' 之前缺少')'”“错误C2275:“类”:非法将此类型用作表达式“”IntelliSense:预期为“)”“IntelliSense:不允许类型名称”。还有其他建议吗? (错误都在第 92 行)
@DJHead-On:有很多更好的方法可以做到这一点。例如,您可以简单地返回一个布尔值,指示用户是否要继续。然后在main中,你可以说do gradeEditor(); while (choice());
。【参考方案2】:
在 main() 中创建一个 struct Classes 的对象,并将该对象作为参数传递给 printGrades()。根据函数定义,函数 printGrades() 需要一个结构对象作为其参数。像这样的。
Classes cl;
printGrades(cl);
即使您执行上述操作,printGrades() 函数也会打印垃圾值,因为结构对象未初始化,因此我建议您更改 gradeEditor( ) 从 void 到 struct 类型,将对象返回到结构并在 c1 中复制该对象。这样的东西会更合适,您也可以删除方法 choice
Classes gradeEditor()
/* Function Body*/
return Eitan;
main()
Classes cl;
char choice;
do
c1=gradeEditor();
cout << "Do you want to edit another grade? Press Y or N: ";
while((choice=getch())!='n' || (choice=getch())!='N');
cout<<"Printing Grades: "<<endl;
printGrades(cl);
并且在gradeEditor中你使用的是goto,仔细观察,控件永远不会从gradeEditor()方法中出来,因为你没有指定任何条件出来。
【讨论】:
【参考方案3】:如果您在 Windows 以外的系统上使用以下代码,您还需要包含 #include <cstdlib>
以使用 system("pause")
,则以下代码将适用于您
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
struct Classes
double accousticGuitarEnsemble;
double biology;
double english;
double enteringAKehillah;
double geometry;
double hebrew;
double worldHistory;
;
void gradeEditor()
cout << "GradeBook 1.0" << endl;
newGrade:
cout << "Which grade are you entering today? (Use the following format: exampleFormat): ";
string classBeingEntered;
getline(cin, classBeingEntered);
Classes Eitan;
cout << "Enter the new grade: ";
double grade;
cin >> grade;
cout << "Grade entered." << endl;
if (classBeingEntered == "accousticGuitarEnsemble")
Eitan.accousticGuitarEnsemble = grade;
else if (classBeingEntered == "biology")
Eitan.biology = grade;
else if (classBeingEntered == "english")
Eitan.english = grade;
else if (classBeingEntered == "enteringAKehillah")
Eitan.enteringAKehillah = grade;
else if (classBeingEntered == "geometry")
Eitan.geometry = grade;
else if (classBeingEntered == "hebrew")
Eitan.hebrew = grade;
else if (classBeingEntered == "worldHistory")
Eitan.worldHistory = grade;
else
cout << "Invalid class name. Try again." << endl;
goto newGrade;
void choice()
choiceBack:
cout << "Do you want to edit another grade? Press Y or N: ";
char chChoice;
cin >> chChoice;
switch (chChoice)
case 'Y':
cout << "Alright then!" << endl;
do
gradeEditor();
goto choiceBack;
while (chChoice == 'Y');
case 'N':
cout << "Printing grades..." << endl;
break;
case 'y':
cout << "Alright then!" << endl;
do
gradeEditor();
goto choiceBack;
while (chChoice == 'y');
case 'n':
cout << "Printing grades..." << endl;
break;
void printGrades(Classes Eitan)
cout << "Accoustic Guitar Ensemble: " << Eitan.accousticGuitarEnsemble << endl;
cout << "Biology: " << Eitan.biology << endl;
cout << "English: " << Eitan.english << endl;
cout << "Entering a Kehillah: " << Eitan.enteringAKehillah << endl;
cout << "Geometry: " << Eitan.geometry << endl;
cout << "Hebrew: " << Eitan.hebrew << endl;
cout << "World History: " << Eitan.worldHistory << endl;
system("PAUSE");
int main()
Classes Eitan;
gradeEditor();
choice();
printGrades( Eitan );
【讨论】:
测试你的代码。还有一些问题需要修正。 我刚刚纠正了语法哥们没有验证逻辑 bvj 是正确的@yanivx。在第 93 行,我收到了这个错误:“error C4700: uninitialized local variable 'eitan' used” @DJHead-On 那是因为你的逻辑是错误的。您永远不会初始化该变量。你必须确保你这样做。 @juanchopanza 你认为我怎么能做到?以上是关于C++ 的 main.cpp 中的结构声明问题的主要内容,如果未能解决你的问题,请参考以下文章