I'm attempting to compile the code below, but the compiler gives the error
Struct.h:38:9: error: ‘priority_queue’ in namespace ‘std’ does not name a type
Several searches failed to reveal an answer so I'm hoping you guys can help out. The code was partially based on the sample code given at the c++ reference site.
struct aimedShot;
union moveFunc;
struct timeCommand;
struct aimedShot
{
void (*move) (Dot*, SDL_Event&, double x, double y);
double x;
double y;
};
//Holds the kind of function used
union moveFunc
{
void (*notAimed) (Dot*);
aimedShot aimed;
};
//Dot to be operated on and the appropriate operator with time
struct timeCommand
{
Dot* target;
moveFunc command;
int time;
bool type; //True indicates aimed (integer inputs), False indicates unaimed
};
class CompareCommand
{
public:
bool operator()(timeCommand& c1, timeCommand& c2) //Return true if c1 comes first
{
return (c1.time < c2.time);
}
};
typedef std::priority_queue< timeCommand, std::vector<timeCommand>, CompareCommand> commandTimeline;
To be able to use std::priority_queue<> class template you need to #include <queue> standard header.
Related
I have been attempting to make some code, but I am a bit new to c++ and need some help.
I cannot instantiate class Player as a pointer, because it's an "incomplete type" (or undefined type, vs says both). Below are some (simplified, albeit not very) versions of my code:
Entity.h
#pragma once
#include <vector>
class Entity
{
public:
static void init();
class EntityObject;
class Player;
static std::vector<EntityObject*> entities;
};
Entity.cpp
#include "Entity.h"
void Entity::init()
{
entities = std::vector<EntityObject*>();
}
class Entity::EntityObject
{
private:
float velX, velY, x, y;
public:
EntityObject(float xa, float ya) { x = xa; y = ya; }
float getVelX() { return velX; }
float getVelY() { return velY; }
float getX() { return x; }
float getY() { return y; }
};
class Entity::Player : EntityObject
{
public:
Player(float xa, float ya) : EntityObject(xa, ya)
{
printf("Player created");
}
};
Can anyone tell me why
#include "Entity.h"
int main(int argc, char* argv)
{
Entity::init();
Entity::EntityObject* player = new Entity::Player(10.0f, 10.0f);
Entity::entities.push_back(player);
}
gives an incomplete/undefined type?
Thanks.
Edit:
The errors are:
Both errors direct to this line: Entity::EntityObject* player = new Entity::Player(10.0f, 10.0f);
Error (active) E0070 incomplete type is not allowed
Error C2027 use of undefined type 'Entity::Player'
You defined the Entity::Player class in the .cpp file, not in the .h file. Therefore, even though the main() includes the .h file, it does not know about Entity::Player.
Entity::Player is forward declared in Entity.h.
When the compiler compiles your main.cpp module, it does not know anything about this class except that it exists, in particular it has no idea that this class as a constructor taking two float : Player(float xa, float ya)
=> Your issue is related to forward declaring, not nested class.
Read this thread to understand your problem
What are forward declarations in C++?
Read this one to understand what you can and what you can't do with forward declaration
When can I use a forward declaration?
I have posted this already one day ago but I did not know how to add a second question to my first question.
I get a forward declaration error. You told me that it should be no problem if I define my class in KdTree.h and my functions, structs, etc in KdTree.cpp. However it does not work so here I post my whole code:
This is my header:
#Includes <iostream>
#Includes others
using namespace TooN;
#ifndef KDTREE_H_
#define KDTREE_H_
class KdTree {
public:
KdTree(std::vector<TooN::Vector<3,GLfloat> > & ,size_t);
struct node;
struct temptask;
struct temphold;
struct ...;
double function(...);
...;
std::vector<node> nodes;
std::vector < int > searchInRadius(const TooN::Vector<3, GLfloat> &,float , const std::vector<TooN::Vector<3,GLfloat> > & );
};
#endif
So and this is my KdTree.cpp:
#include "KdTree.h"
KdTree::KdTree(std::vector<TooN::Vector<3,GLfloat> > & points, size_t pointssize){
const size_t stacksize = 200;
nodes.push_back(node());
temphold tasksarray[stacksize] = {0,pointssize-1,0,0};
int taskindex = 0;
...A lot more stuff
if (!is_leaf(n)){
do something;
}
}
And then my functions in KdTree.cpp
struct KdTree::node{
std::size_t a, b, c;
node() : a(-1), b(-1), c(-1) {}
bool is_leaf(const node &n){
return blablabla;
}
};
And here my first 3 Compiler messages:( :
jni/Visual/KdTree.cpp: In constructor 'KdTree::KdTree(const std::vector<TooN::Vector<3, float> >&, size_t)':
jni/Visual/KdTree.cpp:32:23: error: invalid use of incomplete type 'struct KdTree::node'
nodes.push_back(node());
^
In file included from jni/Visual/KdTree.cpp:8:0:
jni/Visual/KdTree.h:29:9: error: forward declaration of 'struct KdTree::node'
struct node;
^
jni/Visual/KdTree.cpp:33:54: error: elements of array 'KdTree::temphold tasksarray [200]' have incomplete type
temphold tasksarray[stacksize] = {0,pointssize-1,0,0}; //starting at firstpoint = 0 index, lastpoint = lastindex, nodenumber = 0 index, dim = x-dimension (i.e. 0)
And a lot more of these kind of messages.
The struct definitions must still occur before their first use in the .cpp file.
#include "KdTree.h"
struct KdTree::node{
std::size_t a, b, c;
node() : a(-1), b(-1), c(-1) {}
bool is_leaf(const node &n){
return blablabla;
}
};
KdTree::KdTree(std::vector<TooN::Vector<3,GLfloat> > & points, size_t pointssize){
...A lot more stuff
}
Put the implementation of node at the beginning of KdTree.cpp, or at least before its first use:
#include "KdTree.h"
struct KdTree::node {
// ...
};
KdTree::KdTree() {
// ...
}
I am using a Dijkstra for finding a shortest path in graph. I used to use std::set but I think a heap could perform better. But I am having troubles using the d_ary_heap or the priority_queue.
This is a simplified version:
#include <string>
#include <inttypes.h> // for uint32_t
#include <boost/heap/fibonacci_heap.hpp>
#include <boost/heap/binomial_heap.hpp>
#include <boost/heap/d_ary_heap.hpp>
#include <boost/heap/priority_queue.hpp>
using namespace std;
struct __attribute__ ((__packed__)) __attribute__((aligned(8)) Cmp {
// Do *not* reorder the following two fields or comparison will break.
const int32_t _id;
const float _cost;
Cmp(int32_t id, float cost) : _id(id), _cost(cost) {
}
};
struct Entry {
Cmp _cmp;
string str = "some variable";
Entry(int32_t id, float cost) : _cmp(id, cost) {}
Entry(Entry &&e) : _cmp(e._cmp._id, e._cmp._cost) {}
Entry(const Entry &e) : _cmp(e._cmp._id, e._cmp._cost) {}
};
template<class T>
struct gt_entry: public binary_function <T, T, bool>
{
bool operator()(const T &l, const T &r) const
{
return *(int64_t const *)&l > *(int64_t const *)&r;
}
};
typedef boost::heap::d_ary_heap<
Entry,
boost::heap::arity<2>,
boost::heap::compare<gt_entry<Entry> > > DHeap;
typedef boost::heap::binomial_heap<
Entry,
boost::heap::compare<gt_entry<Entry> > > BHeap;
typedef boost::heap::fibonacci_heap<
Entry,
boost::heap::compare<gt_entry<Entry> > > FHeap;
typedef boost::heap::priority_queue<
Entry,
boost::heap::compare<gt_entry<Entry> > > PQueue;
int main() {
//DHeap h; // Doesn't compile
//PQueue h; // Doesn't compile
//BHeap h; // Works but slower than FHeap
FHeap h; // Works but only 3% performance increase vs std::set
h.push(Entry(1, 500.1));
h.top();
h.pop();
return 0;
}
(I am using the packaging of the _cost and _id to speed up comparison, see C++ Optimize if/else condition if you are interested.)
This seems to be the relevant error line, I guess it has something to do with the move or copy constructor.
.../move.h:177:7: error: use of deleted function ‘Entry& Entry::operator=(const Entry&)’
heaps.cpp:19:8: note: ‘Entry& Entry::operator=(const Entry&)’ is implicitly declared as deleted because ‘Entry’ declares a move constructor or move assignment operator
I am using gcc 4.6 (-std=c++0x) and boost 1.50.
Your gcc version does not implement the rules for implicitly deleted functions correctly. The code works at least with gcc 4.7.
A quick workaround is to declare the move assignment operator Entry& operator=(Entry&&) as well.
In general I wouldn't recommend using C++11 with a compiler that is not completely up-to-date.
Also: You move constructor and copy constructor behave odd. They don't copy/move the string. You might want to change that. If you really only need one string across, make it a static member.
I am trying to implement the a map from the C++ STL as follows:
#include <string>
#include <iostream>
#include <map>
using namespace std;
#include "assembler.h"
// This Class makes use of the Map Template from the Standart Template Library
// All addresses are stored as numerical (Dec) integers
SymbolTable::SymbolTable() { // Constructor
map <string, int> symbolTable;
int address = 0;
}
void SymbolTable::addEntry(string symbol, int address) {
symbolTable[symbol] = address;
address++;
}
// Returns true if symbolTable already contains symbol
bool SymbolTable::contains(string symbol) {
if (symbolTable.find(symbol) == symbolTable.end()) { return true; }
else { return false; }
}
int SymbolTable::getAddress(string symbol) {
return symbolTable[symbol];
}
I try to compile this with
c++ *.cpp -0 assembler.out
and I get the following error message:
symboltable.cpp:57:9: error: no viable conversion from 'mapped_type' (aka 'std::basic_string<char>') to 'int'
return symbolTable[symbol];
^~~~~~~~~~~~~~~~~~~
1 error generated.
I have searched for this error online and all I get is bug reports relating to the STL and I cannot figure out if those reports are the same problem I am having and if so how to get around it. Am I doing something wrong?
I have tried (probably stupidly) to typecast the offending line as
return (int) symbolTable[symbol];
Thank you for any help.
My header file declares the class as:
class SymbolTable {
public:
SymbolTable();
void addEntry(string, int);
bool contains(string);
int getAddress(string);
private:
map <string, string> symbolTable;
int address;
};
This:
SymbolTable::SymbolTable() { // Constructor
map <string, int> symbolTable;
^
^
is a function-local variable, not a member variable. It is not the same as the symbolTable that you're accessing in e.g. getAddress, which presumably is a member variable. You haven't shown the class body, but my guess is that it's defined differently.
class X_class{
public:
struct extra
{int extra1;
int extra2;
int extra3;
};
enum a
{
n,m};
struct x_struct{
char b;
char c;
int d;
int e;
std::map <int, extra> myExtraMap;
};
};
in my code I define :
x_struct myStruct;
why do I get compile errors compiling the above class? The error either says:
1) expected ; before < on the line --- where I defined the map (above) if I eliminate std::
or
2) error: invalid use of ::; error: expected ; before < token
Probably you get erorrs because you didn't #include <map>