Re: Classes and structures
Quote:
Originally posted by mcorn
Hello. I am having trouble accessing a structure from a class. I don't think I'm using the dot operator correctly. Could somebody help me with this issue? I've commented where the error occurs in the compiler. Thanks!
Code:
#include<iostream.h>
#include<string.h>
using namespace std;
struct VehicleInfo
{
string make;
string model;
};
class CVehicle
{
protected:
VehicleInfo vinfo;
public:
CVehicle(VehicleInfo);
CVehicle(){};
void getinfo();
void writeinfo();
};
CVehicle::CVehicle(VehicleInfo info){vinfo=info;}
void CVehicle::getinfo()
{
cout<<"\nEnter Make: ";
cin>>vinfo.make;
cout<<"\nEnter Model: ";
cin>>vinfo.model;
}
void writeinfo(VehicleInfo vinfo)
{
cout<<"\nMake is: " << vinfo.make <<"."
<<"\nModel is: " << vinfo.model <<"." << endl;
}
int main()
{
CVehicle veh1(VehicleInfo info); // the trouble is here somewhere
veh1.info.writeinfo(); //or here...I don't know what to do.
//h1.getinfo();
//h1.writeinfo();
return 0;
}
<iostream.h> is depreciated, use <iostream>
<string.h> is depreciated and should be replaced with <cstring>, but that's not even your problem in this case -- you shouldnt have included <string.h> in the first place. the stl string class is in <string>