I have
template <typename ConcContainer>
class WebBrowsingPolicyData
{
public:
typedef ConcContainer<std::shared_ptr<WBRuleDetails>>::iterator iterator;
...
private:
ConcContainer<std::shared_ptr<WBRuleDetails>> usersData_;
CRITICAL_SECTION critSection
I get a compile error at line (Error 6 error C2238: unexpected token(s) preceding ';')
typedef ConcContainer<std::shared_ptr<WBRuleDetails>>::iterator iterator
How can I make a typedef inside the template ? I must be missing something..
ConContainer is itself a template so it needs to be a template template parameter:
template <template <typename T> class ConcContainer>
class WebBrowsingPolicyData
{
public:
typedef typename ConcContainer<std::shared_ptr<WBRuleDetails>>::iterator iterator;
};
Two possibilities:
The compiler is having trouble with >>. Insert a space. Note that if you're using a C++11-conformant compiler, this should not be an issue.
example:
typedef ConcContainer<std::shared_ptr<WBRuleDetails> >::iterator iterator;
ConcContainer doesn't have a member or typedef iterator. Check to make sure that it really does.
EDIT: This is not the most vexing parse.
Related
I have a class that uses unordered_map container and custom templates.
template <typename KeyType, typename ValueType>
class List {
}
In that class I have defined a new unordered_map::const_iterator with typedef as follow:
typename typedef std::unordered_map<KeyType, ValueType>::const_iterator ListIterator;
Note that I have created that type to use it as a function parameter as follow:
void Remove(List<KeyType, ValueType>::ListIterator from, List<KeyType, ValueType>::ListIterator to) {}
The problem is that there is an error occurs when compiling Error C2061 syntax error: identifier 'ListIterator'.
Why That error occurs, what's wrong in my typedef?
You have the order of typename and typedef reversed. Change the declaration to
typedef typename std::unordered_map<KeyType, ValueType>::const_iterator ListIterator;
Or apply using (since C++11) which looks more clear.
using ListIterator = typename std::unordered_map<KeyType, ValueType>::const_iterator;
Why do I keep on getting the following error in this code in Visual C++ 2010, and how do I fix it while maintaining the type inference capability for the member variable?
error C2825: 'Foo<T>::value_type': must be a class or namespace when followed by '::'
template<class T>
struct Foo
{
typedef typename T::value_type value_type;
template<class M>
void foo(M value_type::*member) const; // error
};
struct S { typedef int value_type; };
int main() { Foo<S> s; }
The template parameter T turns out to be type S, therefore value_type turns out to be int (the nested-type in S). So how can you write value_type::*member? Note that it turns out to be int::*member which doesn't make sense. int is not a class type.
I think you meant T::*member instead of value_type::*member.
value_type is not a member of structure S.
Its just a typedef so you cant access it as you are doing.
I have a problem with the following code:
template <typename U>
class lamePtr
{
public:
typedef U* ptr;
};
template <typename U>
class smarterPointer
{
public:
void funFun()
{
typedef lamePtr<U> someType;
someType::ptr query;
}
};
As you see, I have a typedef inside lamePtr. Inside smarterPointer class I have a function funFun(). What am I trying to do is to make another typedef someType. Till that line, everything works fine until we get to the line with someType::ptr query.
What I want here to happen is that "query" will become lamePtr< U >::ptr (a simple value, not a typedef ;). However, I get compilation errors (with gcc 4.4.3):
temp.cpp: In member function ‘void smarterPointer<U>::funFun()’:
temp.cpp:15: error: expected ‘;’ before ‘query’
What am I doing wrong here?
someType, as lamePtr<U> is a "dependant name". It depends on what U is as to whether or not there is a member ptr and, if so, what kind of "thing" that member is.
Of course, you know that for all T, lamePtr<T>::ptr is a type, but at this stage of compilation the parser does not know that.
Use the typename keyword to hint to the parser that it's a type. The rest will be resolved later in the compilation process. Just a little C++ quirk.
template <typename U>
class lamePtr
{
public:
typedef U* ptr;
};
template <typename U>
class smarterPointer
{
public:
void funFun()
{
typedef lamePtr<U> someType;
typename someType::ptr query;
}
};
You need the typename keyword to signify that someType::ptr is a type.
typename someType::ptr query;
See Officially, what is typename for? for detail.
I am working through some C++ code from "Financial Instrument Pricing Using C++" - a book on option pricing using C++. Following code is a small snippet stripped of many details which basically attempts to define a SimplePropertySet class that is intended to contain a name and list.
#include <iostream>
#include <list>
using namespace::std;
template <class N, class V> class SimplePropertySet
{
private:
N name; // The name of the set
list<V> sl;
public:
typedef typename list<V>::iterator iterator;
typedef typename list<V>::const_iterator const_iterator;
SimplePropertySet(); // Default constructor
virtual ~SimplePropertySet(); // Destructor
iterator Begin(); // Return iterator at begin of composite
const_iterator Begin() const;// Return const iterator at begin of composite
};
template <class N, class V>
SimplePropertySet<N,V>::SimplePropertySet()
{ //Default Constructor
}
template <class N, class V>
SimplePropertySet<N,V>::~SimplePropertySet()
{ // Destructor
}
// Iterator functions
template <class N, class V>
SimplePropertySet<N,V>::iterator SimplePropertySet<N,V>::Begin()//<--this line gives error
{ // Return iterator at begin of composite
return sl.begin();
}
int main(){
return(0);//Just a dummy line to see if the code would compile
}
On compiling this code on VS2008, I obtain the following errors:
warning C4346: 'SimplePropertySet::iterator' : dependent name is not a type
prefix with 'typename' to indicate a type
error C2143: syntax error : missing ';' before 'SimplePropertySet::Begin'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Is there something stupid or basic that I am getting wrong or forgetting here? Is it a syntax error? I am unable to put my finger on it. The book from which this code snippet is taken says their code was compiled on Visual Studio 6. Is this some version-related issue?
Thanks.
As indicated by the compiler, you must replace :
template <class N, class V>
SimplePropertySet<N,V>::iterator SimplePropertySet<N,V>::Begin()
with :
template <class N, class V>
typename SimplePropertySet<N,V>::iterator SimplePropertySet<N,V>::Begin()
See this link for an explanation on dependent names.
template <typename vec1, typename vec2>
class fakevector
{
public:
/* Do something */
};
template <class A>
class caller
{
public:
struct typeList
{
struct typeOne
{
//...
};
};
typedef fakevector<typeList::typeOne,int> __methodList; /* This will trigger compile error */
};
The error messages I got are:
Error: type/value mismatch at
argument 1 in template parameter
list for ‘template class fakevector’
Error: expected a type, got ‘caller::typeList::typeOne’
If template is removed from the caller class, no error will be reported,
like this
class caller
{
public:
struct typeList
{ ....
};
I don't know the reason. Thank you very much!
Try:
typedef fakevector<typename typeList::typeOne,int> __methodList;
http://www.comeaucomputing.com/techtalk/templates/#typename
Looks like the compiler is in doubt what typeOne is.
typedef fakevector<typename typeList::typeOne,int>
should compile
Try typedef fakevector<typename typeList::typeOne,int>
The typename prefix to a name is required when the name
Appears in a template
Is qualified
Is not used as in a list of base class specifications or in a list of member initializers introducing a constructor definition
Is dependent on a template parameter
Furthermore, the typename prefix is not allowed unless at least the first three previous conditions hold.