I've got some problems here. I' am trying to make my code working like interface in java. This class is being inherited by 2 other by they are causing some problems.
And also i'd like to know if i am doing it correctly, the ways to improve my code. I'am novice.
Look:
Interface class:
#include <iostream>
#include <string>
class IZarzadzaniePozycjami{
public:
IZarzadzaniePozycjami(void);
~IZarzadzaniePozycjami(void);
public:
virtual void ZnajdzPozycjePoTytule();
virtual void ZnajdzPozycjePoId();
virtual void WypiszWszystkiePozycje();
};
#include "Pozycja.h"
#include "IZarzadzaniePozycjami.h"
#include <iostream>
#include <list>
Catalog class which inherites from IZarzadzanie... Class.
#include "Pozycja.h"
#include "IZarzadzaniePozycjami.h"
#include <iostream>
#include <list>
class Katalog : public IZarzadzaniePozycjami
{
private:
std::string dzialTematyczny;
public:
void ZnajdzPozycjePoTytule(std::string tytul);
void ZnajdzPozycjePoId(int id);
void WypiszWszystkiePozycje();
Katalog(void);
Katalog(std::string dzialTematyczny_);
void DodajPozycje(Pozycja);
std::list<Pozycja> lista;
friend bool operator==(const Katalog & kat, const std::string & s);
~Katalog(void);
};
inline bool operator==(std::string& s, Katalog& katalog)
{
return katalog == s;
}
and then library class(biblioteka.h) which alseo inherite from interface
#include<iostream>
#include "IZarzadzaniePozycjami.h"
#include "Bibliotekarz.h"
#include "Pozycja.h"
#include "Katalog.h"
class Biblioteka :
public IZarzadzaniePozycjami
{
public:
Biblioteka(void);
~Biblioteka(void);
Biblioteka(std::string adres_);
void DodajBibliotekarza(Bibliotekarz);
void WypiszBibliotekarzy();
void DodajKatalog(Katalog);
void DodajPozycje(Pozycja p, std::string dzialTematyczny);
void ZnajdzPozycjePoTytule(std::string tytul);
void ZnajdzPozycjePoId(int id);
void WypiszWszystkiePozycje();
private: std::string adres;
};
And here are some errors. Sorry for long post. Didn't know what to cut off.
c:\users\komputer\documents\visual studio 2012\projects\project1\izarzadzaniepozycjami.h(3): error C2011: 'IZarzadzaniePozycjami' : 'class' type redefinition
1> c:\users\komputer\documents\visual studio 2012\projects\project1\izarzadzaniepozycjami.h(3) : see declaration of 'IZarzadzaniePozycjami'
1>c:\users\komputer\documents\visual studio 2012\projects\project1\katalog.h(7): error C2504: 'IZarzadzaniePozycjami' : base class undefined
1>c:\users\komputer\documents\visual studio 2012\projects\project1\biblioteka.h(9): error C2504: 'IZarzadzaniePozycjami' : base class undefined
1> Generating Code...
1>c:\users\komputer\documents\visual studio 2012\projects\project1\katalog.cpp(16): warning C4717: 'operator==' : recursive on all control paths, function will cause runtime stack overflow
Use #pragma once in all the header files:
#pragma once
#include <iostream>
#include <string>
class IZarzadzaniePozycjami{
public:
IZarzadzaniePozycjami(void);
~IZarzadzaniePozycjami(void);
....
and make sure the header with IZarzadzaniePozycjami is included everywhere before IZarzadzaniePozycjami is used.
You declare
virtual void ZnajdzPozycjePoTytule();
but in your classes you declare (and I hope you define)
void ZnajdzPozycjePoTytule(std::string tytul);
These are not the same methods. A method signature includes its parameters, so your virtual method is still unimplemented.
What you're calling an "Interface" class is not the same as the Java concept. In java an interface has no implementation, but the class you've defined, must have an implementation defined. You can avoid defining the virtual methods if you make them pure virtual (e.g. = 0), but you must provide an implementation for the all non-pure virtual methods of IZarzadzaniePozycjami. In its current form, that means every method must be defined. This is why it won't compile.
Related
Having trouble understanding why I'm getting an 'undeclared identifier' error when I've made sure to include the header file that has the declaration of the class I'm making a vector of.
#pragma once
#include <vector>
#include "Member.h"
class Party {
private:
std::vector<Member> members;
public:
Party();
int get_party_size();
void add_member(Member new_member);
Member& get_member(int num);
};
Here's "Member.h"
#pragma once
#include <vector>
#include <string>
#include "Party.h"
class Member
{
private:
int hp;
bool is_stunned;
bool is_alive;
public:
Member();
~Member();
int get_hp();
bool get_is_stunned();
bool get_is_alive();
void take_damage(int amt);
void stun();
virtual void turn(std::vector<Party>& parties, int my_party, int my_member_number);
virtual std::string get_class_name();
};
Pretty new to the language, so sure I'm missing something obvious.
You have circular dependency between Member and Party
Remove the line
virtual void turn(
std::vector<Party>& parties,
int my_party,
int my_member_number);
in Member and remove the #include "Party.h" in Member.h
Instead think along the lines that a Party is just a collection of Members so there is no need for an individual Member to know about the container
So after input from #some-programmer-dude you could also solve it by adding a forward declaration in your Member.h instead of including the Party.h
class Party;
class Member { ... }
I have a fairly simple situation, and the fact that I can't cope with it drives me crazy.
I have a class that is declared as follows:
// inc/Services/Specific/ReviewRetriever.h
#include "../../ReviewRetriever.h"
class Specific_ReviewRetriever : public ReviewRetriever
{
public:
Specific_ReviewRetriever(Service* service);
~Specific_ReviewRetriever() = default;
};
Implementation of the class goes as follows:
// src/Services/TrustedShops/ReviewRetriever.cpp
#include <string>
#include <vector>
#include "Service.h"
#include "Services/Specific/ReviewRetriever.h"
Specific_ReviewRetriever::Specific_ReviewRetriever(Service* service) :
ReviewRetriever(service)
{
}
std::string Specific_ReviewRetriever::prepare_update_link(std::string link)
{
}
std::vector<int> Specific_ReviewRetriever::parse_response(boost::property_tree::ptree responseXML)
{
}
This class inherits from the class that is declared as follows:
// inc/ReviewRetriever.h
#include <string>
#include <vector>
#include <boost/property_tree/ptree.hpp>
#include "Review.h"
class Service;
class ReviewRetriever
{
public:
~ReviewRetriever() = default;
void retrieve(std::vector<Review> & reviews);
protected:
ReviewRetriever(Service* service);
virtual std::string prepare_update_link(std::string link) = 0;
virtual std::vector<Review> parse_response(boost::property_tree::ptree responseXML) = 0;
Service* _service;
};
And this class on its part is defined as follows:
// src/ReviewRetriever.cpp
#include <vector>
#include <string>
#include <boost/property_tree/ptree.hpp>
#include "Review.h"
#include "ReviewRetriever.h"
void ReviewRetriever::retrieve(std::vector<Review> & reviews)
{
}
So a fairly simple class and another one that inherits from it. But when I try to compile the code I get the following error:
no ‘std::string Specific_ReviewRetriever::prepare_update_link(std::string)’
member function declared in class ‘Specific_ReviewRetriever’
So, even though I got implementation of the class, the compiler doesn't seem to notice it (even though it sees that the class Specific_ReviewRetriever inherits from ReviewRetriever, it refuses to recognize its methods).
I build with cmake and here is the relevant part:
// src/CMakeLists.txt
file(GLOB_RECURSE sources *.cpp)
target_include_directories(my_target PRIVATE ${PROJECT_SOURCE_DIR}/inc/)
(here ${PROJECT_SOURCE_DIR} is src/../).
So as I mentioned, implementation and declaration of the classes is present, and yet compilation fails. I understand that the problem may not be on the surface, but I am really clueless where to start to track the error, maybe you have a piece of advice on that.
In case this excerpt of code is insufficient, entire code lies here.
Thank you in advance!
You still have to declare the functions that you're overriding in the derived class:
class Specific_ReviewRetriever : public ReviewRetriever
{
public:
Specific_ReviewRetriever(Service* service);
~Specific_ReviewRetriever() = default;
// Overrides:
virtual std::string prepare_update_link(std::string link);
virtual std::vector<int> Specific_ReviewRetriever::parse_response(boost::property_tree::ptree responseXML);
};
I would like to create a basic abstract class in C++, where the subclasses are each in a separate file. My abstract class looks something like
class Process_Base{
public:
virtual void process() = 0;
}
Should a simple implementation like this be contained entirely in a header file? If it is do I need to have a .cpp file associated with it?
When I create the subclasses what should their header file look? I was thinking .cpp file for the subclass should look something like
#include "Process_Base.h"
class Hello_Process : public Process_Base{
void process(){
printf("%s\n", "Hello, World");
}
}
Could someone verify that I am approaching this correctly, and if not give a simple example of what I should be doing.
UPDATE
I continued with the implementation but I am now getting the following compiler error
g++ -c -Wall -g Process_Message.cpp
Process_Message.cpp:4: error: expected class-name before ‘{’ token
The following is the abstract class header
// Abstract header .hpp file
class Process_Base{
public:
virtual void process() = 0;
};
the subclass header
// The subclass header .hpp file
#include "Process_Base.hpp"
class Process_Message : public Process_Base {
public:
void process();
};
and the implementation
// Simple implementation .cpp file
#include <stdio.h>
#include <string.h>
class Process_Message : Process_Base {
public:
void process(){
printf("%s", "Hello");
}
}
I don't understand why I am getting the error, can someone please explain.
You're missing ; at the end of Process_Base declaration, when you include the file, the compiler goes nut. correct is:
class Process_Base{
public:
virtual void process() = 0;
};
Why this 'Process_Base' is there in below code? You have already mentioned inheritance in header file,right?
// Simple implementation .cpp file
#include <stdio.h>
#include <string.h>
class Process_Message : Process_Base {
public:
void process(){
printf("%s", "Hello");
}
}
Also don't forget to type ';' at the end of the class declaration after '}'
Your correct code should look like this:
//Process_Message.h
#include "Process_Base.hpp"
class Process_Message : public Process_Base {
public:
void process();
};
//Process_Message.cpp
#include <stdio.h>
#include <string.h>
#include "Process_Message.h"
void Process_Message::process()
{
printf("%s", "Hello");
}
Don't forget to declare your overrides as virtual, or they will hide parent methods instead of implementing them.
#include <iostream>
class Process_Base
{
public:
virtual ~Process_Base() {}
virtual void process() =0;
};
class Process_Message : public Process_Base
{
public:
virtual void process(); // <- virtual
};
void Process_Message::process()
{
std::cout << "Hello";
}
The above should compile fine
I'm starting to learn C++ (coming from Java), so bear with me.
I can't seem to get my method declaration to accept a class I've made.
'Context' has not been declared
I think I'm not understanding a fundamental concept, but I don't know what.
Expression.h
#include "Context.h"
class Expression {
public:
void interpret(Context *); // This line has the error
Expression();
virtual ~Expression();
};
Context.h
#include <stack>
#include <vector>
#include "Expression.h"
class Context {
private:
std::stack<Expression*,std::vector<Expression*> > theStack;
public:
Context();
virtual ~Context();
};
You have to forward declare Expression in Context or vice versa (or both), otherwise you have a cyclic dependency. For example,
Expression.h:
class Context; // no include, we only have Context*.
class Expression {
public:
void interpret(Context *); // This line has the error
Expression();
virtual ~Expression();
};
Context.h:
#include <stack>
#include <vector>
class Expression; // No include, we only have Expression*
class Context {
private:
std::stack<Expression*,std::vector<Expression*> > theStack;
public:
Context();
virtual ~Context();
};
You can perform the forward declarations because the full definition of the classes isn't needed, since you are only referring to pointers to the other class in each case. It is likely that you will need the includes in the implementation files (that is, #include "Context.h" in Expression.cpp and #include Expression.h in Context.cpp).
Finally, remember to put include guards in your header files.
In C++, class definitions always have to end with a semi-colon ;
so example:
class foo {};
Java and C# doesn't require that, so I can see your confusion.
Also it looks like both your header files include each other. Thus it's kind of like a snake eating it's tail: Where does it start? Thus in your Expression.h you can replace the 'include' with a forward declaration instead:
class Context;
class Expression {
public:
void interpret(Context *); // This line has the error
Expression();
virtual ~Expression();
}
And last but not least, you should put a compiler guard to prevent the header from getting included more than once into a .cpp file. You can put a #pragma once in the top of the header file. That is useful if you are using visual studio and the microsoft compiler. I don't know if GCC supports it or not. Or you can wrap your header file like this:
#ifndef EXPRESSION_H_
#define EXPRESSION_H_
class Context;
class Expression {
public:
void interpret(Context *); // This line has the error
Expression();
virtual ~Expression();
}
#endif
you might need to forward declare the classes Context and Expression in the header files before the #include
e.g.
#include <stack>
#include <vector>
// forward declaration
class Context;
class Expression;
#include "Expression.h"
class Context {
private:
std::stack<Expression*,std::vector<Expression*> > theStack;
public:
Context();
virtual ~Context();
}
I know it is one of the constant ask question
so i get this error
'WorldObject': [Base class undefined (translated from german)]
Here is the code which produce this error:
ProjectilObject.h:
#pragma once
#ifndef _PROJECTILOBJECT_H_
#define _PROJECTILOBJECT_H_
#include "GameObjects.h"
class ProjectilObject: public WorldObject
{
public:
ProjectilObject(IGameObject* parent,int projectiltype);
void deleteyourself();
protected:
virtual void VProcEvent( long hashvalue, std::stringstream &stream);
virtual void VInit();
virtual void VInitfromStream( std::stringstream &stream );
virtual void VonUpdate();
virtual void VonRender();
private:
vec3 vel;
float lifetime;
float lifetimeend;
vec3 target;
int m_projectiltype;
};
#endif
Here is the code file from the WorldObject class:
GameObjects.h:
#pragma once
#ifndef _GAMEONJECTCODE_H_
#define _GAMEONJECTCODE_H_
#include "IGameObject.h"
#include "Sprite.h"
#include "GamePath.h"
#include "HashedString/String.h"
#include "IAttribute.h"
#include "CharacterObjects.h"
#include "ProjectilObject.h"
[...]
class WorldObject: public IGameObject, public MRenderAble
{
public:
WorldObject(IGameObject* parent);
virtual bool IsDestroyAble();
virtual bool IsMageAble();
virtual bool IsRenderAble();
protected:
virtual void VProcEvent( long hashvalue, std::stringstream &stream);
virtual void VonUpdate();
virtual void VonRender();
virtual void VInit() =0;
virtual void VInitfromStream( std::stringstream &stream ) =0;
virtual void VSerialize( std::stringstream &stream );
vec3 poscam;
};
[...]
#endif
There are some other Classes in this file but they shouldn't interrupt, I think.
Maybe there is a tiny error I didn't saw but I don't understand why this error is produced. When you need more of the code feel free to ask because i think it would only disturb.
If you have any source file that includes GameObjects.h before ProjectilObject.h or does not include ProjectilObject.h directly, then the compiler will first find the declaration of ProjectilObject through the include in GameObjects.h before knowing what WorldObject is. That is because GameObjects.h first includes ProjectilObject.h and then declares WorldObject. In that case the include of GameObjects.h present in ProjectilObject.h won't work because _GAMEONJECTCODE_H_ will be already defined.
To avoid this, either be sure to include ProjectilObject.h instead of GameObjects.h in your source file, or use forward declarations.
It's hard to answer this question without looking at the whole code. Even a misplaced brace could count. Check your namespaces - are you sure the WorldObject is in the same namespace?
I suggest you use the #pragma message by placing it near the WorldObject definition and checking the compiler output:
#pragma message ("World object is defined")
If it does not show up, move the pragma to the parent .h file and check the compiler output again. With this you can easily locate the error.
class WorldObject;
class ProjectilObject: public WorldObject
You are forward declaring WorldObject, but to inherit from a class you need the definition, so you have to include the header of WorldObject.
In my case: i delete derived class include from base class header file.
for example:
file 1 :
#include "B.h"
-> A()
file 2:
-> B() : A()
solution : delete #include "B.h" from file1