function implementation outside the class

Mar 19, 2011 at 9:06am
I have a class header with function declarations and want to implement these functions in a cpp file:

class User {
private:
/* username is unique */
string username;
string name;
string surname;
public:
User(string username);
User(string username, string name, string surname);
~User();
const string& getUsername() const;
void setUsername(const string& username);
const string& getName() const;
void setName(const string& name);
const string& getSurname() const;
void setSurname(const string& surname);
bool operator==(User& rhs) const;
friend ostream& operator<< (ostream& out, const User & user);
const string& getIdentifier() const { return getUsername(); }
};

I'm having difficulties with get functions. Maybe I have misunderstood something in this class, but how can I get the name if I have no parameters in a get function? Thank you.

Mar 19, 2011 at 9:33am
No parameters are written, but there is one: this pointer. You could write return this->name;. this is implicit, so you may just write return name;.
Mar 19, 2011 at 11:28am
Thank you:)
There is also another difficulty for me: I have implemented the second constructor as:

User::User(string username, string name, string surname){
this->username=username;
this->name=name;
this->surname=surname;
}

But I don't understand what am I supposed to do for the first constructor because I have got only username. Is it for replacing the old one with the new?
Mar 19, 2011 at 11:38am
Since the first constructor only takes one argument, for the rest, you'll either have to give some default values, or leave them uninitialized.
Mar 20, 2011 at 11:31am
Now I try to implement function in another class but using the first one. Is it possible? My second class is the following:

class Event {
private:
/* id is unique */
long id;
string name;
User *owner;
static int counter;
public:
Event(string name, User* owner);
~Event();
long getID() const;
const string& getName() const;
void setName(const string& name);
const User* getOwner() const;
void setOwner(User* owner);
bool operator< (const Event& event) const;
long getIdentifier() { return getID(); }
};

The function which I'm trying to implement is setOwner. I have wrote it that way:

void Event::setOwner(User* owner){
this->owner.username=owner.username;
this->owner.name=owner.name;
this->owner.surname=owner.surname;
}

But this seems incorrect because I get these errors:
event.cpp:34: error: request for member ‘username’ in ‘((Event*)this)->Event::owner’, which is of non-class type ‘User*’
event.cpp:34: error: request for member ‘getUsername’ in ‘owner’, which is of non-class type ‘User*’
event.cpp:35: error: request for member ‘name’ in ‘((Event*)this)->Event::owner’, which is of non-class type ‘User*’
event.cpp:35: error: request for member ‘getName’ in ‘owner’, which is of non-class type ‘User*’
event.cpp:36: error: request for member ‘surname’ in ‘((Event*)this)->Event::owner’, which is of non-class type ‘User*’
event.cpp:36: error: request for member ‘getSurname’ in ‘owner’, which is of non-class type ‘User*’

Is there a way to access another class?
Mar 20, 2011 at 11:47am
owner is a pointer. To access a member of the object it points to use ->.
Mar 20, 2011 at 12:09pm
I have replaced ' . ' with ' -> ' . Now I get these errors:

User.h:13: error: ‘std::string User::username’ is private
event.cpp:34: error: within this context
User.h:13: error: ‘std::string User::username’ is private
event.cpp:34: error: within this context
User.h:14: error: ‘std::string User::name’ is private
event.cpp:35: error: within this context
User.h:14: error: ‘std::string User::name’ is private
event.cpp:35: error: within this context
User.h:15: error: ‘std::string User::surname’ is private
event.cpp:36: error: within this context
User.h:15: error: ‘std::string User::surname’ is private
event.cpp:36: error: within this context

Mar 20, 2011 at 12:28pm
What the error said. You either have to make those members public or use appropriate get methods.
Mar 20, 2011 at 1:33pm
I try to use get methods. I have used getName(),getUsername() and getSurname() functions that I have implemented for the User class, but it does not work, I get the same errors :( What I have done is:

void Event::setOwner(User* owner){
this->owner->username=owner->getUsername();
this->owner->name=owner->getName();
this->owner->surname=owner->getSurname();
}
Mar 20, 2011 at 1:39pm
You directly accessed members on both sides of =, so you need both setters and getters.
Mar 20, 2011 at 3:15pm
thank you very much:)
Mar 21, 2011 at 8:04pm
"You directly accessed members on both sides of =, so you need both setters and getters. " what did you mean with this sentence explicitly? hamsterman.
Mar 21, 2011 at 8:45pm
zulfikar, consider struct Object{ private: int x; };. Now if in your main() you have Object o, and write o.x, you'll get an error saying that you can't access private members. Thus you have to write two functions. One for getting x : int Object::GetX(){ return x; } and one for setting it : void Object::SetX(int i){ x = i; }. Now while code o.x = 5; int a = o.x; was not correct, o.SetX(5); int a = o.GetX(); is fine. You could also write a getter which returns a reference: int& Object::Get(){ return x;} and then in main() : o.Get() = 5; int a = o.Get();.
Note, however, that none of getters or setters in this post so far are meaningful. They do not provide any encapsulation as x can be freely given any value. If you didn't have setter at all, or if it was void Object::SetX(int i){ if(i > 0) x = i; else x = 0; }//allows x to have only non-negative values it would make much more sense to have them (as opposed to making x public).
Mar 21, 2011 at 9:00pm
thank you hamsterman and as a result what will be the correct code related to john andres's setter function? :

void Event::setOwner(User* owner)
{
this->owner->username=owner->getUsername();
this->owner->name=owner->getName();
this->owner->surname=owner->getSurname();
}
Mar 21, 2011 at 10:06pm
1
2
3
4
5
void Event::setOwner(User* u)//it is best to avoid arguments with same names as members
{
owner->SetUsername( u->getUsername() );
//etc.
}
Mar 21, 2011 at 10:23pm
Thanks a lot hamsterman worked fine!
Mar 21, 2011 at 10:43pm
Hey zulfikar, just a request, if you have a second when you ask your next question, could you possibly throw your code in [ code ] tags [ / code ]?

(edit:) Thanks mate.
Last edited on Mar 21, 2011 at 11:11pm
Mar 21, 2011 at 11:07pm
ok i will do it ultifinitus thanks for your reminder...
Topic archived. No new replies allowed.