Thread: Classes and structures

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    34

    Classes and structures

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

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Code:
    typedef struct _VehicleInfo
    {
    	string make;
    	string model;
    } VehicleInfo;
    
    //or else you have to declare it as:
    
    struct VehicleInfo vinfo;
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    don't typedef structs in C++, only in C. The only difference between structs and classes in C++ is the default access rights of the members: public in structs, private in classes.

    The problem is here:

    veh1.info.writeinfo();

    It should be

    veh1.vinfo.writeinfo();

    as info is the information struct passed in, not the information struct that is a member of veh1. vinfo is the information struct in veh1, and all instances of CVehicle for that matter.

  4. #4
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946

    Re: Classes and structures

    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>
    hello, internet!

  5. #5
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Originally posted by FillYourBrain
    Code:
    typedef struct _VehicleInfo
    {
    	string make;
    	string model;
    } VehicleInfo;
    
    //or else you have to declare it as:
    
    struct VehicleInfo vinfo;
    Elad, this is why I said that. he will need "struct VehicleInfo" if he must use a struct.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  6. #6
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by FillYourBrain
    Elad, this is why I said that. he will need "struct VehicleInfo" if he must use a struct.
    No, that's in C, not C++

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Sorry, FillYourBrain, but I disagree wholeheartedly. mcorn's declaration of the struct VehicleInfo and it's use in CVehicle is correct C++ syntax. Your syntax is appropiate in C, but only optional in C++ (it should work, but it doesn't have to be done that way, and IM(humble)O it shouldn't be). Again, in C++ the ONLY difference between keyword struct and class is the default member access.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by elad
    don't typedef structs in C++, only in C.
    There is no harm in providing the keyword 'struct', or 'class' for that matter. C++ allows you to leave it off. However, providing it does no harm. Additionally, using 'typedef' also does no harm. It's perfectly valid in C++ do typedef whatever you want. The same goes for #define.

    The difference between C and C++ is that C++ allows you to omit the keyword. Providing it does no harm.

    [edit]
    I see you clarified yourself as I was typing. Exactly. It's optional, not required, but it does no harm if it's there.
    [/edit]

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Jul 2002
    Posts
    34
    Thanks all! Here is still what I get.When I tried veh1.vinfo.writeinfo(); as elad suggested here is the error message that I get. (I using Visual C++ 6.0 if that makes a difference.)

    er.cpp(9) : error C2228: left of '.vinfo' must have class/struct/union type
    C:\C++\TermTest\driver.cpp(9) : error C2228: left of '.writeinfo' must have class/struct/union type
    Error executing cl.e

    I've tried every combination that I can think of and I still get relatively the same error messages. When I use the original line (veh1.info.writeinfo(); ) I only get one error that says : error C2228: left of '.info' must have class/struct/union type. Any suggestions?

  10. #10
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    You had a few problems.

    Your writeinfo method was never defined -- you instead made a function called writeinfo and defined it.

    You also weren't understanding passing arguments to your constructor -- you have to pass an instance of an object when it takes a parameter of an object, just like if it were an int you'd pass an int.

    IE if a functions declaration is

    void AFunc( int a );

    you DONT call it by doing

    AFunc( int a );

    you'd call it like

    AFunc( 3 );

    or

    AFunc( var ); // where var would be a variable of type int

    // etc.

    I gave 3 examples of constructing the object and using the methods and i fixed your method/function problem with writeinfo

    Code:
    #include<iostream>
    #include<string>
    
    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 CVehicle::writeinfo()
    {
        cout<<"\nMake is: " << vinfo.make <<"."
            <<"\nModel is: " << vinfo.model <<"." << endl;
    }
    
    
    int main()
    {
        CVehicle veh1;
        CVehicle veh2( VehicleInfo() );
    
        VehicleInfo InitialData;
    
        InitialData.make = "Blah";
        InitialData.model = "Poop";
    
        CVehicle veh3( InitialData );
    
        veh1.getinfo();
        veh1.writeinfo();
    
        veh2.getinfo();
        veh2.writeinfo();
    
        veh3.writeinfo();
    
        return 0;
    }

  11. #11
    Registered User
    Join Date
    Jul 2002
    Posts
    34
    Thanks...I somewhat understand my error!
    Also, If I wanted to create child class, do I just need the class to inherit it in order to use the same stucture?
    something like

    CNewclass: public CVehicle

  12. #12

  13. #13
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356
    Why would one use structures in a c++ program since classes are available ?
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

  14. #14
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Why not? There's no real reason that they shouldn't. They can do exactly the same things.

  15. #15
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356
    Since both of them are the same thing, then why use structs sice classes are defined for C++ .I mean isnt classes a superior strct .Then why use it .I am a c programmer learning C++ so i just wanted to know if it does make any different.
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about classes and structures.
    By RealityFusion in forum C++ Programming
    Replies: 19
    Last Post: 08-30-2005, 03:54 PM
  2. Replies: 1
    Last Post: 06-17-2005, 07:31 AM
  3. Classes and Structures.
    By jrahhali in forum C++ Programming
    Replies: 6
    Last Post: 03-28-2004, 05:03 PM
  4. Structures, Unions and Classes
    By Makoy in forum C++ Programming
    Replies: 2
    Last Post: 02-23-2004, 02:57 PM
  5. structures fooloing to look like classes
    By samsam1 in forum C++ Programming
    Replies: 4
    Last Post: 01-18-2003, 11:43 AM