Error: Expected template-name before ‘<’ token in C++ - c++

I got the error in the title wich cause a lot of other error and I really don't know how to solve it, the error starts when I tried to declare a list ListeTriee in the main, before that i didn't have any issue with that.
Errors:
In file included from Cours.h:10:0,
from ListeBase.h:13,
from ListeBase.cxx:1:
Liste.h:12:51: error: expected template-name before ‘<’ token
template<typename T> class Liste: public ListeBase<T>
^
Liste.h:12:51: error: expected ‘{’ before ‘<’ token
Liste.h:12:51: error: expected unqualified-id before ‘<’ token
In file included from ListeBase.h:13:0,
from ListeBase.cxx:1:
Cours.h:19:20: error: field ‘groupes’ has incomplete type
Liste<int> groupes;
^
Cours.h: In member function ‘void Cours::insererGroupe(int)’:
Cours.h:28:13: error: ‘groupes’ was not declared in this scope
groupes.insere(val);
The files are :
ListeBase.h:
#ifndef LISTE_BASE
#define LISTE_BASE
#include <stdlib.h>
#include <iostream>
#include <time.h>
using namespace std;
#include "Timing.h"
#include "Professeur.h"
#include "Groupe.h"
#include "Local.h"
#include "Cours.h"
template<typename T> struct Cellule
{
T valeur ;
Cellule<T> *suivant ;
Cellule(T v, Cellule<T> *s) : valeur(v), suivant(s) {};
};
template<typename T> class Iterateur;
template<typename T> class ListeBase
{
protected:
Cellule<T> *pTete ;
public:
ListeBase();
~ListeBase();
bool estVide() const;
int getNombreElements() const;
void Affiche() const;
virtual T* insere(const T & val)=0;
friend class Iterateur<T>;
};
#endif
Liste.h
#ifndef LISTE
#define LISTE
#include <stdlib.h>
#include <iostream>
#include <time.h>
using namespace std;
#include "ListeBase.h"
#include "Timing.h"
template<typename T> class Liste: public ListeBase<T>
{
public:
T* insere(const T & val);
};
#endif
Cours.h
#ifndef COURS
#define COURS
#include <stdlib.h>
#include <string.h>
#include <iostream>
using namespace std;
#include "Liste.h"
#include "Event.h"
#include "Professeur.h"
#include "Groupe.h"
class Cours: public Event
{
private:
char* professeur;
Liste<int> groupes;
public:
Cours();
Cours(const Cours &);
~Cours();
const char* getProfesseur() const;
void setProfesseur(Professeur &);
void insererGroupe(int val)
{
groupes.insere(val);
}
void Affiche();
};
#endif
Thank you, if you need more details just ask.

You have a circular dependency: Cours.h depends on Liste.h which depends on ListeBase.h which depends on Cours.h which depends on. Liste.h...
I don't see any reason for Cours.h to be included in ListeBase.h, so simply don't include it there. You should not include header files you don't actually need, and IMHO you should not include header files in header files at all if it can be avoided, Instead include all needed header files in the source files in the order needed.

Related

C++ Compile error:'HelpModel' was not declared in this scope - using an optional

I'm getting the following error when compiling, using GCC 11.2.0:
/src/SearchController.h:12:23: error: ‘HelpModel’ was not declared in this scope
12 | std::optional<HelpModel> searchModel;
| ^~~~~~~~~
/src/SearchController.h:12:32: error: template argument 1 is invalid
12 | std::optional<HelpModel> searchModel;
| ^
I'm including the HelpModel class in the header, but this is pretty much my first C++ program so my understanding of this is pretty thin at the min.
Here's the SearchControlle.h file
#ifndef ARCH_HELP_SEARCH_CONTROLLER
#define ARCH_HELP_SEARCH_CONTROLLER
#include <string>
#include <optional>
#include "HelpModel.h"
class SearchController
{
public:
std::optional<HelpModel> searchModel;
void searchedFor(std::string searchTerm);
};
#endif
And here's the HelpModel.h file:
#ifndef ARCH_HELP_HELP_MODEL
#define ARCH_HELP_HELP_MODEL
#include <vector>
#include "Topic.h"
#include "TerminalView.h"
class HelpModel
{
public:
HelpModel(TerminalView view);
private:
TerminalView view;
std::vector<Topic> topics;
void getTopics();
void pushToView();
};
#endif
Here's TerminalView.h
#ifndef ARCH_HELP_TERMINAL_VIEW
#define ARCH_HELP_TERMINAL_VIEW
#include <vector>
#include <string>
#include "SearchController.h"
#include "Topic.h"
class TerminalView
{
public:
void makeHeader();
void update(std::vector<Topic> modelData);
private:
std::string searchTerm;
std::vector<Topic> helpData;
SearchController controller;
void makeSearchInput();
void printToTerminal();
void printAnswers(std::vector<std::string> answers);
};
#endif
What I would like to be able to do is then assign an instance of HelpModel to the SearchController like so - say in main.cpp:
HelpModel model(terminal);
SearchController controller;
controller.searcModel = model;
Any advice greatly appreciated

Template class does not name a type error, separated definition and declaration for header

I am attempting to create a templated vector class, but upon compilation I am receiving an error of
def.hpp:3:1: error: 'TempVector' does not name a type
I keep referring to reference material and my syntax and handling of the header file declaration and definition (.h and .hpp) seem right to me, but I can not figure out what I am overlooking.
Below is the three files I am working with, thank you.
driver.cpp:
#include <iostream>
#include <string>
#include "dec.h"
using namespace std;
int main() {
TempVector <int> v1;
cout<<"ran successfully"<<endl;
}
dec.h:
#ifndef DEC_H
#define DEC_H
#include <iostream>
#include <utility>
// Declaration of class Vector
template <typename T>
class TempVector {
public:
TempVector ();
private:
T* array;
static const unsigned int spare = 10;
};
#include "def.hpp"
#endif
def.hpp:
template <typename T>
TempVector<T>::TempVector () {
std::cout<<"ran successfully";
}

Undeclared identifier and type 'int' unexpected error when calling a class from main

Hey guys so I am working on a Hash program in C++ for my class. I am using a template T class for my header file and when I try to call the class constructor from main it gives me a undeclared identifier and type 'int' unexpected error. Here is my HashTable.h file:
#pragma once
#include "Record.h"
#define MAXHASH 1000
#ifndef HASHTABLE_H
#define HASHTABLE_H
using namespace std;
template <class T> class HashTable
{
public:
HashTable();
~HashTable();
bool insert(int, T, int&);
bool remove(int);
bool find(int, T&);
float alpha();
private:
int key;
T value;
};
#endif
and here is my main:
#include "HashTable.h"
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;
int main()
{
HashTable<int> *test = new HashTable<int>();
return 0;
}
Here's the constructor in the .cpp file as well:
#pragma once
#include "stdafx.h"
#include "HashTable.h"
#include "Record.h"
#define HASHTABLE_CPP
#ifndef HASTABLE_CPP
template <class T>
HashTable<T>::HashTable()
{
Record hashArray = new Record[MAXHASH];
for (int i = 0; i < MAXHASH; i++)
{
hashArray[i]->key = 0;
hashArray[i]->value = NULL;
}
}
The specific errors I am getting are:
Error C2062 type 'int' unexpected identifier
Error C2065 'HashTable': undeclared identifier
Both the errors point to the call line in main.
It is difficult because I can't test my program until I can get this test hash to work. Any input on how to fix this issue would be awesome!
the old Microsoft application file extensions interface "stdafx.h" has to be the first directive listed if the pre-compiled header is used. It actually works best because VS expects it...I always use it.

Template class CArray compile under gcc, Error: the class which used as a parameter of a function undeclared

#ifndef _ALLOCATOR_H
#define _ALLOCATOR_H
#include "ace/OS_NS_stdio.h"
#include "ace/OS_NS_string.h"
#include "ace/MMAP_Memory_Pool.h"
#include "ace/Malloc_T.h"
#include "ace/Null_Mutex.h"
#include "ace/PI_Malloc.h"
#include "ace/OS_NS_unistd.h"
#include "ace/Thread_Mutex.h"
#include "ace/Process_Mutex.h"
#include <string>
using namespace std;
class CAllocator
{
public:
CAllocator();
~CAllocator(void);
public:
bool Create(char* strPoolName);
void Destroy();
public:
char* NewMem(char* strBlockName,int nBlockSize);
char* FindMem(char* strBlockName);
bool FreeMem(char* strBlockName);
private:
typedef ACE_Malloc_T <ACE_MMAP_MEMORY_POOL,
ACE_Process_Mutex,
ACE_PI_Control_Block>
ALLOCATOR;
ALLOCATOR* m_pAllocator;
};
#endif //_ALLOCATOR_H
#ifndef _ARRAY_H
#define _ARRAY_H
#include "allocator.h"
template<typename T>
class CArray
{
public:
bool CreateArray(CAllocator* pAllocator,char* strArrayName,int nArraySize);
bool OpenArray(CAllocator* pAllocator,char* strArrayName);
public:
CArray()
{
m_pArrayData = NULL;
}
~CArray()
{
m_pArrayData = NULL;
}
public:
T* GetObject(int nIndex);
int GetArraySize();
private:
T* m_pArrayData;
};
#include "array.cpp"
#endif //_ARRAY_H
In the function CreateArray of the template class CArray,
the gcc compiler says CAllocator has not been declared.
but all the code worked under vs2010
please help,thanks gurus
Please stop naming like _ALLOCATOR_H. Name start with __ or _ followed by a capital letter is reserved for using by compiler and standard.
– Danh
change #ifndef _ALLOCATOR_H #define _ALLOCATOR_H to #ifndef ALLOCATOR_H #define ALLOCATOR_H Everything is ok! Thank u all – Jack

Expected initializer before * token

So I have this header file:
#pragma once
#include "engine.hpp"
namespace spacecubes
{
extern engine* _engine;
}
and the included engine.hpp:
#pragma once
#include <iostream>
#include "glinclude.hpp"
#include "debug.hpp"
#include "convert.hpp"
#include "renderer.hpp"
#include "global.hpp"
namespace spacecubes {
void display();
class engine {
renderer renderengine;
public:
void start(int argc, char* argv[]);
void stop(int status = 0);
void poll();
renderer getRenderEngine() {return renderengine;}
};
}
What the compiler reported later on was:
g++ -c -o bin/obj/engine.o src/engine.cpp
In file included from src/engine.hpp:9,
from src/engine.cpp:1:
src/global.hpp:7: error: expected initializer before '*' token
src/engine.cpp: In function 'void spacecubes::display()':
src/engine.cpp:5: error: '_engine' was not declared in this scope
I don't get it. What did it mean that it expected an init? Thanks in advance!
Replace
#include "engine.hpp"
with a forward declaration:
namespace spacecubes { class engine; }