Vectors

Oct 16, 2017 at 8:53pm
I am trying to get my class to go into a vector I am using an older version of C++ and this was an optional assignment what i want to know is what i did wrong and how I can fix it please keep it simple so i can understand Thanks!
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream>
#include<vector>
using namespace std;



class student
{
	private:
		string name;
		double gpa;
		int rank, year;
	
	public:
		student()
		{

			/*cout << "Please enter the students GPA." << endl;
			cin >> gpa;
			cout << "Please enter the students rank for the year." << endl;
			cin >> rank;
			cout << "Please enter the year the student was attending." << endl;
			cin >> year;*/
			
		}
		void setName(string w)
		{
			name = w;
		}
		void setGpa(double x)
		{
			gpa = x;
		}
		void setRank(int y)
		{
			rank = y;
		}
		void setYear(int z)
		{
			year = z;
		}
		string getName()
		{
			return name;
		}
		double getGpa()
		{
			return gpa;
		}
		int getRank()
		{
			return rank;
		}
		int getYear()
		{
			return year;
		}
};

void fillVector(vector<student>&);
void printVector(const vector<student>&);

int main()
{	
	vector<student> allStudents;
	
	/*cout << "The students name is: " << student1.getName() << endl;
	cout << "The students GPA is: " << student1.getGpa() << endl;
	cout << "The students year and rank was: " << student1.getYear() << "(" << student1.getRank() << ")" << endl;*/
	
	return 0;
}

void fillVector(vector<student> & newAllStudents)
{
	string name;
	double gpa;
	int year, rank;
	
	cout << "How many students are there: " << endl;
	int numStudents;
	cin >> numStudents;
	
	for (int i = 0; i < numStudents; i++)
	{
		cout << "Enter student name: " << endl;
		cin >> name;
		cout << "Enter student GPA: " << endl;
		cin >> gpa;
		cout << "Enter student year: " << endl;
		cin >> year;
		cout << "Enter student rank: " << endl;
		cin >> rank;
		
		student newStudent(name, gpa, year, rank);
		newAllStudents.push_back(newStudent);
	}
	
}

void printVector(const vector<student> & newAllStudents)
{
	unsigned int size = newAllStudents.size();
	
	for (unsigned int i = 0; i < size; i++)
	{
		cout << "Student name: " << newAllStudents[i].getname() << endl;
		cout << "Student GPA: " << newAllStudents[i].getgpa() << endl;
		cout << "Student year and rank: " << newAllStudents[i].getyear << "(" << newAllStudents[i].getrank() << ")" << endl << endl;
	}
}
Oct 17, 2017 at 12:08am
> i want to know is what i did wrong
¿is there a problem with your code?
Oct 17, 2017 at 12:20am
That's what I want to know I get an error message on line 96 stating "[Error] no matching function for the call to 'student::student(std::string&, double&, int&, int&)'"

Then on line 108 - 110 i get the error "[Error] 'const class student' has no member 'getname/getgpa/getyear/getrank'"

I thought I did very well for my first attempt but i still cant get the program to run properly. I'm pretty sure it's something simple that im missing but I've been through it multiple times and cant figure it out
Oct 17, 2017 at 12:21am
my teacher said i shouldn't try to fill my vector dynamically on my first attempt at using vector but I still feel this way would be better
Oct 17, 2017 at 12:25am
should I put the vector<student> allStudents; in the constructor instead of in main? and then call student(); to main?
Last edited on Oct 17, 2017 at 12:26am
Oct 17, 2017 at 12:32am
Please ignore the commented out things as it was suppose to be a different program but if we were to take the optional assignment the commented out parts are not needed I just haven't deleted it yet.
Oct 17, 2017 at 12:37am
Good for listing the error message> I get the same:
a.cpp: In function 'void fillVector(std::vector<student>&)':
a.cpp:95:43: error: no matching function for call to 'student::student(std::__cxx11::string&, double&, int&, int&)'
   student newStudent(name, gpa, year, rank);
                                           ^
a.cpp:15:3: note: candidate: student::student()
   student()
   ^
a.cpp:15:3: note:   candidate expects 0 arguments, 4 provided


The error message is actually very specific: there is no function student::student(string&, double, int, int).

Looking through your code there is exactly one constructor for student: a default constructor taking no arguments. The compiler even gives you line numbers:

Line 95: attempting to call a function (constructor) that does not exist
Line 15: here is the only constructor I could find.

There are two ways to fix it. Create your object and use the setters to set the various pieces of information:

95
96
97
98
99
100
		student newStudent;
		newStudent.setName(name);
		newStudent.setGpa(gpa);
		newStudent.setYear(year);
		newStudent.setRank(rank);
		newAllStudents.push_back(newStudent);

Or add the desired constructor:

15
16
17
18
19
20
21
22
23
24
25
		student()
		{
			// snip
		}
		student(const std::string& name, double gpa, int year, int rank)
		{
			this->name = name;
			this->gpa = gpa;
			this->year = year;
			this->rank = rank;
		}

A similar issue exists for the error about "getname", etc. You do have functions named "getName", etc though.

Hope this helps.
Oct 17, 2017 at 12:44am
That's what I want to know I get an error message on line 96 stating "[Error] no matching function for the call to 'student::student(std::string&, double&, int&, int&)'"


Do you know what this is saying? You are calling a constructor in line 96 that doesn't exist. Either you need to call the constructor declared in lin e 15 (default constructor--no arguments) or you need to write another construtcor that takes a string, a double and 2 ints.

Then on line 108 - 110 i get the error "[Error] 'const class student' has no member 'getname/getgpa/getyear/getrank'"


This one is a little bit more subtle. You have a const vector& argument. When you index into this vector, you get const student objects. Only const member functions can be called on const objects. Because the student objects are const, non-const member functions cannot be called on them. The getName, getGpa, etc. functions are not const functions. Add the word "const" right after the argument list: string getName() const {...} to take care of these problems
Last edited on Oct 17, 2017 at 12:45am
Oct 17, 2017 at 1:09am
That helped alot but now im getting the error "[Error] passing 'const student' as 'this' argument of 'std::string student::getName()' discards qualifiers [-fPermissive]"

updated code:

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <iostream>
#include<vector>
using namespace std;



class student
{
	private:
		string studentName;
		double studentGpa;
		int studentRank, studentYear;
	
	public:
		student()
		{

			/*cout << "Please enter the students GPA." << endl;
			cin >> gpa;
			cout << "Please enter the students rank for the year." << endl;
			cin >> rank;
			cout << "Please enter the year the student was attending." << endl;
			cin >> year;*/
			
		}
		void setName(string name)
		{
			studentName = name;
		}
		void setGpa(double gpa)
		{
			studentGpa = gpa;
		}
		void setRank(int rank)
		{
			studentRank = rank;
		}
		void setYear(int year)
		{
			studentYear = year;
		}
		string getName()
		{
			return studentName;
		}
		double getGpa()
		{
			return studentGpa;
		}
		int getRank()
		{
			return studentRank;
		}
		int getYear()
		{
			return studentYear;
		}
};

void fillVector(vector<student>&);
void printVector(const vector<student>&);

int main()
{	
	vector<student> allStudents;
	
	
	/*cout << "The students name is: " << student1.getName() << endl;
	cout << "The students GPA is: " << student1.getGpa() << endl;
	cout << "The students year and rank was: " << student1.getYear() << "(" << student1.getRank() << ")" << endl;*/
	
	return 0;
}

void fillVector(vector<student> & newAllStudents)
{
	string name;
	double gpa;
	int year, rank;
	
	cout << "How many students are there: " << endl;
	int numStudents;
	cin >> numStudents;
	
	for (int i = 0; i < numStudents; i++)
	{
		cout << "Enter student name: " << endl;
		cin >> name;
		cout << "Enter student GPA: " << endl;
		cin >> gpa;
		cout << "Enter student year: " << endl;
		cin >> year;
		cout << "Enter student rank: " << endl;
		cin >> rank;
		
		student newStudent;
		newStudent.setName(name);
		newStudent.setGpa(gpa);
		newStudent.setYear(year);
		newStudent.setRank(rank);
		newAllStudents.push_back(newStudent);
	}
	
}

void printVector(const vector<student> & newAllStudents)
{	 
	unsigned int size = newAllStudents.size();
	
	for (unsigned int i = 0; i < size; i++)
	{
		cout << "Student name: " << newAllStudents[i].getName() << endl;
		cout << "Student GPA: " << newAllStudents[i].getGpa() << endl;
		cout << "Student year and rank: " << newAllStudents[i].getYear() << "(" << newAllStudents[i].getRank() << ")" << endl << endl;
	}
}
Last edited on Oct 17, 2017 at 1:10am
Oct 17, 2017 at 1:25am
You didn’t follow doug4’s advice. Fix that and you’ll be all set.
Oct 17, 2017 at 4:25am
Sorry about that when I posted I didnt refresh the page but it works perfectly now thanks!!!!
Topic archived. No new replies allowed.