Inheritance - Unable to Run Parent Methods

Hi all :)

My parent class, Course, has the method addStudent(Student s). My child class, BetterCourse, inherits from Course. Every time I try to run BetterCourse.addStudent(s), I get the following error:

error: no matching function for call to ‘BetterCourse::addStudent(Student (&)())’
note: candidates are: void Course::addStudent(Student)


I understand it's telling me addStudent() hasn't been defined in BetterCourse and that it's recommending I use the one present in the parent class, Course. This has me confused as the whole idea around inheritance is not needing to redefine inherited functions and variables.

Code is as follows:
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
#include <iostream>
#include <string>
#include "Student.h"

using namespace std;

class Course
{

	protected:
		string id;
		string name;
	
	public:
		Course();
		Course(string id, string name);		
		void addStudent(Student s);
};

Course::Course()
{
   //code
}

Course::Course(string id, string name)
{
   //code
}

void Course::addStudent(Student s) 
{
   //code
}


1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <string>
#include "Course.h"

using namespace std;

class BetterCourse : public Course
{
	public:
		BetterCourse(string id, string name) : Course(id,name){};
};
Last edited on
I understand it's telling me addStudent() hasn't been defined in BetterCourse and that it's recommending I use the one present in the parent class, Course.

No, that's not what it says. It says that you are trying to pass a function (that takes no arguments and returns a Student) as argument to BetterCourse::addStudent.
Last edited on
Sorry, I pasted output that resulted from me trying to add the addStudent function to BetterCourse. I've fixed the post to reflect that it actually specifies addStudent() from Course.
Can you show how you call addStudent()?
I can...aaaand you've just pointed me in the direction my glaringly obvious mistake.
1
2
3
4
5
6
7
8
9
10
int main()
{
	BetterCourse better("CSMDSI", "Data Structures");
	
	Student s1();
	
	better.addStudent(s1);
	
	return 0;
}


I was creating a student with an empty constructor, which while technically defined in Student (see below), is not accounted for in Course.addStudent().
1
2
3
4
5
6
7
8
9
Student::Student(){
	id = "";
	name = "";
}

Student::Student(string id, string name){
	this->id = id;
	this->name = name;
}


I have added the required parameters and I no longer get the error. I am now going to give in to sleep.

Thank you!


Line 5 is creating a function called sl that returns a Student and takes no arguments.
+1 firedraco.

Get rid of the parenthesis:

1
2
3
Student s1();  // <- declares a function, similar to 'int main();'

Student s1;  // <- creates an object 
I've always wondered, why is it even legal to have a function declaration in the scope of a function? I've also always been confused by class/namespace scope vs function scope, how they're the same/different.
Topic archived. No new replies allowed.