i have the following problem:
I have two classes:
FooClass.h
#include <vector>
#include "BaseClass.h"
using namespace std;
class FooClass
{
public:
vector<BaseClass> vBaseClass;
void doStuff();
void addBaseClass(BaseClass &baseClass);
};
FooClass.cpp
#include <iostream>
#include "FooClass.h"
void FooClass::doStuff()
{
cout << "Well nice done" << endl;
}
void FooClass::addBaseClass(BaseClass &baseClass)
{
baseClass.updateData(this);
vBaseClass.push_back(baseClass);
}
And
BaseClass.h
#include "FooClass.h"
class BaseClass {
public:
void updateData(FooClass *pFooClass);
};
BaseClass.cpp
#include "BaseClass.h"
void BaseClass::updateData(FooClass *pFooClass)
{
//We try to get some data, and if we get the data we call pFooClass->doStuff
pFooClass->doStuff();
}
So, basically the function should be that i create one instance of FooClass.
Then i create multiple instances of BaseClass which i want to add to the vBaseClass vector in the FooClass.
If needet i want to access the BaseClass instance with vBaseClass[key] and call the doStuff() function in FooClass. I give FooClass as a pointer parameter because i want to have still access to the vBaseClass vector from the doStuff() function then which is called by an BaseClass instance.
Everything works fine, but when i add the vector i get the following errors:
error: ‘BaseClass’ was not declared in this scope
vector<BaseClass> vBaseClass;
error: template argument 1 is invalid
vector<BaseClass> vBaseClass;
error: template argument 2 is invalid
error: ‘BaseClass’ has not been declared
void addBaseClass(BaseClass &baseClass);
error: ‘FooClass’ has not been declared
void updateData(FooClass *pFooClass);
error: no matching function for call to ‘BaseClass::updateData(FooClass*)’
baseClass.updateData(this);
If someone knows the solution, many thanks!
Thanks to all, my issue is solved, here is the working code:
FooClass.h
#ifndef TESTPROJECT_FOOCLASS_H
#define TESTPROJECT_FOOCLASS_H
#include <vector>
#include "BaseClass.h"
using namespace std;
class BaseClass;
class FooClass
{
public:
vector<BaseClass> vBaseClass;
void doStuff();
void addBaseClass(BaseClass &baseClass);
};
#endif //TESTPROJECT_FOOCLASS_H
FooClass.cpp
#include <iostream>
#include "FooClass.h"
void FooClass::doStuff()
{
cout << "Well nice done" << endl;
}
void FooClass::addBaseClass(BaseClass &baseClass)
{
baseClass.updateData(this);
vBaseClass.push_back(baseClass);
}
BaseClass.h
#ifndef TESTPROJECT_BASECLASS_H
#define TESTPROJECT_BASECLASS_H
#include "FooClass.h"
class FooClass;
class BaseClass {
public:
void updateData(FooClass *pFooClass);
};
#endif //TESTPROJECT_BASECLASS_H
BaseClass.cpp
#include "BaseClass.h"
void BaseClass::updateData(FooClass *pFooClass)
{
//We try to get some data, and if we get the data we call pFooClass->doStuff
pFooClass->doStuff();
}
main.cpp // console output "Well nice done"
#include "lib/FooClass.h"
int main()
{
FooClass fooclass;
BaseClass baseclass;
fooclass.addBaseClass(baseclass);
}
Thanks to everyone, it is my first activity and question here and i am really impressed how fast i got answers, really thanks!
#
Because i got asked to say what the issue was and how i solved it, i addet
to FooClass.h
#ifndef TESTPROJECT_FOOCLASS_H
#define TESTPROJECT_FOOCLASS_H
class BaseClass;
and to BaseClass.h
#ifndef TESTPROJECT_BASECLASS_H
#define TESTPROJECT_BASECLASS_H
class FooClass;
So the main issue was that i had to declare BaseClass and FooClass as shown above in the header of each others class.
Related
I´m building a program with several classes (data structures like stacks, lists,etc).
There is some class (Concesionario) that i need to use in another (ListaE). The class ListaE uses another class called NodoListaE, which uses two pointers, one to the value of the object (Concesionario) and another to the next position of the list (siguiente).
#ifndef NODOLISTAE_HPP
#define NODOLISTAE_HPP
#include "Concesionario.hpp"
class Concesionario;
class ListaE;
class NodoListaE
{
public:
NodoListaE(Concesionario* conc, NodoListaE* sig = NULL);
private:
Concesionario* conc;
NodoListaE* siguiente;
friend class ListaE;
};
typedef NodoListaE* pnodoListaE;
#endif // NODOLISTAE_HPP
#ifndef LISTAE_HPP
#define LISTAE_HPP
#include "NodoListaE.hpp"
#include "Automovil.hpp"
class Automovil;
class NodoListaE;
class ListaE
{
private:
NodoListaE* primero;
public:
ListaE();
void enlistarOrden(Automovil* automovil);
};
#endif // LISTAE_HPP
#ifndef CONCESIONARIO_HPP
#define CONCESIONARIO_HPP
#include <string>
#include "ListaE.hpp"
class ListaE;
class Concesionario
{
public:
Concesionario();
~Concesionario();
std::string mostrar();
void setZona(std::string letra);
void setNum();
int getNum();
private:
int nc=2;
int num_conc;
std::string zona;
int generadorNumsIntervalo(int min, int max);
ListaE automoviles;//ERROR HERE
};
#endif // CONCESIONARIO_HPP
All the cpp files are not implemented (empty constructor and destructor).
The compiler I´m currently using is MINGWx64.
I´ve tried using forward declarations and it worked for the rest of the classes but not for this one.
The program throws the following error in the **Concesionario ** hpp file: include\Concesionario.hpp|22|error: field 'automoviles' has incomplete type 'ListaE'|
Concesionario is implemented in other classes and the program runs perfectly.
Example of another class implementing Concesionario
#ifndef ARBOL_HPP
#define ARBOL_HPP
#include <iostream>
#include "NodoArbol.hpp"
#include "Concesionario.hpp"
using namespace std;
class Arbol {
public:
Arbol();
void Insertar(Concesionario* concesionario);
private:
pnodoArbol raiz;
pnodoArbol actual;
int contador;
int altura;
bool Vacio(pnodoArbol nodo);
};
#endif // ARBOL_HPP
I`ve also tried deleting this class and creating another one from 0 but the error remains.
Any solution to this problem? Thank you very much.
I'm trying to implement dependency injection in a C++ project. However, due to the structure of the dependencies, I'm getting a segmentation fault which I can't solve.
As an example I constructed the following classes and interfaces. I have a class called MyClass which has a dependency on Dependency. Dependency has a dependency on OtherDependency. To allow for proper testing, I inherit the dependencies from an interface, i.e. IDependency and IOtherDependency. OtherDependency has a function some_function().
In main.cpp I create an instance of MyClass and then try to call some_function(). Unfortunately, this gives a segmentation fault:
Segmentation fault (core dumped)
MyClass.h:
#ifndef MYCLASS_H
#define MYCLASS_H
#include "IDependency.h"
class MyClass
{
public:
MyClass(IDependency *dependency);
~MyClass();
IDependency *_dependency = nullptr;
};
#endif
MyClass.cpp:
#include "MyClass.h"
#include <iostream>
MyClass::MyClass(IDependency *dependency) : _dependency(dependency) {}
MyClass::~MyClass() {}
Dependency.h:
#ifndef DEPENDENCY_H
#define DEPENDENCY_H
#include "IDependency.h"
#include "IOtherDependency.h"
class Dependency : public IDependency
{
public:
Dependency(IOtherDependency *other_dependency);
~Dependency();
IOtherDependency *_other_dependency = nullptr;
};
#endif
Dependency.cpp:
#include "Dependency.h"
#include <iostream>
Dependency::Dependency(IOtherDependency *other_dependency) : _other_dependency(other_dependency) {}
Dependency::~Dependency() {}
IDependency.h:
#ifndef IDEPENDENCY_H
#define IDEPENDENCY_H
#include "IOtherDependency.h"
class IDependency
{
public:
IOtherDependency *_other_dependency;
};
#endif
OtherDependency.h:
#ifndef OTHERDEPENDENCY_H
#define OTHERDEPENDENCY_H
#include "IOtherDependency.h"
class OtherDependency : public IOtherDependency
{
public:
OtherDependency();
~OtherDependency();
void some_function();
};
#endif
OtherDependency.cpp:
#include "OtherDependency.h"
#include <iostream>
OtherDependency::OtherDependency() {}
OtherDependency::~OtherDependency() {}
void OtherDependency::some_function()
{
std::cout << "I am OtherDependency." << std::endl;
}
IOtherDependency.h:
#ifndef IOTHERDEPENDENCY_H
#define IOTHERDEPENDENCY_H
class IOtherDependency
{
public:
virtual void some_function() = 0;
};
#endif
main.cpp:
int main()
{
OtherDependency *other_dependency = new OtherDependency;
Dependency *dependency = new Dependency(other_dependency);
MyClass my_class(dependency);
my_class._dependency->_other_dependency->some_function();
}
What am I doing wrong / do I need to change?
You have two variables called _other_dependency: one in IDependency, the other in Dependency. The Dependency constructor initialized the latter, while the one in the IDependency class retains its default nullptr value.
When you access my_class._dependency->_other_dependency, the other_dependency will be the one in IDependency, because _dependency points to the base class.
One way to fix this is to remove the other_dependency from Dependency, and pass the value from the Dependency constructor to IDependency to properly initialize its member.
I got two classes separated in four files. The main class includes a sub class and needs to execute functions of it (not shown in the minimal example code). What I want to do is to execute a function of the main class in the scope of the subclass.
I think some ideas would be to inherit the functions in the sub class but I could not figure out how to do this.
MainClass.cpp
#include "MainClass.hpp"
void MainClass::mainCallback() {
std::cout << "[mainCallback] executed" << std::endl;
}
void MainClass::subCallback() {
std::cout << "[subCallback] executed" << std::endl;
}
int main() {
MainClass mainClass;
mainClass.mainCallback();
SubClass subClass;
subClass.activateSubClass();
return 0;
}
MainClass.hpp
#pragma once
#include "SubClass.hpp"
#include <iostream>
class MainClass{
public:
void mainCallback();
void subCallback();
};
SubClass.cpp
#include "SubClass.hpp"
void SubClass::activateSubClass(){
mainClass.subCallback(); //TODO call this function from this scope
}
SubClass.hpp
#pragma once
class SubClass{
public:
void activateSubClass();
};
The error in SubClass.cpp is of course:
error: use of undeclared identifier 'mainClass'
Just subclass the subclass:
class SubClass: public MainClass {
public:
void activateSubClass();
};
This (public) way the SubClass makes all methods of MainClass callable in SubClass instances. You could also private inherit. That way only activateSubClass() 'ld be callable.
In activateSubClass you can call directly the methods of the parent class:
void SubClass::activateSubClass(){
mainClass.subCallback(); //TODO call this function from this scope
}
Don't forget to include MainClass.hpp in SubClass.hpp
You try to call a MainClass.subCallback() without having an instance of MainClass. According to me, this is the typical use case for static methods.
Then, I think you make your #include directives the wrong way. Indeed, MainClass does not seem to need to know SubClass but the opposite is true. I think it is better to include MainClass.hpp in SubClass.hpp. This will solve your circle dependencies problem.And you can write your main() function in another file.
EDIT: Example
MainClass.hpp:
#pragma once
class MainClass
{
public:
void mainCallback();
static void subCallback(); // mak it static to be able to call it without an instance of the class
};
MainClass.cpp:
#include "MainClass.hpp"
#include <iostream>
void MainClass::mainCallback()
{
std::cout << "[mainCallback] executed" << std::endl;
}
void MainClass::subCallback()
{
std::cout << "[subCallback] executed" << std::endl;
}
SubClass.hpp:
#pragma once
class SubClass
{
public:
void activateSubClass();
};
SubClass.cpp:
#include "SubClass.hpp"
#include "MainClass.hpp" // MainClass inclusion
void Suclass::activateSubClass()
{
MainClass::subCallback(); // Call of the static method
}
main.cpp:
#include "MainClass.hpp"
#include "SubClass.hpp"
int main()
{
MainClass mc;
mc.mainCallback();
SubClass sc;
sc.activateSubClass(); // Will call MainClass::subCallback()
return 0;
}
I hope it can help you.
The following source aims to create an abstract base class (SubsystemClass) and a derived final class (DisplaySubsystemClass). Implementation of the constructor for the derived class fails on error "no instance of overloaded function "DisplaySubsystemClass::DisplaySubsystemClass" matches the specified type". I am baffled.
SubsystemClass.hpp
#ifndef SUBSYSTEMCLASS_HPP
#define SUBSYSTEMCLASS_HPP
#include <memory>
#include "DriverClass.hpp"
class SubsystemClass
{
protected:
std::shared_ptr<DriverClass> _driver;
public:
virtual ~SubsystemClass();
enum DriverCatalog;
};
#endif
DisplaySubsystemClass.hpp
#ifndef DISPLAYSUBSYSTEMCLASS_HPP
#define DISPLAYSUBSYSTEMCLASS_HPP
#include <memory>
#include "../SubsystemClass.hpp"
#include "DisplayDriverClass.hpp"
class DisplaySubsystemClass final : public SubsystemClass
{
private:
std::shared_ptr<DisplayDriverClass> _driver;
public:
DisplaySubsystemClass(DisplaySubsystemClass::DriverCatalog driverCatalogItem);
~DisplaySubsystemClass();
enum DriverCatalog {
DISPLAY_DRIVER_CONSOLE,
DISPLAY_DRIVER_CURSES,
DISPLAY_DRIVER_SFML,
DISPLAY_DRIVER_OPENGL
};
};
#endif
DisplaySubsystemClass.cpp
#include <memory>
#include "DisplaySubsystemClass.hpp"
#include "SFMLDisplayDriverClass.hpp"
DisplaySubsystemClass::DisplaySubsystemClass(DisplaySubsystemClass::DriverCatalog driverCatalogItem)
{
}
DisplaySubsystemClass::~DisplaySubsystemClass()
{
}
The enumeration should be declared before its using as a parameter type in the constructor.
I've seen many answers for this question, know what to look for and still can see that. Looks like some obvious problem.
Algorithm.h:
#ifndef ALGORITHM_H
#define ALGORITHM_H
#include <iostream>
using namespace std;
template <typename T>
class Algorithm
{
private:
T data;
T result;
public:
Algorithm(T in){
data = in;
}
void compute();
void displayData(){
cout<<data<<endl;
}
T getResult(){
return result;
}
};
#endif // ALGORITHM_H
Bubble.h:
#ifndef BUBBLE_H
#define BUBBLE_H
#include "algorithm.h"
class Bubble : public Algorithm{
public:
Bubble();
};
#endif // BUBBLE_H
main.cpp
#include "bubble.h"
#include <iostream>
using namespace std;
int main()
{
Algorithm<int> a(1);
Algorithm<char> b('a');
a.displayData();
b.displayData();
return 0;
}
Error is:
/home/user/Projects/Algorithms/main.cpp:1: In file included from
../Algorithms/main.cpp:1:0: /home/user/Projects/Algorithms/bubble.h:6:
error: expected class-name before '{' token class Bubble : public
Algorithm{
^
Why compiler cannot see Algorithm class? I included it in Bubble.h, so?
You forgot to provide the template argument for Algorithm. If you fix this, your code compiles fine. (Live)
Bubble inherits from the Algorithm class, which is a template. So it also needs the template specification:
#ifndef BUBBLE_H
#define BUBBLE_H
#include "algorithm.h"
template <typename T>
class Bubble : public Algorithm<T> {
public:
Bubble();
};
#endif // BUBBLE_H