如何将一个ostream指针指向另一个?

时间:2017-11-04 03:37:31

标签: c++ vi

我试图将ostream指针地址传递给函数,并且它一直返回指针错误。什么是正确的分配方式(设置ostream指针?

代码:

头文件中的声明:

static ostream *out ;

在实施文件中初始化为:

ostream * Vehicle :: out = &cout; 

ostream指针" setter"功能

void MotorVehicle :: setOut(ostream &o){

        out = &o;

}

void MotorVehicle :: print(){

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

继承的类打印函数

void Truck :: setcargoCapacity(){ //other assignments are done same. 

        cin >> cargoCapacity;

}

void Truck :: print(){

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

}

void Truck :: read(){

        cout << "Please enter cargo capacity for this Vehicle:  " << endl;
        setcargoCapacity();
        cout << "Please enter name for this Vehicle:  " << endl;
        setName();
        cout << "Please enter make for this Vehicle:  " << endl;
        setMake();

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

        cout << "Please enter MPG for this Vehicle:  " << endl;
        setMpg();

}

主要实施

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

#include "car.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;    // 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;


   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;
}

ostream传递给函数并设置它的理想方法是什么?当*out尝试在main()中打印时,它会崩溃并返回指针错误。任何帮助将不胜感激。

谢谢!

0 个答案:

没有答案