简体   繁体   中英

Splitting up classes c++

I'm facing a problem for the past couple of days.

First of all, I had a project that I've done. But now I've to split it's classes.

Here's how I split the classes (a class as an example):

Header file:

#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

Alright, now here is the implementation:

#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 I've done the other classes the same exact way.

Now what's left is the MakeFile, here it is:

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 <−o@

Now when I try running make it brings up this message:

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

Can somebody explains it? or what am I doing wrong?

Thanks.


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;

}

You create Makefile with lot of mistakes:

Should be something like this:

project: Question.o 
    g++ -std=c++11 $^ -o $@

.cc.o:
    g++ -std=c++11 -c $< -o $@

add other dependencies into project in similar way, not forget defining main function in some of your .cc files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM