-
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 :)
-
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?
-
well age and stuff is in the top
-
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 ();
}
-
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.
-
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.
-
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!
-
"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?
-
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 :)
-
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; }
-
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.
-
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?
-
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;
-
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.
-
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.