Deque of user-defined structures - c++

I've got a user-defined structure struct theName and I want to make a deque of these structures (deque<theName> theVar). However when I try to compile I get this error:
In file included from main.cpp:2:
Logger.h:31: error: ISO C++ forbids declaration of ‘deque’ with no type
Logger.h:31: error: expected ‘;’ before ‘<’ token
Why can't I do it this way?
File: Logger.h
#ifndef INC_LOGGER_H
#define INC_LOGGER_H
#include <deque>
#include "Motor.h"
struct MotorPoint {
double speed;
double timeOffset;
};
class Logger{
private:
Motor &motor;
Position &position;
double startTime;
(31) deque<MotorPoint> motorPlotData;
double getTimeDiff();
public:
Logger(Motor &m, Position &p);
//etc...
};
#endif

The namespace of deque is not defined:
std::deque<MotorPoint> motorPlotData;
or
using namespace std;
// ...
deque<MotorPoint> motorPlotData;

deque is in namespace std, so std::deque.

Related

c++ error: mysterious conflicting declaration error

I have carefully looked into my code but don't see why this error comes out.
The error message is the following:
main.cc: In function ‘int main()’:
main.cc:12: error: conflicting declaration ‘traj dim’
main.cc:11: error: ‘dim’ has a previous declaration as ‘unsigned int dim’
and one can reproduce it with the following command
g++ -o a.out realvector.cc traj.cc main.cc
My main.cc is
#include "realvector.h"
#include "traj.h"
using namespace std;
int main() {
unsigned int dim=1000;
traj TRAJ(dim);
return 1;
}
traj is defined in traj.h as
#ifndef TRAJ
#define TRAJ
#include "realvector.h"
class traj{
public:
traj(unsigned int);
~traj();
void next(double &);
private:
unsigned int it,nt; // index, total array size
double dt; // step time
RealVector r,v,a;
};
#endif
the constructor is defined in traj.cc
#include "realvector.h"
#include "traj.h"
traj::traj(unsigned int dim) : nt(dim) {
RealVector r(nt),v(nt),a(nt);
it=0;
}
traj::~traj(){
r.~RealVector();
}
Any idea why this error comes out? Also, is the way to define r,v,a correct? RealVector is a home-defined class with its constructors defined as the following
#include "realvector.h"
using namespace std;
RealVector::RealVector() {}
RealVector::RealVector(unsigned int n)
: dim(n) {
data = new double[dim];
for (int i=0; i<dim; i++)
data[i]=0;
}
RealVector::~RealVector(){
delete[] data;
}
with realvector.h as
#ifndef REAL_VECTOR_H
#define REAL_VECTOR_H
#include <iostream>
class RealVector {
public:
RealVector();
RealVector(unsigned int n);
~RealVector();
int dim;
double* data;
};
#endif
The code is not complete... as a wild guess you also have a TRAJ macro that makes reading what the code really is impossible.
In traj.h you have
#define TRAJ
which defines TRAJ as an empty "string" and this leads to this replace by the preprocessor:
traj TRAJ(dim);
to
traj (dim);
which produces the error message.
I guess you should rename TRAJ in the include file to TRAJ_H and then it works.

Error: Expected template-name before ‘<’ token in 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.

In member function I get the error " invalid use of undefined type 'struct (name)' - forward declaration of 'struct (name)' "

I have the following files in the same project.
Don't bother reading all the blocks of code if you think it's not necessary,
the error messages appear only in the ship.cpp
main.cpp
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#include "chart.cpp"
#define N 10
using namespace std;
int main()
{
int i,j, flagi=-3, flagj=-3;
int test, ports_count=0, treas_count=0;
chart ***mapp;
mapp=new chart **[N];
for(i=0;i<N;i++){mapp[i]=new chart *[N];}
/*missing code initilazing chart ***mapp */
return 0;
}
chart.cpp
#include <iostream>
#include "ship.cpp"
using namespace std;
class chart{
bool isPort;
int weather;
int treasure;
ship* shipPtr;
public:
chart(){isPort=false; weather=0; treasure=0; shipPtr=NULL;}
bool getPort(){return isPort;}
int getWeather(){return weather;}
int getTreasure(){return treasure;}
ship* getShip(){return shipPtr;}
void setPort(bool port){isPort=port;}
void setWeather(int weath){weather=weath;}
void setTreasure(int treas){treasure=treas;}
void setShip(ship* shp){shipPtr=shp;}
};
and
ship.cpp
#include <iostream>
#define N 10
using namespace std;
class ship{
protected:
string name;
int maxhp, curhp, speed, curtreas, x_pos, y_pos;
public:
friend class chart;
//the line above gives error message " forward declaration of 'struct chart' "
static int shipcount;
ship(){shipcount++;}
string getName(){return name;}
int getMaxhp(){return maxhp;}
int getCurhp(){return curhp;}
int getSpeed(){return speed;}
int getCurtreas(){return curtreas;}
int getX_pos(){return x_pos;}
int getY_pos(){return y_pos;}
bool Move(chart ***mapp){
int x, y, blocked=0;
for(x=x_pos-1;x<=x_pos+1;x++){
if((x>-1) && (x<N)){
for(y=y_pos-1;y<=y_pos+1;y++){
if((y>-1) && (y<N)){
/* the line below gives error message "invalid use of undefined type 'struct chart'"*/
if((!mapp[x][y]->getPort) && (mapp[x][y]->getShip==NULL)){
blocked++;
}
}
}
}
}
if(blocked<2){
return false;
}
/* missing the rest of the body of bool Move cause it is too big */
}
}
The compiler gives the following error messages:
"invalid use of undefined type 'struct chart' " in ship.cpp -> line 39
"forward declaration of 'struct chart' " in ship.cpp -> line 12
Why are these errors showing up?
I know the code is probably complex but any help would be appreciated.
Thank you for your time.
The reason this code does not compile is that your ship.cpp file needs a definition of chart class in order to use its members. You fail to provide this definition, prompting the compiler to complain.
Since all of the members of class chart are defined in the class declaration, you can rename chart.cpp to chart.h, and add an #include for it in your ship.cpp file:
#include <iostream>
#include "chart.h"
#define N 10
... // The rest of ship.cpp code
Also replace the name chart.cpp in your main with chart.h.

Expected Initializer before '<' token

I am working with following header definition.
finstrumentor.hpp
#ifndef _FINSTRUMENTOR_HPP_
#define _FINSTRUMENTOR_HPP_
#include "../../../api/probe_API.hpp"
#include <cstdint>
extern Instrumentor* INSTRUMENTOR_INSTANCE;
typedef void (*instrumentation_func)(uint16_t);
class Probe_Info {
public :
uint8_t* patch_addr;
uint8_t size;
};
class Statistics {
public:
uint64_t addr;
uint64_t count;
};
class Finstrumentor : public Instrumentor {
protected :
void (*prolog_func)(uint16_t);
void (*epilog_func)(uint16_t);
public :
Finstrumentor(instrumentation_func prolog_func, instrumentation_func epilog_
void initialize(Instrumentor* inst);
virtual ~Finstrumentor();
};
/* Global Data */
typedef std::unordered_map<int, std::list<Probe_Info*>*> probe_map;
extern probe_map probe_info;
typedef std::map<uint64_t, uint16_t> func_table;
extern func_table functions;
extern uint16_t func_id_counter;
Statistics* global_stats;
#endif /* _FINSTRUMENTOR_HPP_ */
When I include this file I get the following error during compilation.
In file included from ../src/instrumentor.cpp:4:
../src/finstrumentor.hpp: At global scope:
../src/finstrumentor.hpp:43: error: expected initializer before ‘<’ token
../src/finstrumentor.hpp:44: error: ‘probe_map’ does not name a type
../src/finstrumentor.hpp:46: error: expected initializer before ‘<’ token
../src/finstrumentor.hpp:47: error: ‘func_table’ does not name a type
../src/cyg_functions.cpp:16: error: ‘probe_map’ does not name a type
../src/cyg_functions.cpp:17: error: ‘func_table’ does not name a type
You haven't included the headers that define list, map and unordered_map.
#include <list>
#include <map>
#include <unordered_map>
Also, you shouldn't use reserved names like _FINSTRUMENTOR_HPP_. You should (at least) remove the underscore from the beginning.

G++ Error: In file included, then Foo was not declared

I have a problem with my C++ code. If I insert #include "god.hpp" in neuron.hpp, g++ shows me the following error:
In file included from neuron.hpp:4,
from main.cpp:5:
god.hpp:11: error: ‘Neuron’ has not been declared
god.hpp:13: error: ‘Neuron’ was not declared in this scope
god.hpp:13: error: template argument 1 is invalid
god.hpp:13: error: template argument 2 is invalid
main.cpp: In function ‘int main()’:
main.cpp:36: error: no matching function for call to ‘God::regNeuron(Neuron*&)’
god.hpp:11: note: candidates are: long int God::regNeuron(int*)
In file included from god.hpp:5,
from god.cpp:3:
neuron.hpp:10: error: ‘God’ has not been declared
In file included from neuron.hpp:4,
from neuron.cpp:2:
god.hpp:11: error: ‘Neuron’ has not been declared
god.hpp:13: error: ‘Neuron’ was not declared in this scope
god.hpp:13: error: template argument 1 is invalid
god.hpp:13: error: template argument 2 is invalid
and here are the related (parts) of the necessary files:
//main.cpp
#include <string>
#include <vector>
#include "functions.hpp"
#include "neuron.hpp"
#include "god.hpp"
using namespace std;
int main()
{
God * god = new God();
vector<string>::iterator it;
for(it = patterns.begin(); it != patterns.end(); ++it) {
Neuron * n = new Neuron();
god->regNeuron(n);
delete n;
cout << *it << "\n";
}
}
The God ;) Who will handle all neurons...
//god.hpp
#ifndef GOD_HPP
#define GOD_HPP 1
#include <vector>
#include "neuron.hpp"
class God
{
public:
God();
long regNeuron(Neuron * n);
private:
std::vector<Neuron*> neurons;
};
#endif
//god.cpp
#include <iostream>
#include <vector>
#include "god.hpp"
#include "neuron.hpp"
using namespace std;
God::God()
{
vector<Neuron*> neurons;
}
long God::regNeuron(Neuron * n)
{
neurons.push_back(n);
cout << neurons.size() << "\n";
return neurons.size();
}
And at least, my Neuron.
//neuron.hpp
#ifndef NEURON_HPP
#define NEURON_HPP 1
#include "god.hpp" //Evil
class Neuron
{
public:
Neuron();
void setGod(God *g);
};
#endif
//neuron.cpp
#include <iostream>
#include "neuron.hpp"
#include "god.hpp"
Neuron::Neuron()
{
}
void Neuron::setGod(God *g)
{
std::cout << "Created Neuron!";
}
I hope someone can help me to find the error. It happens when I write #include "god.hpp" in neuron.hpp. I searched around three hours with Google, but I had no luck.
Kind regards
-Boris
Compiled with:
g++ -Wall -o getneurons main.cpp functions.cpp god.cpp neuron.cpp
Remove
#include "god.hpp"
and replace it with a forward declaration:
//neuron.hpp
#ifndef NEURON_HPP
#define NEURON_HPP 1
class God; //forward declaration
class Neuron
{
public:
Neuron();
void setGod(God *g);
};
#endif
Same for God.hpp:
//god.hpp
#ifndef GOD_HPP
#define GOD_HPP 1
#include <vector>
class Neuron; //forward declaration
class God
{
public:
God();
long regNeuron(Neuron * n);
private:
std::vector<Neuron*> neurons;
};
#endif
Note that you'll need the includes in your implementation files. (cpp files)
If you use pointers or references to objects as members or use that type as a return type or parameter, the full definition isn't required, so a forward declaration is enough.