"error: expected class-name before '{' token" trying to inheritance - c++

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.

Related

(solved) field has incomplete type of class

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.

How to instantiate an object inheriting from a pure virtual class that uses templates

I'm working on creating an "Array_Base" interface that will allow a user to create either a fixed array or an expandable array of any type. Right now I can't even get the regular Array to work.
I've broken the problem down to a few of its simplest components to try and isolate the issue. I believe it has something to do with my instantiation. I'm using Visual Studio to run the code.
Array_Base.h
#ifndef _ARRAY_BASE_H_
#define _ARRAY_BASE_H_
#include <cstring> // for size_t definition
template <typename T>
class Array_Base
{
public:
typedef T type;
//Default Constructor
virtual void Array_Base(void) = 0;
// Destructor.
virtual ~Array_Base(void) = 0;
protected:
/// Pointer to the actual data.
T* data_;
/// Current size of the array.
size_t cur_size_;
/// Maximum size of the array.
size_t max_size_;
};
#endif // !defined _ARRAY_H_
Array.h
#ifndef _ARRAY_H_
#define _ARRAY_H_
#include <cstring> // for size_t definition
#include "Array_Base.h"
template <typename T>
class Array : public Array_Base
{
public:
/// Type definition of the element type.
typedef T type;
/// Default constructor.
Array (void);
///Destructor
~Array (void);
};
#include "Array.cpp"
#include "Array.inl"
#endif // !defined _ARRAY_H_
Array.cpp
#include <stdexcept> // for std::out_of_bounds exception
#include <iostream>
#define MAX_SIZE_ 20
template <typename T>
Array <T>::Array (void)
:data_(new T[MAX_SIZE_]),
cur_size_(0),
max_size_(MAX_SIZE_)
{ }
template <typename T>
Array <T>::~Array (void)
{
delete[] this->data_;
this->data_ = nullptr;
}
Main.cpp:
#include "Array.h"
int main(void)
{
Array_Base<int>* arr = new Array<int>();
delete arr;
}
I keep getting an error that says: "a value type of "Array" cannot be used to initialize an entity of type "Array_Base" from a red line that appears under the "new" operator in main.
Any help would be greatly appreciated. Thank you!
template <typename T>
class Array : public Array_Base
Array_Base does not name a class. You need to provide it with a template argument.
Do you mean this?
template <typename T>
class Array : public Array_Base<T>

Expected class-name before { token - very simple

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

Cereal library error using enable_shared_from_this

EDIT: Created a minimal VS solution to make it easier to reproduce the error: https://www.dropbox.com/s/pk0t8t2xykjmtc5/test%20cereal%20this.zip (add cereal in includes instead of $(LIBSROOT) where I have it).
I get 2 errors stating that I have no default constructor:
error C2139: 'Node' : an undefined class is not allowed as an argument to compiler intrinsic type trait '__is_constructible'
error C2338: Trying to serialize a an object with no default constructor.
<path>\cereal\details\traits.hpp line 1248
But I think the classes' default constructors should be fine. If I comment out the serialization of Node class variables I get the same errors but with Part class variables.
I have the following code structure (some parts like include guards or unrelated code were omitted, I can of course provide the whole thing if needed, but I wanted to keep it as short as possible):
Shape.h:
#include <cereal/types/memory.hpp>
#include <cereal/types/vector.hpp>
#include <string>
class Part;
typedef std::shared_ptr<Part> PartPtr;
class Node;
typedef std::shared_ptr<Node> NodePtr;
class Shape {
private:
std::vector<PartPtr> parts;
NodePtr root;
std::vector<std::vector<NodePtr>> levels;
public:
Shape();
Shape(std::string fileName);
template <class Archive>
void serialize(Archive & ar) {
ar(parts, levels, root);
}
};
Shape.cpp:
#include "Shape.h"
#include "Node.h"
#include "Part.h"
Shape::Shape() {
}
Shape::Shape(std::string fileName) {
// omitted code
}
Node.h:
#include "PointCloud.h"
#include <cereal/types/vector.hpp>
#include <cereal/types/memory.hpp>
class Part;
typedef std::shared_ptr<Part> PartPtr;
class Node;
typedef std::shared_ptr<Node> NodePtr;
class Node : public std::enable_shared_from_this<Node> {
private:
std::vector<PartPtr> parts;
NodePtr parent;
PointCloud pointCloud;
public:
Node();
Node(std::vector<PartPtr> parts, NodePtr parent);
template <class Archive>
void serialize(Archive & ar) {
ar(parts, parent, pointCloud);
}
};
Node.cpp:
#include "Node.h"
#include "Part.h"
Node::Node() {
}
Node::Node(std::vector<PartPtr> parts, NodePtr parent) : parts(parts), parent(parent) {
// code omitted
}
Part.h:
#include "PointCloud.h"
#include <cereal/types/memory.hpp>
#include <cereal/types/vector.hpp>
class Contact;
class Part;
typedef std::shared_ptr<Contact> ContactPtr;
typedef std::shared_ptr<Part> PartPtr;
class Part : public std::enable_shared_from_this<Part> {
private:
PointCloud pointCloud;
std::vector<ContactPtr> contacts;
public:
Part();
Part(double diameter);
template <class Archive>
void serialize(Archive & ar) {
ar(pointCloud, contacts);
}
};
Part.cpp:
#include "Part.h"
#include "Contact.h"
Part::Part() {
}
Part::Part(double diameter) {
// omitted code
}
Contact class contains a PartPtr as a member variable, PointCloud contains just a bunch of Eigen::Matrix data (should probably be a smart pointer too, to speed up the code, but that shouldn't be important for this problem).
Any suggestions how to fix this? Or could it possibly be a bug? I am using VS2013, which could be the cause as well.
You have only declared class Part;and class Node; in Shape.h. To use a class as template parameter you the compiler also needs a definition of that class.
Include Part.h and Node.h in your Shape.h file. Also include Part.hin Node.h and Contact.h in Part.h.
And put some include guards into the headers:
#ifndef SOME_UNIQUE_IDENTIFIER
#define SOME_UNIQUE_IDENTIFIER
// content of your header goes here
#endif
SOME_UNIQUE_IDENTIFIER would usially be something like NODE_H in your Node.h file and PART_H in your Part.h file.

expected class-name before '{' token c++

I've been googling and reading about this and didn't come up with an answer yet, maybe someone can help me with this please.
the error I get is: expected class-name before '{' token
Carte_num.h
#ifndef CARTE_NUM_H
#define CARTE_NUM_H
#include <string.h>
#include <iostream>
#include "Carte.h"
using namespace std;
class Partie;
class Carte_num : public Carte
{ //<--------------this is where I get the error
public:
Carte_num(int haut,string typ, char coul [8], int nb_p);
~Carte_num();
protected:
int hauteur;
public:
friend Partie;
};
#endif // CARTE_NUM_H
Carte.h
#ifndef CARTE_H
#define CARTE_H
#include <iostream>
#include <string.h>
#include "Partie.h"
using namespace std;
class Joueur;
class Partie;
class Carte
{
public:
Carte();
Carte( string typ, char coul [8], int nb_p);
~Carte();
protected:
char couleur[8];
int nb_pts;
string type;
public:
//bool action(Partie p);
string definir();
bool est_valable(Partie p);
//int getnb_pts() { return(nb_pts);}
friend class Joueur;
friend class Partie;
};
#endif // CARTE_H
the error I get is: expected class-name before '{' token where I indicated earilier
First, the friend declaration should be
friend class Partie;
Second, you need to include the <string> header, without the trailing .h. That is where std::string is defined.
Third, you could have a circular include dependency, for example if Partie.h includes Carte.h or Carte_num.h. You can fix that by removing #include "Partie.h" from Carte.h (you may need to include it in Carte's implementation file).
Another possibility is that you have a missing ; after your class Carte declaration in Carte.h.
Your friend declaration is incorrect.
See the correct format:
class Carte_num : public Carte
{
// ...
friend class Partie;
};