I have the following code :
class v8NMatch : public V8Wrap<Match, v8NMatch>
{
public:
static v8::Persistent<v8::FunctionTemplate> s_ct;
static void Init(v8::Handle<v8::Object> target);
static v8::Handle<v8::Value> IsSuccess(Arguments& args);
v8NMatch(const v8::Arguments& args);
};
It tells me I have a syntax error at the identifier Arguments.
If I put the const keyword before Arguments, I have 2 errors :
missing type specifier - int assumed. Note: C++ does not support default-int
syntax error : missing ',' before '&'
Any clue ?
IsSuccess is defined without the v8::Arguments.
Related
Im having a compilation error and i cant figure why.
I have enum declaration on the .h file and the .cpp file suppose to use it inside stringToEnum() function
this is the .cpp file
#include "A.h"
A::A(void)
{
}
A::~A(void)
{
}
values A::stringToEnum (string inputString) {
if (inputString == "string1") return val1;
if (inputString == "string2") return val2;
}
this is the header file
class A
{
public:
A(void);
~A(void);
private:
enum values{
val1,
val2,
val3,
val4
};
values stringToEnum (string inputString);
};
this is the error I get:
1>c:\users\documents\visual studio 2010\projects\A.cpp(25): error C2143: syntax error : missing ';' before 'A::stringToEnum'
1>c:\users\documents\visual studio 2010\projects\A.cpp(25): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\documents\visual studio 2010\projects\A.cpp(25): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\documents\visual studio 2010\projects\A.cpp(25): error C2556: 'int A::stringToEnum(std::string)' : overloaded function differs only by return type from 'A::values A::stringToEnum(std::string)'
1> c:\users\documents\visual studio 2010\projects\A.h(22) : see declaration of 'A::stringToEnum'
1>c:\users\documents\visual studio 2010\projects\A.cpp(25): error C2371: 'A::stringToEnum' : redefinition; different basic types
1> c:\users\documents\visual studio 2010\projects\A.h(22) : see declaration of 'A::stringToEnum'
I will be happy for some guidance.
thanks
Since values is contained in A, you need to qualify the name:
A::values A::stringToEnum (string inputString) {
//...
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;
}
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;
}
The compiler is giving me the following complaints about the following class:
class AguiWidgetBase
{
//variables
AguiColor tintColor;
AguiColor fontColor;
//private methods
void zeroMemory();
virtual void onPaint();
virtual void onTintColorChanged(AguiColor color);
void (*onPaintCallback)(AguiRectangle clientRect);
void (*onTintColorChangedCallback)();
public:
AguiWidgetBase(void);
~AguiWidgetBase(void);
void paint();
void setTintColor(AguiColor color);
AguiColor getBackColor();
};
Warning 13 warning C4183: 'getBackColor': missing return type; assumed to be a member function returning 'int' c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\agui\AguiWidgetBase.h 26
Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\agui\AguiWidgetBase.h 11
Error 3 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\agui\AguiWidgetBase.h 11
Error 5 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\agui\AguiWidgetBase.h 12
Error 6 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\agui\AguiWidgetBase.h 12
Error 11 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\agui\AguiWidgetBase.h 26
Error 12 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\agui\AguiWidgetBase.h 26
Error 1 error C2146: syntax error : missing ';' before identifier 'tintColor' c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\agui\AguiWidgetBase.h 11
Error 10 error C2146: syntax error : missing ';' before identifier 'getBackColor' c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\agui\AguiWidgetBase.h 26
Error 4 error C2146: syntax error : missing ';' before identifier 'fontColor' c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\agui\AguiWidgetBase.h 12
Error 8 error C2061: syntax error : identifier 'AguiRectangle' c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\agui\AguiWidgetBase.h 17
Error 7 error C2061: syntax error : identifier 'AguiColor' c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\agui\AguiWidgetBase.h 16
Error 9 error C2061: syntax error : identifier 'AguiColor' c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\agui\AguiWidgetBase.h 25
This should be working, I'm including the headers for those classes.
This is the h file:
//integer Point class
class AguiPoint {
int x;
int y;
public:
int getX();
int getY();
void setX(int x);
void setY(int y);
void set(int x, int y);
AguiPoint(int x, int y);
AguiPoint();
std::string toString();
std::string xToString();
std::string yToString();
};
//floating version of Agui Point
class AguiPointf {
float x;
float y;
public:
float getX();
float getY();
void setX(float x);
void setY(float y);
void set(float x, float y);
AguiPointf(float x, float y);
AguiPointf(AguiPoint p);
AguiPointf();
std::string toString();
std::string xToString();
std::string yToString();
};
//Integer rectangle class
class AguiRectangle {
int x;
int y;
int width;
int height;
public:
bool isEmpty();
int getTop();
int getLeft();
int getBottom();
int getRight();
AguiPoint getTopLeft();
AguiPoint getBottomRight();
};
class AguiColor {
unsigned char r;
unsigned char g;
unsigned char b;
unsigned char a;
void verifyColorBounds();
public:
AguiColor(int r, int g, int b, int a);
AguiColor(float r, float g, float b, float a);
AguiColor();
int getR();
int getG();
int getB();
int getA();
};
Thanks
I include the main header in the WidgetBase and the main header includes the base types before it includes the widgetbase
It seems you are not including the headers in the right order.
C++ is very sensitive to the order in which identifiers are declared. The compiler will process a source file (and any #include-s it finds along the way) in a sequential order and for every (non-builtin) identifier, the compiler must have seen a declaration before you can use it.
If you already have the headers included i'd guess you need to fully qualify the names because of a namespace or that Aguiwidgitbase is in the wrong namespace? either way check the namespaces in the headers you included
Check header files and namespaces. You may also need forward declarations.
You may have a problem of include dependencies. Header guards preventing multiple definition (#pragma once, or #ifndef HEADER #define HEADER) can block your inclusion.
One answer can be forward declarations... for pointers to class, not for class members though (need to know storage size at compile time).
I have a problem with a template and pointers ( I think ). Below is the part of my code:
/* ItemCollection.h */
#ifndef ITEMCOLLECTION_H
#define ITEMCOLLECTION_H
#include <cstddef>
using namespace std;
template <class T> class ItemCollection
{
public:
// constructor
//destructor
void insertItem( const T );
private:
struct Item
{
T price;
Item* left;
Item* right;
};
Item* root;
Item* insert( T, Item* );
};
#endif
And the file with function defintion:
/* ItemCollectionTemp.h-member functions defintion */
#include <iostream>
#include <cstddef>
#include "ItemCollection.h"
template <class T>
Item* ItemCollection <T>::insert( T p, Item* ptr)
{
// function body
}
Here are the errors which are generated by this line of code:
Item* ItemCollection <T>::insert( T p, Item* ptr)
Errors:
error C2143: syntax error : missing ';' before '*'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2065: 'Type' : undeclared identifier
error C2065: 'Type' : undeclared identifier
error C2146: syntax error : missing ')' before identifier 'p'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2470: 'ItemCollection::insert' : looks like a function definition, but there is no parameter list; skipping apparent body
error C2072: 'ItemCollection::insert': initialization of a function
error C2059: syntax error : ')'
Any help is much appreciated.
The short answer is what Alexey already posted:
template <typename T>
typename ItemCollection<T>::Item* ItemCollection<T>::insert( T p, Item * ptr ) {
// ...
}
(To understand why typename is required, search SO for related questions, or else drop a comment. I will focus the answer in the lookup rules that explain why the return and the argument types must be declared differently)
The explanation is that the lookup rules in c++ have different scopes for the return type and the rest of the parameters. When the compiler sees the definition A B::c( D ), A is checked in the enclosing namespace of the definition, as is B. When the compiler finds ::c it looks up c inside class B. At that point, the rest of the definition is inside the scope of the class B for the rest of parameters. That means that if the return type is an internal type of the class, you have to use the qualified name for the return type, while in the case of D the compiler will first look it up inside the class B.
That explains why the return type must be fully qualified even if the last parameter is not. When the parameter Item * ptr is found by the compiler, it is already in the scope of the class, and it will find it there. On the other hand, there is no Item defined in the enclosing namespace.
One of the changes in the upcomming standard will deal with this, even if it was not designed with this purpose in mind. If I recall correctly, the following syntax should compile without the full type qualification:
template <T>
auto ItemCollection<T>::insert( T p, Item * ptr ) -> Item *
{
return 0;
}
The reason is exactly the same. After ItemCollection<T>::insert has been parsed the remainder tokens will be verified inside the ItemCollection<T> scope, including the -> Item * return definition.
template <class T>
typename ItemCollection <T>::Item* ItemCollection<T>::insert( T p, Item* ptr)
{
// function body
}