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
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 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.
#ifndef CLASSB
#define CLASSB
#include "ClassA.h"
namespace name {
class ClassB
{
public:
static Handle conn();
};
}
#endif
-
#include "ClassB.h"
Handle name::ClassB::conn()
{
return getHandle(ClassA::it().str());
}
-
#ifndef CLASSA
#define CLASSA
#include "ClassB.h"
namespace name {
class ClassA
{
public:
template <typename T>
T myFunc(const std::string&)
{
auto tmp = ClassB::conn();
}
};
}
#endif
Calling ClassB::conn() gives a compiler error which says that the class ClassB is not declared. When I forward declare it I get an error message about an incomplete type.
I can't move the template function to my .cpp files as it is a template function. So, how to fix this?
Just remove #include "ClassA.h" from class B's header and it should work. But there appear to be multiple compilation problems with your code so it's hard to say (missing function getHandle, missing it(), missing type Handle etc).
I tried to create a queue that inherits from list and get this error:
"error: expected class-name before '{' token"
these are the codes that I have ...
cola_lista.cpp
#ifndef cola_hereda_lista
#define cola_hereda_lista
#include <iostream>
#include "lista_t.hpp"
//#include "nodo_t.hpp"
using namespace std;
template <class T>
class cola : public lista{
private:
nodo<T> *frente, *final;
public:
cola();
bool es_vacia();
int longitud(); //
void encolar(T e);
void desencolar(); //precondicion ¬es_vacia
T obtener_frente(); //precondicion ¬es_vacia
~cola();
};
#endif
lista.hpp
#ifndef lista_template
#define lista_template
#include <iostream>
#include "nodo_t.hpp"
using namespace std;
template <class T>
class lista{
private:
nodo<T> *primero, *ultimo;
int cantidad;
public:
//
};
nodo.hpp
#include <iostream>
#ifndef nodo_template
#define nodo_template
using namespace std;
template <class T>
class nodo{
private:
T elemento;
nodo<T> *siguiente;
public:
nodo();
T get_elem();
void set_elem(T e);
nodo<T>* get_siguiente();
void set_siguiente(nodo<T> *sigui);
~nodo();
};
I've been hours trying to figure out what is what is ill-posed in the code. Help!
change your code to this
template <class T>
class cola : public lista<T>{
You need to adjust your declaration of cola:
template <class T>
class cola : public lista<T>
^^^
cola is a class template and you need to specify the type. Also you should not put using namespace std; in your header files and I would discourage you from using it in general, this previous thread Why is 'using namespace std;' considered a bad practice in C++? goes into why.
I am trying to implement an observer pattern with a template subject class. The observers don't (need to) know the subjects type, so I made an interface for the attach method without this type. This is my implementation:
SubjectInterface.h
#ifndef SUBJECTINTERFACE_H_
#define SUBJECTINTERFACE_H_
#include <list>
#include "Observer.h"
// Template-independant interface for registering observers
class SubjectInterface
{
public:
virtual void Attach(Observer*) = 0;
}; // class SubjectInterface
#endif // SUBJECTINTERFACE_H_
Subject.h
#ifndef SUBJECT_H_
#define SUBJECT_H_
#include <list>
#include "Observer.h"
#include "SubjectInterface.h"
template <class T>
class Subject : public SubjectInterface
{
public:
Subject();
~Subject();
void Attach(Observer*);
private:
T mValue;
std::list<Observer*> mObservers;
}; // class Subject
#include "Subject.cpp"
#endif // SUBJECT_H_
Subject.cpp
template <class T>
Subject<T>::Subject()
{
}
template <class T>
Subject<T>::~Subject()
{
}
template <class T>
void Subject<T>::Attach(Observer* test)
{
mObservers.push_back(test);
}
Observer.h
#ifndef OBSERVER_H_
#define OBSERVER_H_
#include "SubjectInterface.h"
#include <iostream>
class Observer
{
public:
Observer(SubjectInterface* Master);
virtual ~Observer();
private:
SubjectInterface* mMaster;
}; // class Observer
#endif // OBSERVER_H_
Observer.cpp
#include "Observer.h" // include header file
Observer::Observer(SubjectInterface* Master)
{
Master->Attach(this);
}
Observer::~Observer()
{
}
When I compile this using the gcc 4.3.4, I get the following error message:
SubjectInterface.h:10: error: ‘Observer’ has not been declared
I don't understand this, because the Observer is included just a few lines above. When I change the pointer type from Observer* to int*, it compiles OK. I assume that there is a problem with the template subject and the non-template interface to it, but that is not what gcc is telling me and that doesn't seem to be the problem when using int*.
I searched for template/observer, but what I found (e.g. Implementing a Subject/Observer pattern with templates) is not quite what I need.
Can anyone tell me, what I did wrong or how I can call the templated attach-method from a non-template observer?
You have a circular include chain, SubjectInterface.h includes Observer.h which in turns includes SubjectInterface.h.
This means that the include guards will prevent Observer from being visible. To fix it instead forward declare Observer.
// SubjectInterface.h
#ifndef SUBJECTINTERFACE_H_
#define SUBJECTINTERFACE_H_
#include <list>
class Observer; //Forward declaration
// Template-independant interface for registering observers
class SubjectInterface
{
public:
virtual void Attach(Observer*) = 0;
}; // class SubjectInterface
#endif // SUBJECTINTERFACE_H_
You have a circular dependency; Observer.h includes SubjectInterface.h, and vice versa. You will need to break this with a forward declaration.