Thread: Classes Continued.

  1. #1
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128

    Classes Continued.

    Sorry I could not figure out how to upload files.

    Anyways, I used some previous code and mix n matched some suggestions and got a whole pile of errors,

    Now im pretty weak in CPP atm so dont be surprised if they are the dumbest obvious errors ever haha

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Basically it seems that your prototypes don't match in various places:

    Code:
    User (const string name&, int age); //PS, & is misplaced
    User::User(const string& name, const string& age) : userName(name), userAge(age) { }
    
    const string& GetName();
    string User::GetName() { return userName; }
    
    int GetAge();
    string User::GetAge() { return userAge;}
    
    void setAge(int userAge);
    void User::setAge(const string& age) { userAge = age; }
    And then in main you are using a lot of variables which are not defined anywhere: where do you think name, age, and userName came from?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    well age and stuff is in the top

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    37
    main() is using variables that don't exist.

    Your class is still not right. The prototype now matches in the main file, but not in the classes.h file. Both files must be consistent throughout, and with each other. Also, you probably will want to use an int for the age, a string will work, however (1) it doesn't do math, and (2) it allows non-numbers to be stored in age.

    This is an example of how the main() function might be written:
    Code:
    #include <sstream>
    
    // ... your class stuff which probably should be in a different file anyway.
    
    main () {
    	string name;			// we need these for getline to work
    	int age;			// using an int
    
    	cout << "Enter your name";
    	getline (cin, name);
    
    	cout << "Enter your age";
    	string ageString;		// string
    	getline (cin, ageString);	// input age as string
    	stringstream ssAge (ageString);	// convert to int
    	ssAge >> age;
    
    	User user (name, age);		// again, using int, you'll have to change you classes first.
    
    	cout << "Name is: " << user.GetName ();
    	cout << "Age is:  " << user.GetAge ();
    }

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Implicit main is forbidden in C++.
    You must use int main.
    And don't forget using namespace std or prefix std:: to all library functions.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    Classes.h
    Code:
    //  Game Player Class START //
    //--------------------------//
    
    class User
    {
        public:
    
        User (const string name&, int age);
    
        const string& GetName();
        int GetAge();
    
        void setName(const string&  userName);
        void setAge(int userAge);
    
        private:
        string userName;
        int userAge;
    
    };
    gamestructure.cpp

    Code:
    using namespace std;
    
    User (const string name&, int age); //PS, & is misplaced
    User::User(const string& name, const string& age) : userName(name), userAge(age) { }
    
    const string& GetName();
    string User::GetName() { return userName; }
    
    int GetAge();
    string User::GetAge() { return userAge;}
    
    void setAge(int userAge);
    void User::setAge(const string& age) { userAge = age; }
    
    int main()
    {
    
    	string name;		// we need these for getline to work
    	int age;			// using an int
    
    	cout << "Enter your name";
    	getline (cin, name);
    
    	cout << "Enter your age";
    	string ageString;	    	        // string
    	getline (cin, ageString);	        // input age as string
    	stringstream ssAge (ageString);	    // convert to int
    	ssAge >> age;
    
    	User user (name, age);		        // again, using int, you'll have to change you classes first.
    
    	cout << "Name is: " << user.GetName ();
    	cout << "Age is:  " << user.GetAge ();
    }
    so thats waht i basicy got n it spitting lots of errors.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    1) All getters should be const.
    That means function such as
    int GetAge()
    really should be
    int GetAge() const

    2) Include your class definition before you implement the class.
    3) Remove the declarations in the .cpp file. They are global functions with the same name as the class so you don't want them, plus they're not implemented!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    "spittin lots of errors" is not exactly something we can help you with unless we know what the errors are.

    Code:
    const string& GetName();
    string User::GetName() { return userName; }
    
    int GetAge();
    string User::GetAge() { return userAge;}
    Why are you declaring a non-class function, that has no implementation, and then also calling a class implementation that has a different return type? And yes 'string &' is different from just 'string'.

    Code:
    User (const string name&, int age); //PS, & is misplaced
    So why not fix it instead of commenting it?

  9. #9
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    To be honest I am not totally sure, I kind of had one thing, and got told to change it and as Im fairly new to classes Im slightly confused

    Thanks for your replies but It is 2 am here, so I will be back in the morning to go through it properly

  10. #10
    Registered User
    Join Date
    Feb 2009
    Posts
    37
    Using const can properly can be confusing. I've never really gotten quite used to it myself.
    Code:
    int getAge () const {	// const at the end prevents 
    	age = 23;	// changing member data inside the method, like this line = error
    	return age;
    }
    
    const string& getName () const; 	// using references, also put const at beginning
    
    // somewhere inside the main() ...
    user.getName () = "Ross";		// the const prevents this from working
    This is the class using const properly:
    Interface:
    Code:
    class User {
    private:
    	string name;
    	int age;
    public:
    	User (const string& name, const int age);
    
    	const string& getName () const;
    	int getAge () const;
    
    	void setName (const string& name);
    	void setAge (const int age);
    };
    Implementation:
    Code:
    User::User (const string& name, const int age) : name (name), age (age) { }
    
    const string& User::getName () const { return name; }
    int User::getAge () const { return age; }
    
    void User::setName (const string& name) { this->name = name; }
    void User::setAge (const int age) { this->age = age; }

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Const is neither difficult nor confusing.
    Ask yourself these questions?
    1) Should this member function change any variables in the class? If no, then make it const. Getter functions are a prime example of functions that should be const.
    2) Shall the function that are receiving parameters change them? If no, then make them const. Setter functions are a prime example of these, since they don't change the parameters they receive, they only modify class variables mostly.
    3) If the function returns something, should the user be able to modify that data? If no, then make it return const. Getters are a prime example of this, since setting new data is what setters are for.

    Some additional rules and thoughts:
    - If a function is const, then it cannot return non-const variables (doing so would make the function break its promise that it shall make sure no changes are made to the class variables).
    - Const is not needed on parameters that are passed by value since a copy of that value is made anyway.
    - Only const functions can be called from inside a const function or on a const object. The reason is that the const function must ensure it does not change any member variables, and by calling a non-const member function, it can do just that. Thus, it's forbidden.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    Hey,

    Sorry for the late reply, I have had my exams over the last week so I have not had much spare time !

    Thanks greatly for the replies, its starting to get slightly more understanderble haha ( just the fact that im new to cpp)

    So I get the user class code now, but where does implementation go, does that go in the header, at the top of main?

    or where ever i use the user class in the main function?

    I have tried gettin the name from a input and what ever i try it says player does not name a type and this function is not declared.

    To define the class I did,

    User Player

    is that not correct?
    Last edited by Aliaks; 07-02-2009 at 03:45 AM.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The definition as a rule goes into its own header, and the implementation into its own .cpp file.
    To use the class, you must first include the class's header file.
    Then you can do
    User Player;
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    Code:
    #include <iostream>
    #include <sstream>
    
    //#include "functions.h"
    #include "classes.h"
    //#include "playerInvent.h"
    
    using namespace std;
    
    User::User (const string& name, const int age) : name (name), age (age) { }
    
    const string& User::getName () const { return name; }
    int User::getAge () const { return age; }
    
    void User::setName (const string& name) { this->name = name; }
    void User::setAge (const int age) { this->age = age; }
    
    int main()
    {
    
    User player
    
    player.SetName (cin,name);
    
    }
    Ok well I want something along the lines of this..
    I know it isnt right but I dont fully understand how the top part works.

  15. #15
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    Dude, top part? And also you are passing objects in your SetName() function that again, don't exist. I've never seen a user defined function pass in a cin object... You have passed two objects as arguments when your function only accepts one argument.

    Code:
    player.SetName(cin /*What the hell are you doing with this?*/
                    name); /*Where did you declare this variable?*/
    I think you should go back to basics.
    Last edited by legit; 07-04-2009 at 02:39 AM.
    MSDN <- Programmers Haven!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 05:38 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM