No matching constructor for initialization error

Feb 4, 2022 at 9:53pm

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

//this is the main function 
#include <iostream>
#include "Student.h"
#include "StudentClub.h"
#include <vector>
#include <iomanip>

using namespace std; 

int main() { 
 vector <Student*> clubmem;
    Student m (name);

 while (m.get_name() != "Q")
    {
        cout << "New Member (Q to quit): " << endl;
        cin >> name; // Error Message: No matching member function for call to 'push_back'
        clubmem.push_back(m);
    }

    //Error Message: No matching constructor for initialization of 'StudentClub'
    StudentClub club (&p, &v, &s, &t, &clubmem);
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "StudentClub.h"
#include <iostream>
#include <vector>
#include "Student.h"

using namespace std;


//initialization of parametrized constructor in StudentClub.cpp
StudentClub::StudentClub (Student* p, Student* v, Student* s, Student* t, vector<Student*> m) 
{
    president = p;
    vicepresident = v;
    secretary = s;
    treasurer = t;
    member = m;
}


For the first error message: Error Message: No matching member function for call to 'push_back'

I don't understand why I can't use the push_back operation that is defined under the vector library. I'm trying to add a pointer to a Student object to a vector of student objects called clubmem.

For the second error message: Error Message: No matching constructor for initialization of 'StudentClub'

There is a vector of pointers to Student objects in the constructor and clubmem in the main function is also a vctor of pointers to Student objects so I'm not sure why it's saying that there is no matching constructor here.


Last edited on Feb 4, 2022 at 10:53pm
Feb 4, 2022 at 10:27pm
the vectors are different types. one is fulla pointers. one is fulla objects.
maybe the function should have been
vector<student> *M?

also these could be references. why the pointers?
Last edited on Feb 4, 2022 at 10:28pm
Feb 4, 2022 at 10:54pm
How would I change them to references? I also fixed the vector so that both are vectors of pointers to Student objects.

Feb 4, 2022 at 11:10pm
Why a std::vector of pointers? std::vector<Students> works just as well, the C++ library handles the memory management for you. Putting the objects stored on the heap/free store automatically so you don't have to do manual memory management.

Instead of pointer, using references for your constructor:
1
2
3
4
5
6
7
8
StudentClub::StudentClub(Student& p, Student& v, Student& s, Student& t, std::vector<Student>& m)
{
    president       = p;
    vicepresident = v;
    secretary       = s;
    treasurer       = t;
    member         = m;
}


In main you declare your vector: std::vector<Student> clubmem;

Where are all those single-letter variables being declared? Certainly not in main, You are passing non-existent variables into your ctor.

Where is "name" (being passed as a parameter) when creating the instance of a student declared? Not in main.

If they are instantiated in headers then showing the contents of the headers would be helpful.

The single-letter variable names probably means something to you, but they should be descriptive so their names self-document what they are.

Topic archived. No new replies allowed.