symbol cannot be used in a using-declaration - c++

I've got a header in which my base problem is with the using keyword.
#ifndef SHAPEFACTORY_H__
#define SHAPEFACTORY_H__
#include <istream>
#include <map>
#include <string>
#include "shape.h"
/* thrown when a shape cannot be read from a stream */
template<class T>
class WrongFormatException { };
template<class T>
class ShapeFactory
{
public:
using createShapeFunction=Shape<T>*()(void);
static void registerFunction(const std::string &, const createShapeFunction *);
static Shape<T> *createShape(const std::string &);
static Shape<T> *createShape(std::istream &);
private:
std::map<std::string, createShapeFunction *> creationFunctions;
ShapeFactory();
static ShapeFactory<T> *getShapeFactory();
};
#endif
And I've got some errors which I can't resolve.
1>shapefactory.h(21): error C2873: 'createShapeFunction' : symbol cannot be used in a using-declaration
1>shapefactory.h(29) : see reference to class template instantiation 'ShapeFactory<T>' being compiled
1>shapefactory.h(21): error C2143: syntax error : missing ';' before '='
1>shapefactory.h(21): error C2238: unexpected token(s) preceding ';'
1>shapefactory.h(22): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>shapefactory.h(22): error C2143: syntax error : missing ',' before '*'
1>shapefactory.h(26): error C2065: 'createShapeFunction' : undeclared identifier
1>shapefactory.h(26): error C2059: syntax error : '>'
1>shapefactory.h(29): error C2143: syntax error : missing ';' before '}'
1>shapefactory.h(29): fatal error C1004: unexpected end-of-file found
Any idea would be great.

It seems that the compiler does not support the alias decladation. Substitute it for a typedef declaration. For example (at least the code is compiled)
#include <map>
#include <string>
template <typename T>
class Shape;
template<class T>
class ShapeFactory
{
public:
typedef Shape<T>* createShapeFunction(void);
static void registerFunction(const std::string &, const createShapeFunction *);
static Shape<T> *createShape(const std::string &);
static Shape<T> *createShape(std::istream &);
private:
std::map<std::string, createShapeFunction *> creationFunctions;
ShapeFactory();
static ShapeFactory<T> *getShapeFactory();
};
int main()
{
return 0;
}

Related

When adding the header file in the .cpp file, errors arise

The Nodes.h file alone when compiled works, however when I include it in the Nodes.cpp file, all the errors arise, such as missing type specifier - int assumed at line 11,12,13 . Also another error is syntax error: identifier 'ASTSimpleExpressionNode'. Is it something that I am doing wrong. Can't I specify how my struct can be constructed by defining the different constructors?
For now the ASTSimpleExpressionNode is empty because if I continued the process it would duplicate all the errors.
Some of the errors:
Error C2535 'ASTExpressionNode::ASTExpressionNode(void)': member
function already defined or declared [line 16] Error C4430 missing
type specifier - int assumed. Note: C++ does not support
default-int [Line 11, 12,13 so on.] Error C2143 syntax error: missing
';' before '*' [Line 11,12,13] unexpected token(s) preceding ';' [Line
11,12,13]
Nodes.h file.
#pragma once
#include <string>
using namespace std;
struct ASTNode
{
};
struct ASTExpressionNode : ASTNode
{
ASTSimpleExpressionNode *left;
ASTRelationOperatorNode *rel_op;
ASTSimpleExpressionNode *right;
ASTExpressionNode(ASTSimpleExpressionNode *l);
ASTExpressionNode(ASTSimpleExpressionNode *l, ASTRelationOperatorNode *op, ASTSimpleExpressionNode *r);
};
struct ASTSimpleExpressionNode : ASTExpressionNode
{
};
struct ASTRelationOperatorNode :ASTExpressionNode
{
string rel_op;
ASTRelationOperatorNode(string op);
};
Nodes.cpp file.
#include "Nodes.h"
Forward-declare ASTSimpleExpressionNode and ASTRelationOperatorNode:
struct ASTSimpleExpressionNode;
struct ASTRelationOperatorNode;
struct ASTExpressionNode : ASTNode
{
// etc

C++ error C2143 syntax error : missing ';' before function name

I have header file :
#ifndef VIP_TICKET_H
#define VIP_TICKET_H
#include "ticket.h"
class VIPTicket : public Ticket
{
public:
enum VIPType { FIRST_CLASS, FAST_LINE };
VIPType getTicketType() const;
private:
VIPType type;
};
#endif
and it's cpp file
#include "vipTicket.h"
VIPType VIPTicket::getTicketType() const
{
return type;
}
the error says " error C2143: syntax error : missing ';' before 'VIPTicket::getTicketType' "
this error is very confusing.. i guess it's not a ';' that is missing but probably something else wrong with the code that I can't put my finger on..
The problem is this definition
VIPType VIPTicket::getTicketType() const
{
...
}
When you define this function you have to remember that VIPType is not in the global scope, but in the scope of the VIPTicket class, so you have to explicitly mention the scope:
VIPTicket::VIPType VIPTicket::getTicketType() const
{
...
}

C++ Linked Lists Template Class

I'm currently practicing some linked list assignments out of my book right now and I'm stuck compiling errors. The implementation file seems to be fine, however the header file is what is receiving the errors.
Here is my header file:
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <string>
using namespace std;
template<class T>
class linkedList {
public:
linkedList();
linkedList(const linkedList& copy);
~linkedList();
int getSize() const;
void addEntry(T entry);
bool deleteEntry(T entry);
T getEntry(int input) const;
linkedList operator=(const linkedList& right);
private:
struct node {
T data;
node<T> *next;
};
node<T> *linkedList = NULL;
};
#include "linkedlist.cpp"
#endif // LINKEDLIST_H
The errors I'm getting from my compiler is as follows:
c:\users\andym_000\documents\linkedlist\linkedlist.h(22) : error C2059: syntax error : '<'
c:\users\andym_000\documents\linkedlist\linkedlist.h(20) : see reference to class template instantiation 'linkedList<T>::node' being compiled
c:\users\andym_000\documents\linkedlist\linkedlist.h(25) : see reference to class template instantiation 'linkedList<T>' being compiled
c:\users\andym_000\documents\linkedlist\linkedlist.h(22) : error C2238: unexpected token(s) preceding ';'
c:\users\andym_000\documents\linkedlist\linkedlist.h(24) : error C2059: syntax error : '<'
c:\users\andym_000\documents\linkedlist\linkedlist.h(24) : error C2238: unexpected token(s) preceding ';'
c:\users\andym_000\documents\linkedlist\linkedList.h(24) : error C2059: syntax error : '<'
..\linkedList\main.cpp(9) : see reference to class template instantiation 'linkedList<T>' being compiled
with
[
T=std::string
]
c:\users\andym_000\documents\linkedlist\linkedList.h(24) : error C2238: unexpected token(s) preceding ';'
c:\users\andym_000\documents\linkedlist\linkedList.h(24) : error C2059: syntax error : '<'
..\linkedList\main.cpp(10) : see reference to class template instantiation 'linkedList<T>' being compiled
with
[
T=int
]
c:\users\andym_000\documents\linkedlist\linkedList.h(24) : error C2238: unexpected token(s) preceding ';'
Um, struct node {...} isn't a template. Your errors are telling you that is has no idea what to do with node<T>.
You use template notation with node class, which s not a template. T type is defined for whole linkedList - in definiition of node it is well defined type.
Note that node is defined inside linkedList, so it's qualified name will be linkedList<T>::node for linkedList<T> (i.e. each instantiation of linkedList has it's own node class).
So, just replace node<T> with node and that should work.

C++ static const template member initialization

I'm having some strange issues trying to initialize a static const member variable of a template class. All of my other static variables initialize fine but for some reason it doesn't like this one. I put together some sample code to test and it doesn't have the problem so I really don't know what's going on.
On top of this I'm also having issues defining functions that use typedefs declared inside of the template class with the same issue saying it can't find the type. This problem I have been able to reproduce though in the code below. I know one way to fix it is to define the function inside of the class, but the function is really large and I'm trying to keep it consistent with having all of the huge functions defined outside of the class to make the class definition easier to read. If that's my only option though then I guess I'll have to make an exception...
class tTestType
{
public:
tTestType(int32_t val) : fValue(val) { }
private:
int32_t fValue;
};
template<class T>
class tTestTemplate
{
public:
tTestTemplate() { }
private:
typedef std::vector<int32_t> tSomeVec;
tSomeVec mTestFunction() const;
static const tTestType kTestStatic;
};
// Should cause the following errors but I can't reproduce them for some reason:
// error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
// error C2988: unrecognizable template declaration/definition
// error C2059: syntax error : 'constant'
template<class T>
const tTestType tTestTemplate<T>::kTestStatic(10);
// Causes the following errors:
// error C2143: syntax error : missing ';' before 'tTestTemplate<T>::mTestFunction'
// error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
// fatal error C1903: unable to recover from previous error(s); stopping compilation
template<class T>
tTestTemplate<T>::tSomeVec tTestTemplate<T>::mTestFunction() const
{
tSomeVec result;
result.push_back(0);
return result;
}
Thanks to a coworker I've figured out a solution to both issues.
For the first issue, the static member variable, I've moved the definition to the CPP file and used a template specialization. The reason this doesn't break in the test code I posted is because the basic type (int, float, etc.) handle the problem, but if you use a more complex type, like a class, then it should cause the error. This solution isn't the best thing in the world to do, I know, but it's the only thing that works that is somewhat clean. If someone has a better solution please let me know:
template<>
const tTestType tTestTemplate<uint32_t>::kTestStatic(10);
For the second issue, the function using a type declared inside the class, I went with the solution I described in the initial post and just moved the function definition inside of the template class so now it looks like this:
template<class T>
class tTestTemplate
{
public:
tTestTemplate() { }
private:
typedef std::vector<int32_t> tSomeVec;
// Declaring the function inside the class to fix the compiler error.
tSomeVec mTestFunction() const
{
tSomeVec result;
result.push_back(0);
return result;
}
static const tTestType kTestStatic;
};
I made 2 changes in your code and its compiling fine.
class tTestType
{
public:
tTestType(int32_t val) : fValue(val) { }
private:
int32_t fValue;
};
typedef std::vector<int32_t> tSomeVec;
template<class T>
class tTestTemplate
{
public:
tTestTemplate() { }
private:
tSomeVec mTestFunction() const;
static const tTestType kTestStatic;
};
// Should cause the following errors but I can't reproduce them for some reason:
// error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
// error C2988: unrecognizable template declaration/definition
// error C2059: syntax error : 'constant'
template<class T>
const tTestType tTestTemplate<T>::kTestStatic(10);
// Causes the following errors:
// error C2143: syntax error : missing ';' before 'tTestTemplate<T>::mTestFunction'
// error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
// fatal error C1903: unable to recover from previous error(s); stopping compilation
template<class T>
tSomeVec tTestTemplate<T>::mTestFunction() const
{
tSomeVec result;
result.push_back(0);
return result;
}

Trying to compile a .h file without understanding something

I'm trying to compile Opengazer (Open source gaze tracker) code with visual studio on windows, while the code was originally written for linux and should be compile with cmake.
Anyway, I can't compile few files.
The code won't compile is this:
Containers.h:
#pragma once
#define xforeachactive(iter,container) \
for(typeof(container.begin()) iter = container.begin(); \
iter != container.end(); iter++) \
if ((*iter)->parent == this)
template <class ParentType, class ChildType> class Container;
template <class ParentType, class ChildType>
class Containee {
protected:
void detach() { parent = 0; }
public:
ParentType *parent; /* set to null to request removal */
Containee(): parent(0) {}
virtual ~Containee() {}
};
template <class ParentType, class ChildType>
class Container {
typedef ChildType *ChildPtr;
static bool isFinished(const ChildPtr &object) {
return !(object && object->parent);
}
protected:
std::vector<ChildPtr> objects;
void removeFinished() {
objects.erase(remove_if(objects.begin(), objects.end(), isFinished),
objects.end());
}
public:
void clear() {
xforeachactive(iter, objects)
(*iter)->parent = 0;
removeFinished();
}
static void addchild(ParentType *parent, const ChildPtr &child) {
parent->objects.push_back(child);
child->parent = parent;
parent->removeFinished();
}
virtual ~Container() {
clear();
}
};
template <class ParentPtr, class ChildPtr>
class ProcessContainer: public Container<ParentPtr, ChildPtr> {
public:
virtual void process() {
xforeachactive(iter, this->objects)
(*iter)->process();
this->removeFinished();
}
virtual ~ProcessContainer() {};
};
btw Containers.cpp is empty
ad the code uses the above class is:
#pragma once
class FrameProcessing;
class FrameFunction:
public Containee<FrameProcessing, FrameFunction>
{
const int &frameno;
int startframe;
protected:
int getFrame() { return frameno - startframe; }
public:
FrameFunction(const int &frameno): frameno(frameno), startframe(frameno) {}
virtual void process()=0;
virtual ~FrameFunction();
};
class FrameProcessing:
public ProcessContainer<FrameProcessing,FrameFunction> {};
class MovingTarget: public FrameFunction {
WindowPointer *pointer;
public:
MovingTarget(const int &frameno,
const vector<Point>& points,
WindowPointer *&pointer,
int dwelltime=20);
virtual ~MovingTarget();
virtual void process();
protected:
vector<Point> points;
const int dwelltime;
int getPointNo();
int getPointFrame();
bool active();
};
class CalibrationHandler
{
public:
CalibrationHandler(void);
~CalibrationHandler(void);
};
the error I get is :
visual studio 2008\projects\eyemouse\eyemouse\containers.h(58) : error C2146: syntax error : missing ';' before identifier 'iter'
visual studio 2008\projects\eyemouse\eyemouse\containers.h(58) : error C2065: 'iter' : undeclared identifier
visual studio 2008\projects\eyemouse\eyemouse\containers.h(58) : error C2065: 'iter' : undeclared identifier
visual studio 2008\projects\eyemouse\eyemouse\containers.h(58) : error C2146: syntax error : missing ')' before identifier 'iter'
visual studio 2008\projects\eyemouse\eyemouse\containers.h(58) : error C2059: syntax error : ';'
visual studio 2008\projects\eyemouse\eyemouse\containers.h(58) : error C2065: 'iter' : undeclared identifier
visual studio 2008\projects\eyemouse\eyemouse\containers.h(58) : error C2059: syntax error : ')'
visual studio 2008\projects\eyemouse\eyemouse\containers.h(58) : error C2143: syntax error : missing ';' before 'if'
visual studio 2008\projects\eyemouse\eyemouse\containers.h(58) : error C2065: 'iter' : undeclared identifier
visual studio 2008\projects\eyemouse\eyemouse\containers.h(58) : error C2227: left of '->parent' must point to class/struct/union/generic type
type is ''unknown-type''
visual studio 2008\projects\eyemouse\eyemouse\containers.h(59) : error C2065: 'iter' : undeclared identifier
visual studio 2008\projects\eyemouse\eyemouse\containers.h(59) : error C2227: left of '->process' must point to class/struct/union/generic type
type is ''unknown-type''
I understand why I'm getting an error.
'iter' is not defined anywhere. anyway, this isnt my code and it should work.
I tried to copy and past the define part to the function, but still get the same error.
I'm stuck with this and trying to solve it for hours, but can't understand what to do to make it work.
I'll really be grateful for any help.
typeof is a gcc extension and equivalent to C++0x decltype there is no VS version that actually supports it.
You would need to use C++0x and decltype or try to use Boost.TypeOf, which comes with its own caveats.
Change the macro to this:
#include <boost/typeof/typeof.hpp>
#define xforeachactive(iter,container) \
for(BOOST_TYPEOF(container.begin()) iter = container.begin(); \
iter != container.end(); iter++) \
if ((*iter)->parent == this)
You could also use BOOST_AUTO if you think this is clearer.