2016-04-22 80 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