0

我正在尝试为学校生成一个测验程序。我的代码如下

主.cpp:

#include <cmath>
#include <ctime>
#include <string>

#include "getQuestion.h"

using namespace std;

int main() {
int mainMenuChoice;
ifstream Bibliography;
//string easyBib;
ifstream easyBib;
ifstream inputFile;

cout << "Quiz Menu\n\n";
cout << "1. Play Game!\n";
cout << "2. Bibliography\n";
cout << "3. Developer Info\n";
//cout << "4. Admin Menu\n";
cout << "4. Exit\n";
cout << "Menu Choice: ";
cin >> mainMenuChoice;

switch (mainMenuChoice) {
    case 1:
        break;
    case 2:
        inputFile.open("Bib.txt");
        inputFile >> easyBib;
        cout << easyBib << endl;
        break;
    case 3:

        break;
    /*case 4:
        break;*/
    case 4:
        cout << "Thank you for playing!" << endl;
        return(0);
    default:
        cout << "Sorry, Invalid Choice!\n";
        return(0);

}
return(0);
}

获取问题.h:

#ifndef getQuestion
#define getQuestion

int getQuestion () {

ifstream Question;
ifstream Linker;                //Linker for 2 flies
ifstream Answer;
string A;
string L;               //Linker for 2 flies
string Q;
int line;


Question.open("Question.txt");
Question >> Q;
Answer.open("Answer.txt");
Answer >> A;
Linker.open("Linker.txt");
Linker >> L;


getline(Question,line);
number_of_lines++;
#endif

编译器错误:

Build Social Studies of project Social Studies with configuration Debug

CompileC "build/Social Studies.build/Debug/Social Studies.build/Objects-normal/x86/main.o" main.cpp normal x86_64 c++ com.apple.compilers.gcc.4_2
cd "/Users/student/Desktop/Social Studies"
setenv LANG en_US.US-ASCII
/Developer/usr/bin/gcc-4.2 -x c++ -arch x86_64 -fmessage-length=0 -pipe -Wno-trigraphs -fpascal-strings -fasm-blocks -O0 -Wreturn-type -Wunused-variable -isysroot /Developer/SDKs/MacOSX10.6.sdk -mfix-and-continue -fvisibility-inlines-hidden -mmacosx-version-min=10.6 -gdwarf-2 -iquote "/Users/student/Desktop/Social Studies/build/Social Studies.build/Debug/Social Studies.build/Social Studies-generated-files.hmap" "-I/Users/student/Desktop/Social Studies/build/Social Studies.build/Debug/Social Studies.build/Social Studies-own-target-headers.hmap" "-I/Users/student/Desktop/Social Studies/build/Social Studies.build/Debug/Social Studies.build/Social Studies-all-target-headers.hmap" -iquote "/Users/student/Desktop/Social Studies/build/Social Studies.build/Debug/Social Studies.build/Social Studies-project-headers.hmap" "-F/Users/student/Desktop/Social Studies/build/Debug" "-I/Users/student/Desktop/Social Studies/build/Debug/include" "-I/Users/student/Desktop/Social Studies/build/Social Studies.build/Debug/Social Studies.build/DerivedSources/x86_64" "-I/Users/student/Desktop/Social Studies/build/Social Studies.build/Debug/Social Studies.build/DerivedSources" -c "/Users/student/Desktop/Social Studies/main.cpp" -o "/Users/student/Desktop/Social Studies/build/Social Studies.build/Debug/Social Studies.build/Objects-normal/x86_64/main.o"

In file included from /Users/student/Desktop/Social Studies/main.cpp:8:
/Users/student/Desktop/Social Studies/getQuestion.h:4: error: expected unqualified-id before 'int'
/Users/student/Desktop/Social Studies/getQuestion.h:4: error: expected `)' before 'int'

谢谢。另外请不要吝啬,我是编程新手!我正在使用 Mac OS X v. 10.6.8 和 XCode 3.2.6 在有人要求我升级任何东西之前,我不是管理员(这是一台学校笔记本电脑)

编辑:感谢大家的帮助!代码中是否还有其他问题(与性能有关?)

4

2 回答 2

2
#define getQuestion

int getQuestion () {

这里发生的事情是你#define将字符串“getQuestion”设置为空白,然后你使用int getQuestion()哪个被转换为int ()现在没有意义的.. @chris 很好。这是您遇到错误的原因,一旦您解决了这个问题,我希望会出现指出的其他问题。

制作宏时的良好经验法则是使其与另一个名称匹配的机会很小甚至没有机会,在这种情况下是一个函数。它们通常都是全部大写,既可以帮助实现这一点,又可以使您在处理宏时看起来很明显。GETQUESTION_H因此,在这种情况下,例如可以命名包含保护。

于 2012-12-16T01:29:37.340 回答
0

您的编译器错误的问题是#define 与函数同名。如果将预处理器语句更改为

#ifndef getQuestion_h
#define getQuestion_h

您的编译器错误将得到解决。

还有许多其他错误会导致其他编译器错误,但因为这是家庭作业,您应该先尝试解决这些错误。

作为风格问题,您应该只在 .h 文件中包含函数声明。您应该在另一个文件中定义 getQuestion 函数体,例如 getQuestion.cpp。

于 2012-12-16T01:31:21.877 回答