C++: Two classes referencing each other - c++

So to toy with friend functions, I decided to make a Child class and a Mother class. The Mother class has a data member that is a Child. The Child class makes two methods of the Mother class friend functions.
When I compile though, it seems that no matter how I handle inclusions I end up with an error. If the Child is the first one to get defined, I get "Mother is not a class or namespace name" for the line friend void Mother::setChildName(string name); in Child.h. If the Mother is the first one to get defined, I get "Missing type specifier" for the line Child c; in Mother.h.
Is there a way around this? I tried putting class Mother; at the top of the Child.h and class Child; at the top of Mother.h and that didn't seem to help.
Or is this kind of circular reference just always going to fail?
In Mother.h:
#ifndef MOTHER_H_
#define MOTHER_H_
#include <string>
#include "Child.h"
using namespace std;
class Mother {
public:
Mother();
void setChildName(string name);
string getChildName();
private:
Child c;
};
#endif
In Mother.cpp:
#include <string>
#include "Mother.h"
using namespace std;
void Mother::setChildName(string name) {
c.name = name;
}
string Mother::getChildName() {
return c.name;
}
In Child.h:
#ifndef CHILD_H_
#define CHILD_H_
#include <string>
#include "Mother.h"
using namespace std;
class Child {
public:
private:
string name;
friend void Mother::setChildName(string name);
friend string Mother::getChildName();
};
#endif

This particular problem cannot be solved without redesign. Mother needs to be defined before Child, and Child needs to be defined before Mother. The simple way is to make the whole Mother class a friend of Child. That way Child can be defined first.
I think there little practical benefit in making individual methods friends of another class. That would imply that your class is so big that it's responsibilities could be divided into smaller classes.

if Mother will hold pointer to child, you will not have to include child.h. forward deceleration will be enough.

Related

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()
{
}
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.

Troubles with Circular Dependencies between 3 classes and with inheritance

I'm a first-year college student that doesn't know everything about CS yet, so please bear with my newness to it, and this is my first question on here.
For an assignment, we are making faux version of Pokemon Go to practice using polymorphism in c++, and I'm running into some compiler errors. Here are the three files with just a sample of the code in them:
#ifndef EVENT_H
#define EVENT_H
#include <string>
#include "Trainer.h"
class Event{
protected:
std::string title;
public:
Event();
~Event();
virtual void action(Trainer) = 0;
};
#endif
Trainer.h:
#ifndef TRAINER_H
#define TRAINER_H
#include "Pokemon.h"
class Trainer{
private:
Pokemon* pokemon;
int num_pokemon;
public:
Trainer();
~Trainer();
//include accessors and mutators for private variables
};
#endif
Pokemon.h:
#ifndef POKEMON_H
#define POKEMON_H
#include "Event.h"
#include <string>
class Pokemon : public Event{
protected:
std::string type;
std::string name;
public:
Pokemon();
~Pokemon();
virtual bool catch_pokemon() = 0;
};
#endif
The trainer.h file is a parent class for each pokemon type (eg Rock) which just defines a few virtual functions. The error I'm getting is when I'm compiling all of this and I get something that says:
Pokemon.h : 5:30: error: expected class-name befoer '{' token:
class Pokemon : Event {
Pokemon need to be a derived class to an event, so that an event pointer can point in another Location class can point to either a pokemon, pokestop, or cave for the assignment, and I have been looking online for hours and can't figure out what to do. I would appreciate the help! Let me know if you need more info or something because again, this is my first time posting a question.
You need some forward declarations.
In Event.h, you can put class Trainer; instead of #include "Trainer.h". In Trainer.h, you can put class Pokemon; instead of #include "Pokemon.h".
You will probably need to include the appropriate headers in the corresponding source files in order to actually use the other classes. But by avoiding the includes in the header files, you get out of the circular dependency trouble.
Pokemon.h must continue to #include "Event.h", since you're inheriting Event, which requires a complete definition.
Use forward declaration, to tell classes the type they need to use will be defined later. You can use forward declaration in situations where the size is know, pointers and references are always the same size regardless of the type they point to so use them.
#ifndef EVENT_H
#define EVENT_H
#include <string>
class Trainer;
class Event
{
protected:
std::string title;
public:
Event();
virtual ~Event();
virtual void action(Trainer* const trainer) = 0;
};
#endif
then
#ifndef TRAINER_H
#define TRAINER_H
class Pokemon;
class Trainer
{
private:
Pokemon* const pokemon;
int numPokemon;
public:
Trainer();
~Trainer();
};
#endif
then
#ifndef POKEMON_H
#define POKEMON_H
#include "Event.h"
#include <string>
class Pokemon : public Event
{
protected:
std::string type;
std::string name;
public:
Pokemon();
virtual ~Pokemon();
virtual bool catchPokemon() = 0;
};
#endif
when using polymorphism (virtual functions) you must always make the base class destructor virtual too. It is also nice to make the derived classes destructor virtual as well, but it is not required.

How to override a function in derived class when Inheriting

I have two classes - Mother(Base) and Daughter(Derived). I am inheriting a function from Mother class and trying to override in Daughter class. It looks like that it overrides, but my confusion is, even though I don't inherit Mother class, the function still works, so how am I inheriting/overriding it? I am very confused as if I'm really inheriting/overriding anything. Please note in the Derived class that I am not inheriting : public Mother
Thanks for the help, as always!!!
This is my code
Mother.hpp
#ifndef Mother_hpp
#define Mother_hpp
#include <iostream>
#include <string>
class Mother
{
public:
Mother();
void sayName();
};
Mother.cpp
#include <iostream>
#include <string>
#include "Mother.hpp"
#include "Daughter.hpp"
using namespace std;
Mother::Mother(){}
void Mother::sayName(){
cout<<"I am Sandy" <<endl;
}
Daughter.hpp
#ifndef Daughter_hpp
#define Daughter_hpp
#include <iostream>
#include "Mother.hpp"
class Daughter : public Mother
{
public:
Daughter();
void sayName();
};
Daughter.cpp
#include <iostream>
#include "Mother.hpp"
#include "Daughter.hpp"
using namespace std;
Daughter::Daughter() : Mother(){}
void Daughter::sayName(){
cout << "my name is sarah" <<endl;
}
Main.cpp
#include <iostream>
#include "Mother.hpp"
#include "Daughter.hpp"
using namespace std;
int main(int argc, const char * argv[]) {
Mother mom;
mom.sayName();
Daughter d;
d.sayName();
return 0;
}
but my confusion is, even though I don't inherit Mother class, the function still works, so how am I inheriting/overriding it? I am very confused as if I'm really inheriting/overriding anything.
you don't really override sayName() of your Mother class because (as you said) Daughter class doesn't inherit it in the first place. That is, you need to inherit a class first in order to be able to override its virtual functions.
your second call to sayName() works because it's a call to a member function of Daughter class, which is totally independent from Mother class. Note that, just having multiple independent classes whose member functions share the same signature is not overriding
side note: you shouldn't include Daughter.hpp in Mother.cpp, whether you plan to inherit Mother in Daughter, or not.

using reference from abstract class in a child class of abstract class c

Hullo, first time asking a question! I have 3 classes, CharacterBuilder, which is an abstract class, BullyCharacterBuilder, which inherits from CharacterBuilder and redefines one of its functions, and Character, which is a generic class full of ints with accessors/mutators that I need to use.
I define a pointer to Character in CharacterBuilder.h like so:
#ifndef CHARACTERBUILDER_H
#define CHARACTERBUILDER_H
#include "Character.h"
class CharacterBuilder {
public:
Character* getCharacter();
void createNewCharacter();
virtual void buildCharacter(int level) = 0;
protected:
Character* leeroy;
};
#endif
CharacterBuilder.cpp
#include "CharacterBuilder.h"
#include "Character.h"
Character* leeroy;
Character* CharacterBuilder::getCharacter() {
return leeroy;
}
void CharacterBuilder::createNewCharacter(){
leeroy = new Character;
}
Then I inherit publicly from CharacterBuilder in my BullyCharacterBuilder class like so:
#ifndef BULLYCHARACTERBUILDER_H
#define BULLYCHARACTERBUILDER_H
#include "Character.h"
#include "CharacterBuilder.h"
class CharacterBuilder;
class BullyCharacterBuilder : public CharacterBuilder{
CharacterBuilder* myParent;
void buildCharacter(int level);
};
#endif
Finally, in the cpp file for my BullyCharacterBuilder, I attempt to call accessor/mutators from the Character pointer (which I assume I've inherited from CharacterBuilder) like so:
#include "Character.h"
#include "BullyCharacterBuilder.h"
#include "CharacterBuilder.h"
void buildCharacter(int level){
leeroy->setAbilityScores();
}
When I attempt to do this, I get Error: identifier "leeroy" is undefined
It might be a dumb question but I tried searching for this particular problem on stackoverflow and google using every combination of keywords I could think of. Would be much obliged to any help!
You forgot to qualify the function buildCharacter with your class name BullyCharacterBuilder:
void BullyCharacterBuilder::buildCharacter(int level){
leeroy->setAbilityScores();
}
you need to implement buildCharacter as a method on the CharacterBuilder class
void BullyCharacterBuilder::buildCharacter(int level)
{
leeroy->setAbilityScores();
}
if you type like this void buildCharacter(int level) meaning that the function is independent and don't belong to any class,
so you must type like this
void BullyCharacterBuilder::buildCharacter(int level) to tell compiler the function belongs to BullyCharacterBuilder

C++ Inheritance - Running Parent Method in Child Class

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.
Course is as follows:
#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
}
BetterCourse:
#include <iostream>
#include <string>
#include "Course.h"
using namespace std;
class BetterCourse : public Course
{
public:
BetterCourse(string id, string name) : Course(id,name){};
};
From your error it seems that you for the first time get to the ugliest part of C++.
This:
Student s();
Is function declaration - not object definition. s type is Student (*)() so when you call:
BetterCourse bc;
bc.addStudent(s);
You get your error - you don't have method to add functions returning Student.
Define Student in the following ways:
Student s;
Student s {}; // new C++11 way
Student s = Student(); //
It sounds like you're actually calling the function 'addStudent' with an inappropriate argument. Could you show the code that is actually calling addStudent on the BetterCourse object? It sounds like you're using a reference to the student instead of the student object itself.
you can not call BetterCourse.addStudent(s) you should create an object
BetterCourse obj;
obj.addStudent(s);
should work
If you want to call BetterCourse::addStudent(s) than declare addStudent(s) as static method