无法定位分段故障。在构造函数中分配内存?

时间:2017-11-03 02:34:57

标签: c++

我已经在这里工作了几个小时,但我似乎无法找到核心转储的来源。我将它限制在主构造函数和几个函数中但我无法找到错误的来源。任何帮助,将不胜感激。 析构函数

Vehicle:: ~Vehicle(){

        delete [] name;

}

基本标题

#ifndef VEHICLE_H
#define VEHICLE_H

#include <iostream>
#include <string>
using namespace std;

class Vehicle{
        protected:
                char *name;
                static ostream *out ;
        public:
                Vehicle();
                Vehicle(string &n);
                Vehicle (const Vehicle & b);
                ~Vehicle();
                Vehicle& operator =(const Vehicle &b);
                virtual void print(void) = 0;
                virtual void read(void) = 0;

};


#endif // VEHICLE_H

基础实现中的构造函数

Vehicle :: Vehicle(){

        name = new char[1];
        strcpy(name[0], "");
}

Vehicle :: Vehicle(string &n){

        int len = n.length()+ 1;
        name = new char[len];
        strcpy(name,n.c_str());

}

Vehicle :: Vehicle(const Vehicle & v){

        int len = strlen(v.name)+ 1;
        name = new char[len];
        strcpy(name,v.name);

}

继承类的标头

#ifndef MOTORVEHICLE_H
#define MOTORVEHICLE_H

#include <cstring>
#include "vehicle.h"

class MotorVehicle: public Vehicle{
        protected:
                string make;
                string model;
                double mpg;
        public:
                MotorVehicle();
                MotorVehicle (const string &n,const string &m = "",const string &md = "", const double &mp = 0.0);

                //accessor functions
                string getName()const;
                string getMake()const;
                string getModel()const;
                double getMpg()const;
                ostream & getOut();

                //mutator functions
                string setName();
                void setMake();
                void setModel();
                void setMpg();
                void setOut(ostream & o);

                //virtual functions
                void print();
                void read();

};

#endif // MOTORVEHICLE_H

继承的类。

    #include "motorVehicle.h"
    #include <string>
    #include <iostream>

MotorVehicle:: MotorVehicle(): Vehicle(), make(""), model(""), mpg(0.0){
        make = "";
        model = "";
        mpg = 0.0;

}

MotorVehicle :: MotorVehicle(const string &n, const string &m, const string &md, const double &mp){

        int len = n.length()+ 1;
        delete [] name;
        name = new char[len];
        strcpy(name,n.c_str());

        make = m;
        model = md;
        mpg = mp;

}

//accessor functions

string MotorVehicle :: getName()const{

        string temp(name);
        return temp;

}

string MotorVehicle :: getMake()const {

        return make;

}

string MotorVehicle :: getModel()const {

        return model;

}

double MotorVehicle :: getMpg()const {

        return mpg;

}

ostream & MotorVehicle  :: getOut(){

        return *out;
}

//mutator functions
string MotorVehicle :: setName(){
        cerr << "dododd" <<endl;

        string nm1;
        cin >> nm1;

        int len = nm1.length()+ 1;
        delete [] name;
        name = new char[len];
        strcpy(name,nm1.c_str());


}

void MotorVehicle :: setMake(){

        cin >> make;
}

void MotorVehicle :: setModel(){

        cin >> model;

}

void MotorVehicle :: setMpg(){

        cin >> mpg;
}

void MotorVehicle :: setOut(ostream & o){

        out = &o;
}

//virtual function
void MotorVehicle :: print(){

        *out << name << make << model << mpg <<" ";
}

void MotorVehicle :: read(){


        *out << "Please enter name for this Vehicle:  " << endl;
        setName();

        *out << "Please enter make for this Vehicle:  " << endl;
        setMake();

        *out << "Please enter model for this Vehicle:  " << endl;
        setModel();

        *out << "Please enter miles per gallon for this Vehicle:  " << endl;
        setMpg();


}

主要

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <typeinfo>
#include "motorVehicle.h"

// car.h & truck.h included twice to test #ifndef #define ... #endif structure
#include "car.h"
#include "car.h"
#include "truck.h"
#include "truck.h"

typedef vector<MotorVehicle *> vectorOfMotorVehicle;

using namespace std;

// prompt the user for the Vehicle type to create, and get the reply
string prompt(void);

int main() {
   vectorOfMotorVehicle v;         // store dynamically created MotorVehicles
                                   // or objects derived from MotorVehicles
   int           numVehicles = 0;  // denotes how many MotorVehicles we have
   string        reply;            // indicates type of MotorVehicle to build
   bool          error = false;    // set when a user response is in error
   string        outputLocation;   // where the output of print() will go
   ofstream     *out = NULL;
   MotorVehicle *m;

   // uncomment following line to test that Vehicle is an abstract base class
   // Vehicle theVehicle;

   // push a Vehicle into the vector so  first "real" Vehicle is at position 1
   m = new MotorVehicle("None");
   v.push_back(m);

   // chose where the output will go
   cout << "Where would you like the output?  ";
   cin  >> outputLocation;

   if (outputLocation == "stdout") {
      ; // no action to take, because stdout (i.e., cout) is the default
   } else if (outputLocation == "stderr") {
      v[0]->setOut(cerr);
   } else {
      out = new ofstream;
      out->open(outputLocation.c_str());
      if (out->fail()) {
         cerr << "Error:  error writing to " << outputLocation << endl;
         return 1;
      }
      v[0]->setOut(*out);
   }

   // get the type of Vehicle to create
   reply = prompt();

   // loop, reading vehicle descriptions, until a "quit" command is received
   while (reply != "quit") {

      // create the new MotorVehicle object and push it into the vector
      switch (toupper(reply[0])) {
        case 'T' : m = (MotorVehicle *) (new Truck);
                   v.push_back(m);
                   break;
        case 'C' : m = (MotorVehicle *) (new Car);
                   v.push_back(m);
                   break;
        case 'Q' : reply = "quit";
                   continue;
        default  : cerr << "Incorrect response\n\n";
                   error = true;
      }

      // if no error, then we have a new Vehicle to initialize via read()
      if (!error) {
         numVehicles++;
         v[numVehicles]->read();
      }

      // reset error flag, and request a new Vehicle type
      error = false;
      reply = prompt();
   }

   // report on what Vehicles were created to test read() and print()
   for (int i = 0; i <= numVehicles; i++) {
      //*out << "Vehicle " << i << endl;

      // print the Vehicle characteristics (attributes)
      v[i]->print();

      //*out << endl;

      // free the storage for this Vehicle
      delete v[i];
   }

   // if we opened an output file, then close it
   if (out != NULL) {
      out->close();
      delete out;
   }

   return 0;
}

// prompt the user for the Vehicle type to create, and get the reply
string prompt() {
   string reply;    // the user reponse to the prompt

   // prompt for and get user response
   cout << "\nWhich type of vehicle would you like to initialize"
        << "\n--car or truck (or \"quit\" to exit):  ";
   cin  >> reply;

   return reply;
}

我试图尽可能地缩小它,但是其他两个继承的类遵循类似的继承,所以我省略了它。任何帮助将非常感激。再次感谢

0 个答案:

没有答案