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

main.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);
}


getQuestion.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'


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

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

最佳答案

#define getQuestion

int getQuestion () {


这里发生的是,您将字符串“ getQuestion”的#define设置为空白,然后使用int getQuestion()将其转换为int (),现在这已经没有意义了。这是导致您收到错误的原因,一旦解决此问题,我希望指出的其他问题也会出现。

制作宏时的一个很好的经验法则是使它与几乎没有匹配其他名称(在这种情况下为函数)的机会很小。通常,它们都是大写字母,既可以帮助实现这一目标,又可以使您看起来很明显,您正在处理宏。因此,在这种情况下,包含保护可以命名为GETQUESTION_H

07-26 09:21