Sign in to follow this  
3 14159MP

C++ member function calling member functions (solved)

Recommended Posts

This program worked fine. Then I tried to make the show() function. It should do the same thing as the commented line above the show() call but I get these errors. void student::show()' is initialized like a variable void student::show()' outside of class is not definition cannot call member function `std::string student::getName() const' without object The last one I think is because the compiler doesn't know what student to work on. Can someone tell why my show() function doesn't work.
#include <cstdlib>
#include <iostream>

using namespace std;

class student
{
public:
       student (string name = " ",string lnumber = " ",int age = 0);
       void    setName(string n);
       string  getName()const;
       void    setAge(int a);
       int     getAge()const;
       void    setLnumber (string l);
       string  getLnumber()const;
       void    show();
       
private:
        string name;
        string lnumber;
        int age;
};


int main()
{
    
    student x ( "Fiddle","L001122", 22);
    student y;

    //cout << x.getName() << " " << x.getAge() << " " << x.getLnumber() << endl;
    x.show();
    //cout << y.getName() << " " << y.getAge() << " " << y.getLnumber() << endl;
    y.show();
    y.setName("Piano");
    y.setAge (40);
    y.setLnumber ("L012345");
    //cout << y.getName() << " " << y.getAge() << " " << y.getLnumber() << endl;
    y.show();
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

student::student (string n,string l,int a)
{
name = n;
lnumber = l;
age =  a;
}


void student::setAge(int a)
{
age = a;
}

int student::getAge() const
{
return age;    
}

void student::setName(string n)
{
name = n;
}

string student::getName() const
{
return name;    
}

void student::setLnumber (string l)
{
lnumber = l;
}

string  student::getLnumber() const
{
return lnumber;        
}

void student::show()
(
cout << student::getName() const;
cout << student::getLnumber() const;
cout << student::getAge() const;
)



[Edited by - 3 14159MP on March 18, 2007 5:30:28 PM]

Share this post


Link to post
Share on other sites
Your function is enclosed by parentheses instead of curly brackets. Change


void student::show()
(
cout << student::getName();
cout << student::getLnumber();
cout << student::getAge();
)




to this


void student::show()
{
cout << student::getName();
cout << student::getLnumber();
cout << student::getAge();
}


Share this post


Link to post
Share on other sites
In addition to { }, what you're doing inside show() is incorrect. AS written, you are trying to call non-member functions within the student namespace (or static functions within the student class), whereas you really want to call the member functions for the object show was called on:

void student::show() const // NOTE change the declaration to match this
{
cout << this->getName() << " ";
cout << this->getLnumber() << " ";
cout << this->getAge() << endl;
}

You could leave out the this-> as it is implicit, and it's a question of personal preference whether to use it. The point is, member functions have an implicit parameter of the object the function is being called on, and here you need to call the other functions on the same object.

Share this post


Link to post
Share on other sites
Quote:
Original post by SunTzu
AS written, you are trying to call non-member functions within the student namespace (or static functions within the student class), whereas you really want to call the member functions for the object show was called on:


Actually, getName() is a shorthand notation for student::getName(), which in turn is a shorthand notation for this->student::getName(). So, the proper function would be called in all four versions (yours included), although both the this and the student qualifications are not necessary (because the situation is unambiguous).

For instance, it's perfectly permissible to call Base::Member() in a Derived member function, without the this prefix.

Share this post


Link to post
Share on other sites
Quote:
Original post by SunTzu
In addition to { }, what you're doing inside show() is incorrect. AS written, you are trying to call non-member functions within the student namespace (or static functions within the student class), whereas you really want to call the member functions for the object show was called on:

void student::show() const // NOTE change the declaration to match this
{
cout << this->getName() << " ";
cout << this->getLnumber() << " ";
cout << this->getAge() << endl;
}

You could leave out the this-> as it is implicit, and it's a question of personal preference whether to use it. The point is, member functions have an implicit parameter of the object the function is being called on, and here you need to call the other functions on the same object.

His code is perfectly valid on this point. student::getName() will call the function getName belonging to the name space student. student is the class he's working with, and he's calling it from within another member function of student. So the call will call getName just like you called it without student:: or with this->.

Doesn't make any difference now, but with inheritance where multiple overrides of the, possibly virtual, getName function exists, you can with the name space resolution operator (::) explicitely select which version you want to call.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this