I'm trying to print out structs that I have made inside a function, and since these must be available outside of the function I have used pointers. I have declared the struct and functions like this in the header file:
struct Car{
double wheelDiam;
int numberOfWheels;
string brand;
};
void makeCars();
ostream & operator<<( ostream & out, const Car & elem );
void printCar( Car car);
The function that makes the cars looks like this:
void makeCars(){
Car *AstonMartin;
Car *Volvo;
Car *Audi;
Audi->numberOfWheels = 4;
Audi->wheelDiam = 20.0;
Audi->brand = "Audi";
Volvo->numberOfWheels = 4;
Volvo->wheelDiam = 23.0;
Volvo->brand = "Volvo";
AstonMartin->numberOfWheels = 5;
AstonMartin->wheelDiam = 25.0;
AstonMartin->brand = "Aston Martin";
}
and I have made another function that prints out the struct (overloaded operator=):
ostream & operator<<( ostream & out, const Car & elem ){
out << elem.brand<<" "<<elem.numberOfWheels <<" "<<elem.wheelDiam<<endl;
return out;
}
void printCar( Car car){
cout << car << endl;
}
but when I call the functions in main() it doesn't print anything and I get an error message:
int main(){
makeCars();
printCar(*AstonMartin);
return 0
}
whats the correct way to do this?