no matching function for call to 'Subject::Subject(<brace-enclosed initializer list>)'|

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
#include<iostream>
#include<string>
using namespace std;


class Teacher{
public:
    std::string name;
    std::string faculty;

    Teacher(std::string n, std::string f)
        : name{n}, faculty{f} {}


};



class Subject{
    std::string title;
    Teacher teach;

public:
    Subject(std::string a,  const Teacher& b);
   
};

Subject::Subject(std::string a, const  Teacher& b)
    : title{a}, teach{b} {}

int main()
{
    Subject s{"Programming", new Teacher{"Daniel","XFAC"}}; 
    return 0; 
}


Errors: no matching function for call to 'Subject::Subject(<brace-enclosed initializer list>)'|
note: candidate: Subject::Subject(const char*, const Teacher&)|
note: no known conversion for argument 2 from 'Teacher*' to 'const Teacher&'|




The thing is that if I remove the " new" keyword from the initializing list , IT WORKS. Please help

Last edited on
The class Subject has one constructor; Subject::Subject(std::string a, const Teacher& b)

That constructor accepts two parameters.

Those parameters must be of type std::string and const Teacher& (or something that can be easily converted to those types - in effect, C++ knows how to turn "Programming", which is a const char* , into a std::string).

new Teacher{"Daniel","XFAC"} is of type Teacher* (i.e. pointer-to-teacher).

Is a Teacher* the same as a const Teacher& ? No.

So why doesn't it work? Because the constructor accepts a parameter of type const Teacher&, but you're trying to give it a Teacher*. You have to give the constructor parameters of the right type.

The thing is that if I remove the " new" keyword from the initializing list , IT WORKS. Please help

You've already fixed it? Then what are you here for?
Last edited on
This is an exercise from an exam at the OOP class that sounded like this:

Write the implementation of classes Teacher and Subject so that code outputs subject title followed by teacher data.


code given by teacher:

1
2
3
4
5
6
Int main()
{
    Subject p{"Programming", new Teacher{"Daniel","XFAC"}};
    Subject db = p.copy().title("Databases");
    cout<< p <<endl<<db<<endl;
}


Thats why I needed it to work with the "new" there. Thanks a lot :)
Last edited on
I see. In which case then just adapt the constructor of the Subject class so it expects a pointer-to-Teacher.

I would send this code back, though, if I were code reviewing it. You're creating a Teacher object with new and giving it straight to the Subject class, which means that unless the Subject class calls delete on that pointer when the Subject class is destroyed, you'll get a memory leak.

However, if the Subject class does call delete, what happens if someone creates one like this:
1
2
3
4
5
6
int main()
{
  Teacher someTeacherObject{"Daniel","XFAC"};
  {
     Subject p{"Programming", &someTeacherObject};
  }  // CRASH here when the Subject object calls delete on something that wasn't allocated using new 


Sure, this may just be for a learning exercise, but that's no excuse to teach dangerous practices. Anyway, now you know.
Last edited on
I guess for him it just has to work for the example he gave us.
But I will implement the classes so that they work for every case, including the one you just gave me. Yep, it's a learning exercise.
Your choices are basically leaky or crashy. Pick one. Be sure to add comments to your class explaining what the problem is; maybe you'll get bonus points!
Topic archived. No new replies allowed.