2016-04-22 12 views
0

Ich habe in den letzten Tagen ein Problem.Klassen aufteilen C++

Zuerst hatte ich ein Projekt, das ich gemacht habe. Aber jetzt muss ich seine Klassen teilen.

Hier ist, wie ich die Klassen aufgeteilt (eine Klasse als Beispiel):

Header-Datei:

#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 

Okay, hier ist jetzt die Umsetzung:

#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 

Ok, so Ich habe die anderen Klassen genauso gemacht.

Nun, was übrig bleibt, ist das Makefile, hier ist es:

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] 

Wenn ich jetzt make versuchen läuft es diese Meldung erscheint:

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 

Kann jemand erklärt es? oder was mache ich falsch?

Danke.


Edited:

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

Es gibt eine Menge Probleme hier. Aber um dein erstes Problem anzugehen: Hast du eine '' 'main.cpp'''? – fetherolfjd

+0

@fetherherolfjd jawohl, mein Herr! und ich habe jede Header-Datei darin eingefügt. – PhpLover1337

+0

Vielleicht lesen Sie über Referenzen und const könnte gut sein (zusammen mit dem Rest des Buches) –

Antwort

2

Sie erstellen Makefile mit vielen Fehlern:

sollen wie folgt sein:

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

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

hinzufügen andere Abhängigkeiten in project in ähnlicher Weise, nicht vergessen, main Funktion in einigen Ihrer .cc Dateien zu definieren.

+0

Ich konnte nicht verstehen, wie ich 'main' Funktion in anderen' .cc' Dateien definieren soll? – PhpLover1337

+0

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

+0

Ich kenne diesen Teil xD Ich entschuldige mich, was ich frage ist, wo soll ich es tun? weil ich 'main' bereits in' main.cc' habe. Wo sollte ich es sonst noch schreiben? – PhpLover1337