#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    5
    Rep Power
    0

    Question C++ : const after member function


    #include<iostream>
    using namespace std;

    class Student{
    public:
    int age;
    int rollNo,marks;
    string name;
    void AddEntry();

    void Display(Student s[],int)const;


    Student();
    ~Student();
    };

    Student::Student(){

    age = 22;
    }

    Student::~Student(){
    }

    void Student::AddEntry(){

    cout << "Enter Roll Number: ";
    cin >> rollNo;

    cout << "Enter Name: ";
    cin >> name;

    cout << "Enter Marks: ";
    cin >> marks;

    cout << endl;
    }

    void Display(Student s1[],int entry)const{
    int i;
    for(i=0;i<entry;i++)
    cout << s1[i].rollNo << " " << s1[i].age << " " << s1[i].name << " " << s1[i].marks << "\n\n";
    }

    int main(){

    int i,entry;
    cout << "Enter No of Entry : ";
    cin >> entry;

    Student arry[entry];

    for(i=0;i<entry;i++)
    arry[i].AddEntry();

    Display(arry,entry);

    }

    error: non-member function 'void Display(Student*, int)' cannot have cv-qualifier|

    why and how can I solve it?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2009
    Posts
    45
    Rep Power
    7
    void Student::Display(Student s1[], int entry)
    Last edited by DRK82; August 27th, 2013 at 06:36 AM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    5
    Rep Power
    0
    i had made that change but it's still not working???
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2009
    Posts
    45
    Rep Power
    7
    Because you are calling it like a non-member funtion. You have to decide if Display should be a member function of Student class or not.
    If it should be member:
    Code:
    class Student
    {
        static void Display(Student s[], int); // declaration
    };
    
    void Student::Display(Student s1[], int entry) // definition
    {
    }
    
    Student::Display(arry, entry); // call
    Otherwise:
    Code:
    class Student
    {
    };
    
    void Display(Student s[], int); // declaration
    
    void Display(Student s1[], int entry) // definition
    {
    }
    
    Display(arry, entry); // call
    Last edited by DRK82; August 27th, 2013 at 06:38 AM.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    5
    Rep Power
    0
    my main problem with const .
    using that how can i make my program to run????
    what r the options??
  10. #6
  11. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,595
    Rep Power
    4207
    Define what is not working. Do you understand what adding the const at the end of the function does, or are you just sprinkling keywords randomly hoping for something to work?
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    40
    Rep Power
    18
    You program will work with a small change.

    //definition
    Code:
    void Student::Display(Student s1[],int entry)const{ ...
    //call
    Code:
    Student t;
    t.Display(arry,entry);
    Did you forget to assign value to 'age' in 'AddEntry()'?


    I think, it will be better if you define Display() one time for each object.

    Code:
    class student{
        ..
        void Display()const;
        ..
    };
    ...
    ...
    void Student::Display()const{
        cout << rollNo << ' '<< age << ' ' << name << ' '<< marks << endl;
    }
    You can use it nicely for array of objects in main function.
    Code:
        for(i=0;i<entry;i++)
            arry[i].Display();
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    5
    Rep Power
    0

    Smile Thanks to all!!!


    :)

IMN logo majestic logo threadwatch logo seochat tools logo