I'm pretty new to C++ and working with books and youtube and here in the same time, but i got a weird problem that I can't find a solution.
I'm trying to inherit from a base class "Mother" to a derived class Daughter and it is not working for me, it says it need to
This code was taking from TheNewBoston, so thank u Bucky, just giving credits
C:\CppProjects\inheritance\Daughter.h|6|error: expected class-name before '{' token
I'm adding the main and the classes:
main:
#include <iostream>
#include "Mother.h"
#include "Daughter.h"
using namespace std;
int main()
{
Daughter t;
t.sayName();
}
Mother class:
#include "Mother.h"
#include <iostream>
#include "Daughter.h"
using namespace std;
Mother::Mother()
{
}
void Mother::sayName(){
cout<<"I am !!!"<<endl;
}
Mother header:
#ifndef MOTHER_H
#define MOTHER_H
class Mother
{
public:
Mother();
void sayName();
};
#endif // MOTHER_H
Daughter class:
#include "Daughter.h"
#include <iostream>
#include "Mother.h"
using namespace std;
Daughter::Daughter()
{
}
Daughter header:
#ifndef DAUGHTER_H
#define DAUGHTER_H
class Daughter : public Mother
{
public:
Daughter();
};
#endif // DAUGHTER_H
I looked in most of the searches in here and nothing found, I even tried noob style and added std::, it has nothing to do with it.
thank you all of you who are willing to try!
just so you know, when I'm trying to put all of the code in one file, it works great:
the new main:
#include <iostream>
using namespace std;
class Mother
{
public:
Mother();
void sayName(){
cout<<"I am!!!!"<<endl;
}
};
class Daughter: public Mother
{
public:
Daughter();
};
int main()
{
Daughter t;
t.sayName();
}
The order of the declarations is important. At the moment in your Daughter class you include a header for Daughter and only afterwards Mother. This means that the compiler will see a definition "hi, I'm Daughter, I inherited Mother" and will stop there wondering "I've never met your Mother, I don't know what to do here."
The order of includes and definitions is important. Always have the base classes first.
Usually you either include the base header in the daughter header, or at least have a forward declaration to tell that the Mother class does exist. Also check up on header guards since you will need them.
Related
This is my EngineIncludes.h file:
#include <stdafx.h>
//Engine Includes
namespace Engine
{
//Classes: 23
class BaseObject;
class Console;
class Engine;
class Node;
template <class T> class Transform;
}
#include "core/BaseObject.h"
#include "core/Console.h"
#include "core/Engine.h"
#include "math/Transform.h"
#include "scene/Node.h"
//Global Objects
extern Engine::Console* CONSOLE;
extern Engine::Engine* CORE_ENGINE;
In stdafx.h I have regular stuff like OpenGL, std::map, boost... and I'm building a precompiled header as standard.
This is the Node.h file:
#ifndef _NODE_H
#define _NODE_H
#include <stdafx.h>
#include <EngineIncludes.h>
namespace Engine
{
class Node : public BaseObject
{
public:
Node();
~Node();
void SetParent(Node* parent);
Node* GetParent();
void SetTransform(const Transform<double> &transform);
Transform<double> GetTransform();
Transform<double> GetDerivedTransform();
Transform<double> GetInheritedTransform();
void Update();
private:
Transform<float> transform;
Transform<float> derived;
Node* parent;
};
}
#endif _NODE_H
I get 3 errors here. One C2504 that Engine::BaseObject is not defined. And two C2079 that both Transform transform and Transform use undefined class. Now this is the BaseObject.h:
#ifndef _BASE_OBJECT_H
#define _BASE_OBJECT_H
#include "stdafx.h"
#include "EngineIncludes.h"
namespace Engine
{
class BaseObject
{
public:
BaseObject()
{
id = CORE_ENGINE->GenerateID();
}
~BaseObject()
{
}
long GetID()
{
return id;
}
private:
long id;
};
}
#endif _BASE_OBJECT_H
Now I specifically fully defined BaseObject inside header file with no luck. Still the same error. But if I comment out forward declarations in EngineIncludes.h, the compiler freaks out with
extern Engine::Console* CONSOLE;
It literally ignores all #includes for whatever reason. I've never had an issue like this before. And I tried everything. I even created EngineIncludes.cpp file. I moved the contents of EngineIncludes.h into stdafx.h with no luck. It somehow just ignores #includes. Even if I put #include "BaseObject.h" inside "Node.h" nothing changes. When it compiles Node it has both Transform which is a template class and BaseObject undefined, even though both objects are clearly defined before Node with forward declarations and #includes.
I'm using Visual Studio 2010, but never had an issue like this. I looked into my previous projects and all is written the same fashion and works without problem.
I learned C++ using the Code::Blocks. However, the compiler gives me an error in the file Daughter.h, line 6, saying "error: expected-class name before '{' token" when I am trying to implement the inheritance. That confuses me more because the tutorial I'm watching using Code::Blocks and compiler in this tutorial does not gives any errors, it in fact works perfectly. I am a bit frustrated at this point. Does anybody know how to make it work? Here are all of my files:
main.cpp
//main.cpp
#include "Daughter.h"
#include "Mother.h"
#include <iostream>
using namespace std;
int main()
{
Mother jelo;
jelo.sayname();
Daughter tina;
tina.sayname();
}
Mother.h
//Mother.h
#ifndef MOTHER_H
#define MOTHER_H
class Mother
{
public:
Mother();
void sayname();
};
#endif // MOTHER_H
Mother.cpp
//Mother.cpp
#include "Daughter.h"
#include "Mother.h"
#include <iostream>
using namespace std;
Mother::Mother()
{
}
void Mother::sayname() {
cout << "I am Roberts" << endl;
}
Daughter.h
//Daughter.h
#ifndef DAUGHTER_H
#define DAUGHTER_H
class Daughter: public Mother
{
public:
Daughter();
};
#endif // DAUGHTER_H
Daughter.cpp
//Daughter.cpp
#include "Daughter.h"
#include "Mother.h"
#include <iostream>
using namespace std;
Daughter::Daughter()
{
}
Daughter.h should #include "Mother.h", because it needs to know what Mother is.
If the tutorial doesn't say that, the tutorial is wrong.
Learn C++ from a good book, instead.
I have an error in my code. All the forums says "in this situation, use Forward Declaration". I tried this and it don't work.
the error is C2079 Credit::mainMenu use a class of MainMenu not declared (translated from french)
I modified the classe to be specific to the question.
main.cpp
#include <SFML/Graphics.hpp>
#include "Menu.h"
#include "credit.h"
#include "mainmenu.h"
MainMenu *mainMenu = new MainMenu();//works here
int main(){
return 0;
}
credit.h
#ifndef DEF_CREDIT
#define DEF_CREDIT
#include <SFML/Graphics.hpp>
#include "Menu.h"
class MainMenu;
class Credit : public Menu
{
private:
MainMenu mainMenu = MainMenu();//DON'T WORK HERE
};
#endif
mainmenu.h
#ifndef DEF_MAINMENU
#define DEF_MAINMENU
#include <SFML/Graphics.hpp>
#include "Menu.h"
#include "credit.h"
class MainMenu :public Menu
{
private:
Credit credit = Credit();//work
};
#endif
Fist i load main.cpp
after the credit.h (there is a credit.hpp but i don't use the variable now)
after the mainmenu.h (there is also a mainmenu.hpp, i don't use variable now)
tip: the Menu class isn't including any of those class
I can send you the complete code if you want
Thanks in advence!
This issue is know as circular dependency. Long story short, two objects should never have another as its subobject.
Instead you have two options. One is to have one point to another
class Credit : public Menu
{
private:
MainMenu* mainMenu;
};
Or, you could have a manager of sorts, where only the manager is aware of both objects, and let them interact through the manager
class Manager
{
private:
Credit credit;
MainMenu mainMenu;
};
For the technical reason why you can't have a member of a declared but not defined type is because it is a incomplete type, and you can't have an object of incomplete type.
Aaand im back again with my second question and im kinda not sure about wether i should have posted all the seperate classes cuz it looks somewhat long. And im sure the solution is pretty small.
Anyways, i am at polymorphism tutorial vid that i am following and everything works fine if i follow it and put all classes in "main.cpp". But when i tried to do the same program with seperate classes (seen below) i am getting error "
E:\Codeblocks\Poly\main.cpp|11|error: cannot convert 'Ninja' to 'Enemy*' in initialization|".*
I kinda understand what the error is saying..i think.. but dont know what i did wrong since the same code was working when Enemy and Ninja class wasnt seperate but now as seperate classes its not working. I think i included those classes properly in main.cpp.
main.cpp
#include <iostream>
#include "Enemy.h"
#include "Ninja.h"
#include "Monster.h"
int main()
{
Ninja n;
Monster m;
Enemy *enemy1=&n;
Enemy *enemy2=&m;
enemy1->setAttackPower(20);
enemy2->setAttackPower(50);
n.attack();
m.attack();
return 0;
}
Enemy.h
#ifndef ENEMY_H
#define ENEMY_H
class Enemy
{
public:
Enemy();
void setAttackPower(int a);
protected:
int attackPower;
private:
};
#endif // ENEMY_H
Enemy.cpp
#include "Enemy.h"
Enemy::Enemy()
{
//ctor
}
void Enemy::setAttackPower(int a)
{
attackPower=a;
};
Ninja.h
#ifndef NINJA_H
#define NINJA_H
class Ninja
{
public:
Ninja();
void attack();
protected:
private:
};
#endif // NINJA_H
Ninja.cpp
#include "Ninja.h"
#include <iostream>
Ninja::Ninja()
{
//ctor
}
void Ninja::attack(){
std::cout<<" I am a ninja. Ninja chop! -"<<attackPower<<"\n";}
This is because your Ninja class is not inhereted from Enemy class. You must define Ninja class like this:
#include "Enemy.h"
class Ninja : public Enemy
{
public:
Ninja();
void attack();
protected:
private:
};
EDIT: I added #include directive. Without it compiler won't know, where to find Enemy class declaration.
Alright, so I have two classes, Vending and Payment. Payment is the child of Vending. I keep getting the "base class undefined" error in my code.
Here are the two header files:
//Parent class (Vending.h)
#ifndef VENDING_H
#define VENDING_H
#include "Main.h";
namespace Vending
{
class Vending
{
public:
Vending();
Vending(int);
void setRequiredAmount(int);
int getRequiredAmount();
protected:
int selectedItem;
int requiredAmount;
};
}
#endif VENDING_H
//child class (Payment.h)
#ifndef PAYMENT_H
#define PAYMENT_H
#include "Vending.h"
namespace Vending
{
class Payment : public Vending
{
public:
Payment(int);
int getEnteredAmount();
void setEnteredAmount(int);
protected:
int enteredAmount;
};
}
#endif PAYMENT_H
It would be greatly appreciated if I can get some help to resolve this error
You say Main.h includes Payment.h, which DOES lead to circular dependencies. Read this post for additional information: http://forums.codeguru.com/showthread.php?288147-C2504-Base-class-undefined-(other-posts-have-no-solution)&p=919112#post919112
You need to rethink your project properly, conditions like this should not happen. Simply try to remove the #include "Main.h" from the Vending.h, and compile Payment.cpp...