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.
Related
In Hire.h i declared two typedefs. In Customer.h i would be able to use this typedefs. How to fix it?
#pragma once
#include "typedefs.h"
#include "Vehicle.h"
#include "Customer.h"
namespace rent {
typedef std::shared_ptr<Hire> shared_Hire_t;
typedef std::map<uuid_t, shared_Hire_t> mapHirePtr_t;
class Hire
{
public:
Hire(date_t start_date, Vehicle *vehicle, Customer *customer);
virtual ~Hire();
private:
uuid_t uuid;
date_t start_date;
date_t end_date;
uint_t hire_days;
double day_price;
double cost;
Vehicle *vehicle;
Customer *customer;
};
}
-
#pragma once
#include <iostream>
#include <map>
#include <memory>
#include "typedefs.h" //uint_t, uuid_t
#include "CustomerType.h"
namespace rent {
class Hire;
class Customer
{
public:
enum EnumCustomerType {standard, medium, premium}; //to set a CustomerType
Customer(std::string name, std::string surname, std::string pesel, double balance = 0.0, EnumCustomerType enum_customer_type = standard);
virtual ~Customer();
void add_hire(shared_Hire_t hire);
void end_hire(shared_Hire_t hire);
protected:
std::string name;
std::string surname;
std::string pesel;
double balance;
EnumCustomerType enum_customer_type;
double discount;
mapHirePtr_t current_hires;
uint_t hires_count;
private:
std::unique_ptr<CustomerType> customer_type;
};
}
-
Customer.h:31:17: error: 'shared_Hire_t' has not been declared void
add_hire(shared_Hire_t hire);
^ Customer.h:32:31: error: 'shared_Hire_t' has not been declared
void end_hire(shared_Hire_t hire);
^ Customer.h:42:3: error: 'mapHirePtr_t' does not name a type mapHirePtr_t current_hires;
^ Customer.cpp:87:14: error: prototype for 'void
rent::Customer::add_hire(rent::shared_Hire_t)' does not match any in
class 'rent::Customer'
void Customer::add_hire(shared_Hire_t hire)
^ In file included from Customer.cpp:1:0: Customer.h:31:8: error: candidate is: void
rent::Customer::add_hire(int) void add_hire(shared_Hire_t hire);
^ Customer.cpp:94:14: error: prototype for 'void rent::Customer::end_hire(rent::shared_Hire_t)' does not match any in
class 'rent::Customer'
void Customer::end_hire(shared_Hire_t hire)
^ Customer.cpp:1:0: Customer.h:32:22: error: candidate is: void rent::Customer::end_hire(int)
void end_hire(shared_Hire_t hire);
PS. How to separate two blocks of code in an appropriate way in stackoverflow?
You have to include Hire.h in Customer.h for the typedefs to be visible.
As that would introduce a circular reference i would suggest putting the typedefs in a seperate file.
You need to #include "Hire.h in Customer.h to make the definitions available there.
Unfortunately, that creates a circular dependency between the headers.
Fortunately, Hire.h doesn't need the definitions of Customer and Vehicle, so you can stop including them and just forward-declare the classes.
Hire.h:
#include "typedefs.h"
namespace rent {
class Vehicle;
class Customer;
class Hire;
typedef std::shared_ptr<Hire> shared_Hire_t;
typedef std::map<uuid_t, shared_Hire_t> mapHirePtr_t;
class Hire
{
// ...
Customer.h:
#include <iostream>
#include <map>
#include <memory>
#include "typedefs.h" //uint_t, uuid_t
#include "CustomerType.h"
#include "Hire.h"
namespace rent {
class Customer
{
// ...
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.
My professor gave me the following .h file as part of a programming assignment to make a lexer.
#ifndef LEXER_H
#define LEXER_H
#include "token.h"
#include <istream>
class Lexer {
public:
Lexer(std::istream& input_stream);
Token next_token();
private:
int current_line;
int current_column;
std::istream& input_stream;
};
#endif
I started making the .cpp file as shown below:
#include "lexer.h"
Lexer::Lexer(std::istream& inputstream){
}
When I try to compile, I get the error message:
lexer.cpp: In constructor ‘Lexer::Lexer(std::istream&)’:
lexer.cpp:4: error: uninitialized reference member ‘Lexer::input_stream’
The compiler is explicit : a reference member must be initialized in the member initialization list :
Lexer::Lexer(std::istream& inputstream) : input_stream(inputstream) {
}
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.
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.