Dark Chocolate Dark Chocolate -3 years ago 175
C++ Question

Base class undefined. Error C2504

Following this tutorial(https://www.youtube.com/watch?v=gq2Igdc-OSI&index=52&list=PLAE85DE8440AA6B83) I encountered 4 errors on Visual Studio C++ 2017. 3 of them are the same thing and just repeat 'Mother': base class undefined in the daughter.h file. The other error reads:'sayName' is not a member of 'Daughter' Now here is the code. It is quite simple what I want the program to print...I want it to print out two lines of "What are you doing there?" If you could help me with this answer, that would be great. Thank you.
For the Main file
`#include "stdafx.h"
#include
#include"Daughter.h"
#include"Mother.h"
using namespace std;

int main()
{
Mother pot;
pot.sayName();
Daughter kettle;
kettle.sayName();
int pause = 0;
cin >> pause;
}

Mother.h
#ifndef MOTHER_H
#define MOTHER_H

class Mother
{
public:
Mother();
void sayName();

};

#endif
Mother.cpp
#include "stdafx.h"
#include<iostream>
#include"Daughter.h"
#include"Mother.h"
using namespace std;

Mother::Mother()
{
}
void Mother::sayName() {
cout << "What are you doing there?" << endl;

}
Daughter.h
#ifndef DAUGHTER_H
#define DAUGHTER_H

class Daughter:public Mother
{
public:
Daughter();
};
#endif
Daughter.cpp
#include "stdafx.h"
#include<iostream>
#include"Daughter.h"
#include"Mother.h"
using namespace std;

Daughter::Daughter()
{
}

Answer Source

When a class inherits another, it must include the parent class header in its header. In your case, you must add #include "Mother.h" at the top of the daughter header (not only at the .cpp file). The other error is happening because of the first one and correcting it should solve it.

When you write the inheritance syntax class Daughter : public Mother, the Daughter class definition needs to have access to the information about its parent class for several reasons. One of them is the information about inherited methods, which was causing your second error.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download