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?
Your Display function is not a member function the way it is written. Remember to write out the fully scoped name:
void Student::Display(Student s1[], int entry)const{//...
Last edited on
i had changed and i got this error:

|error: 'Display' was not declared in this scope|

I am assuming you are referring to your call to Display at the end of main()?

Member functions cannot be used without an object. Just as you had to call AddEntry() with an object, you must do so with Display().

However, I think you would be better off making Display a non-member function.
1
2
3
4
5
class Student{
   //void Display(/*...*/)const; <--Take this out
};

void Display(/*...*/);
what is problem with const???
It'll be better and less confusing if you change your Display function to work more like your AddEntry function.

Also, since 'entry' is a compile time variable, you cannot construct an array of entry elements. Better to use a vector.

Also, since your main function is of type int main(), it's better to give a return value. Ofcourse, the compiler automatically returns a default value if you do not mention it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include<iostream>
#include <vector>
using namespace std;

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

  void Display() 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 Student::Display() const {
  cout << rollNo << " " << age << " " << name << " " << marks << "\n";
}

int main(){

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

  vector<Student> arry(entry);

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

  return 0;
}
Last edited on
can't I direct call Display method?
Last edited on
Then as already suggested by Daleth, you will have to make Display either a non-member function or a static member function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include<iostream>
#include <vector>
using namespace std;

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

  void Display() 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( vector<Student> s1,int entry ) { // a non-member function.
  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;

  vector<Student> arry(entry);

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

  return 0;
}
Last edited on
Topic archived. No new replies allowed.