-1

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?

5
  • Welcome to StackOverflow. When entering your question please use the preview to see how it appears, and format code correctly using the formatting controls above the input box. Your original question was not displaying correctly and had to be fixed. Commented Feb 22, 2016 at 14:32
  • return getname; Is that what you intended? Commented Feb 22, 2016 at 14:35
  • Stop using accessors once and for all. Commented Feb 22, 2016 at 14:36
  • @NathanCooper: return name; is that what you intended? Your comment's code is exactly the same as in the question ;) Commented Feb 22, 2016 at 14:37
  • Replace #include<string.h> with #include<string>. string.h only declares the c-functions for character array manipulation. Commented Feb 22, 2016 at 14:42

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.