Как назначить один указатель ostream другому?

Я пытаюсь передать ostream адрес указателя в функцию, и он продолжает возвращать ошибку указателя. Каков будет правильный способ назначения (настройка ostream указатель?

Код :

Объявление в заголовочном файле:

static ostream *out ;

Инициализируется в файле реализации как:

ostream * Vehicle :: out = &cout;

функция указателя Ostream

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

Решение

Задача ещё не решена.

Другие решения

Других решений пока нет …

По вопросам рекламы [email protected]