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
}
Related
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
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
{
...
}
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.
I am pretty new to the concept of templates. Am I missing something?
#ifndef STACK_H
#define STACK_H
template < class T>
class Stack{
private:
struct node {
T data;
node* next;
};
node * top;
//node* getNewNode(T num);
};
//template <class T>
//(node*)Stack<T> :: getNewNode(T num){
// node * temp = new node;
// temp->data = num;
// temp->next = NULL;
// return temp;
// }
#endif
When I uncomment the function getNewNode and commend the corresponding statement to getNewNode function, complier gives error like
Why is my function getNewNode not working as expected. Where did I go wrong?
Error 7 error C2470: 'Stack<T>::getNewNode' : looks like a function
definition, but there is no parameter list; skipping apparent
body c:\users\nitinjose\documents\visual studio
2010\projects\templatequeue\templatequeue\stack.h 26 1 TemplateQueue
Error 2 error C4430: missing type specifier - int assumed. Note: C++
does not support default-int c:\users\nitinjose\documents\visual
studio
2010\projects\templatequeue\templatequeue\stack.h 26 1 TemplateQueue
Error 5 error C2146: syntax error : missing ')' before identifier
'num' c:\users\nitinjose\documents\visual studio
2010\projects\templatequeue\templatequeue\stack.h 26 1 TemplateQueue
Defining a member function outside of a class body changes the rules for what names are accessible at that point slightly. In your case the compiler has no idea what node is. You need to tell him that the node* is actually in the class Stack<T>, e.g. typename Stack<T>::node. The typename is necessary here, because node is a dependent name.
The specification of the return value is wrong. Try this
template <class T>
typename Stack<T>::node* Stack<T> :: getNewNode(T num){
// ...
}
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;
}