Aug 27, 2013 at 6:52am UTC
#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?
Aug 27, 2013 at 6:54am UTC
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 Aug 27, 2013 at 6:54am UTC
Aug 27, 2013 at 6:56am UTC
i had changed and i got this error:
|error: 'Display' was not declared in this scope|
Aug 27, 2013 at 7:11am UTC
what is problem with const???
Aug 27, 2013 at 7:59am UTC
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 Aug 27, 2013 at 8:15am UTC
Aug 27, 2013 at 8:49am UTC
can't I direct call Display method?
Last edited on Aug 27, 2013 at 8:57am UTC
Aug 27, 2013 at 9:02am UTC
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 Aug 27, 2013 at 9:05am UTC