0

I am new to C++ OOP concepts. The problem I am having currently with the string declaration in getter and setter function. I use eclipse IDE and the error I get is

error: cannot convert 'Student::getname' from type 'std::__cxx11::string (Student::)() {aka std::__cxx11::basic_string<char> (Student::)()}' to type 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}'
return getname;

The code is as follows:

#include<iostream>
#include<string.h>
using namespace std;

class Student
{
private:
    int rollno;
    string name;
    string school;
public:

void setrollno(int i)
{
    rollno =i;
}
void setname(string n)
{
    name =n;
}
void setschool(string s)
{
    school =s;
}
int getrollno()
    {
        return rollno;
    }
string getname()
{
    return getname;
}
string getschool()
{
    return school;
}
};
int main()
{
    Student A;
    A.setrollno(3);
    cout << A.getrollno();
    A.setname("vinod");
    cout << A.getname();
    A.setschool("carmel");
    cout << A.getschool();
}

Could anyone tell me what the problem is?

Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521
vinod
  • 21
  • 5

1 Answers1

5

The name of your name member variable is name and not getname. You should change the getname() function to return the proper member variable:

string getname()
{
    return name;
}

Also I suggest that you look at const and reference to improve your code. for example you could return/pass some const reference (&) to avoid copy. Here is a good post that explains it: Passing std::string by Value or Reference

Community
  • 1
  • 1
Jérôme
  • 8,016
  • 4
  • 29
  • 35