2016-04-22 175 views
0

我在过去几天面临一个问题。拆分类C++

首先,我做了一个项目。但现在我必须拆分它的类。

下面是如何分裂类(A类为例):

头文件

#ifndef QUESTION_H 
#define QUESTION_H 
#include <string> 
#include <iostream> 
#include <fstream> 
#include "Answer.h" 
using namespace std; 

// Name -- hold a first and last name 
class Question { 
protected: 
    string type; // Type of the question, e.g MC or TF 
    string text; // Text of the question 
public: 
    // Default constructor 
    Question(); 

    // Getters and setters 
    string getType(); 
    string getText(); 
    void setType (string t); 
    void setText (string t); 

    // displayText -- Display the text of the question, unformatted at present 
    void displayText(); 

    // Template pattern -- algorithm in parent which does its work calling child methods 
    virtual void displayAnswers(); 
    virtual void display(); 

    // Virtual pure functions that must be implemented by each derived class 

    virtual int grade (Answer*);    // grade a given answer 
    virtual Answer* readAnswer(istream &);  // read a user's answer 
}; 

#endif 

好了,现在这里是实现:

#include "Question.h" 
#include <string> 
#include <iostream> 
#include <fstream> 
using namespace std; 

    Question::Question() { type = ""; text = ""; } 

    // Getters and setters 
    string Question::getType() { return type; } 
    string Question::getText() { return text; } 
    void Question::setType (string t) { type = t; } 
    void Question::setText (string t) { text = t; } 

    // displayText -- Display the text of the question, unformatted at present 
    void Question::displayText() { 
     cout << text; 
    } 
    // Template pattern -- algorithm in parent which does its work calling child methods 
    void Question::displayAnswers(){ }// Require derived classes to implement 
    void Question::display() { 
    Question::displayText(); 
    Question::displayAnswers(); // Call derived class's displayAnswers 
    } 

    // Virtual pure functions that must be implemented by each derived class 

    int Question::grade (Answer*){ return 0; }    // grade a given answer 
    Answer* Question::readAnswer(istream &){ return 0; } // read a user's answer 

好了,我以同样的方式完成了其他课程。

现在剩下的就是在Makefile,那就是:

project: Question MCQuestion TFQuestion Answer IntAnswer CharAnswer Main 
    g++ -std=c++11 Question MCQuestion TFQuestion Answer IntAnswer CharAnswer Main -o project 
.cc.o: 
    g++ -std=c++11 -c <−[email protected] 

现在,当我尝试运行make它带来了这个消息:

g++  Question.cpp -o Question 
/usr/lib/gcc/i586-suse-linux/4.7/../../../crt1.o: In function `_start': 
/home/abuild/rpmbuild/BUILD/glibc-2.17/csu/../sysdeps/i386/start.S:113: undefined reference to `main' 
collect2: error: ld returned 1 exit status 
make: *** [Question] Error 1 

有人可以解释呢?或者我做错了什么?

谢谢。


编辑:

Main.cc:

#include <iostream> 
#include <fstream> 
#include <string> 
#include "Question.h" 
#include "MCQuestion.h" 
#include "TFQuestion.h" 
#include "Answer.h" 
#include "IntAnswer.h" 
#include "CharAnswer.h" 
#include <vector> 
using namespace std; 


int main() { 
    vector<Question *> questions;   // Holds pointers to all the questions 
    ifstream infile ("questions.txt"); // Open the input file 
    int totalCorrect = 0;     // Initialize the count from number of correct answers 

    // Read each question and place it into the questions vector 
    string questionType; 
    while (getline (infile, questionType)) { 
     if (questionType == "MC") {    
     MCQuestion *mc = new MCQuestion(); 
     mc->read(infile);  
     questions.push_back(mc); 
     } 
     else if (questionType[0] == 'T' or questionType[0] == 'F') { 
     TFQuestion* tf = new TFQuestion(); 
     tf->read(infile); 
     tf->setAnswer(questionType[0]); 
     questions.push_back(tf); 
     } 
     else { 
     cout << "Input file is corrupt. Expected to find MC, T or F; found \"" << questionType << "\" instead." << endl; 
     } 
    } 
    infile.close(); 

    // Pose each question, read and grade answers, tally total 

    int questionNo = 0; 
    for (auto &question: questions) { 

     // Pose the question 
     questionNo++; cout << questionNo << ". "; 
     question->display(); 

     // Get the user's answer 
     Answer* ans = question->readAnswer(cin); 

     // Grade it and increment total 
     int correct = question->grade(ans); 
     totalCorrect = totalCorrect + correct 

     // Inform the user as to whether or not they got the question correct 
     cout << "Your answer was " << (correct?"":"not ") << "correct\n" << endl; 

    } 

    // Print the overall score 
    cout << "Your overall score is " << totalCorrect << "/" 
     << questions.size() << endl; 

    return 0; 

} 
+0

这里有很多问题。但要解决你的第一个问题:你有'''main.cpp'''吗? – fetherolfjd

+0

@fetherolfjd是的先生!并且我在其中包含了每个头文件。 – PhpLover1337

+0

也许阅读关于引用和const可能是好的(连同本书的其余部分) –

回答

2

创建Makefile有很多错误的:

应该是这样的:

project: Question.o 
    g++ -std=c++11 $^ -o [email protected] 

.cc.o: 
    g++ -std=c++11 -c $< -o [email protected] 

以类似的方式将其他依赖关系添加到project中,而不要忘记在您的某些.cc文件中定义main函数。

+0

我无法理解我想如何在其他'.cc'文件中定义'main'函数? – PhpLover1337

+0

@ PhpLover1337 http://en.cppreference.com/w/cpp/language/main_function – fghj

+0

我知道这部分xD我道歉,我问的是我应该在哪里做?因为我已经在'main.cc'中拥有'main'了。我应该在哪里写它? – PhpLover1337