I have a template class that is:
template <class identifier,class registeredObject>
class FxPairRegistry : public FxRegistry<pair<identifier,registeredObject> >
{
public:
registeredObject GetEntry(identifier, FxBool assertValue = true);
void RegisterInOrder(const pair<identifier,registeredObject> &ob);
typedef typename std::vector<pair<identifier,registeredObject> >::iterator iter;
};
I then have:
template <class identifier,class registeredObject>
registeredObject FxPairRegistry<identifier,registeredObject>::GetEntry(identifier id, FxBool
assertValue)
{
for (iterator iter = mRegistryList.begin(); iter != mRegistryList.end(); iter++)
{
if ((*iter).first == id)
{
return (*iter).second;
}
}
}
But I get errors like:
error: missing template arguments before 'iter'
error: expected `;' before 'iter'
error: expected primary-expression before '!=' token
error: 'mRegistryList' was not declared in this scope
error: expected primary-expression before '++' token
error: expected primary-expression before ')' token
error: expected primary-expression before ')' token
error: missing template arguments before 'iter'
error: expected `;' before 'iter'
error: expected primary-expression before '!=' token
error: 'mRegistryList' was not declared in this scope
error: expected primary-expression before '++' token
I dont quite get what I am doing wrong, but I am rusty a bit for sure....
It seems that here, you are typedef'ing a type called iter:
typedef typename std::vector<pair<identifier,registeredObject> >::iterator iter;
And then here:
iterator iter = mRegistryList.begin()
You are trying to define a variable called iter of type iterator, which (from the code you've shown) is not the name of any type that exists.
Check your for loop. What is iterator iter = ... supposed to do? You typedef'ed the iterators as iter so iter becomes the name of the type you want to declare.
Related
#include <iostream>
#include <fstream>
#include <map>
#include <set>
#include <string>
using namespace std;
int main(int argc, const char *argv[]) {
ifstream inflie(argv[1]);
int currentLine = 1;
vector<string> words;
map<vector<string>, int> freq;
string line;
while (getline(infile, line)) {
istringstream inputString(line);
currentLine++
string word;
while(inputString >> word){
words.push_back(pair(word, currentLine));
freq[words]++;
}
map<vector<string>, int>::const_iterator iter;
for (iter=freq.begin(); iter != freq.end(); ++iter) {
cout << iter->second << " " << iter->first << endl;
}
return 0;
}
this is what I have so far. Its giving me multiple errors when I try to compile. please keep in mind that last c++ stuff I did was in highschool nearly two years ago. I really have absolutely no idea what I'm doing.
Here are my errors (this is really sad):
tabulate.cc: In function `int main(int, const char**)':
tabulate.cc:11: error: `vector' was not declared in this scope
tabulate.cc:11: error: expected primary-expression before '>' token
tabulate.cc:11: error: `words' was not declared in this scope
tabulate.cc:12: error: `vector' cannot appear in a constant-expression
tabulate.cc:12: error: wrong number of template arguments (1, should be 4)
/usr/local/lib/gcc/sparc-sun-solaris2.9/3.4.6/../../../../include/c++/3.4.6/bits/stl_map.h:92: error: provided for `template<class _Key, class _Tp, class _Compare, class _Alloc> class std::map'
tabulate.cc:12: error: expected unqualified-id before ',' token
tabulate.cc:12: error: expected unqualified-id before "int"
tabulate.cc:14: error: `infile' was not declared in this scope
tabulate.cc:15: error: variable `std::istringstream inputString' has initializer but incomplete type
tabulate.cc:17: error: expected `;' before "string"
tabulate.cc:18: error: `word' was not declared in this scope
tabulate.cc:19: error: missing template arguments before '(' token
tabulate.cc:20: error: `freq' was not declared in this scope
tabulate.cc:20: warning: unused variable 'freq'
tabulate.cc:22: error: `vector' cannot appear in a constant-expression
tabulate.cc:22: error: wrong number of template arguments (1, should be 4)
/usr/local/lib/gcc/sparc-sun-solaris2.9/3.4.6/../../../../include/c++/3.4.6/bits/stl_map.h:92: error: provided for `template<class _Key, class _Tp, class _Compare, class _Alloc> class std::map'
tabulate.cc:22: error: expected unqualified-id before ',' token
tabulate.cc:22: error: expected unqualified-id before "int"
tabulate.cc:23: error: `iter' was not declared in this scope
tabulate.cc:23: error: `freq' was not declared in this scope
tabulate.cc:14: warning: unused variable 'infile'
tabulate.cc:27: error: expected `}' at end of input
I want to write something to share memory,
pAttr is the share memory address.
The template function as below,
but it does not pass the compile.
template <typename Container>
int ShareMemMgn::writeContainerToShareMemMap(void* pAttr, Container& oData)
{
typename Container::mapped_type T;
(T*)(pElem) = (T *)(pAttr); //compile errror
/*
share_mem_mgn.cpp:545: error: expected primary-expression before ‘)’ token
share_mem_mgn.cpp:545: error: ‘pElem’ was not declared in this scope
share_mem_mgn.cpp:545: error: expected primary-expression before ‘)’ token
*/
for(typename Container::iterator it = oData.begin();
it != oData.end(); ++it)
{
memcpy(pElem, (&(it->second)), sizeof(typename Container::mapped_type));
++pElem;
}
return 0;
}
How to get the maped type pointer?
Could anyone help me?
Thanks a lot.
As your code reads right now, T is a variable, not a type. Presumably you meant this:
typedef typename Container::mapped_type T;
T * pElem = static_cast<T *>(pAttr);
You could also do this
template <typename KeyType, typename ValueType>
int ShareMemMgn::writeContainerToShareMemMap(void* pAttr, std::map<KeyType,ValueType>& oData)
If you are using only a Map.
I am trying to pass a template typedef as argument to a function template. However I get following errors:
TestTemplates.cpp:11: error: expected unqualified-id before ‘&’ token
TestTemplates.cpp:11: error: expected unqualified-id before ‘&’ token
TestTemplates.cpp:11: error: expected initializer before ‘&’ token
TestTemplates.cpp:25: error: ‘func’ was not declared in this scope
#include <iostream>
#include <vector>
template<class T>
struct MyVector
{
typedef std::vector<T> Type;
};
template<class T>
void func( const MyVector<T>::Type& myVec )
{
for( MyVector<T>::Type::const_iterator p = myVec.begin(); p != myVec.end(); p++)
{
std::cout<<*p<<"\t";
}
}
int main()
{
MyVector<int>::Type myVec;
myVec.push_back( 10 );
myVec.push_back( 20 );
func( myVec );
}
Can anyone point out how to fix this error. I have looked at some posts, but cannot find the solution. Thanks
You need to tell the compiler that it's a typename
void func( const typename MyVector<T>::Type& myVec )
Then you need to explicitly help the compiler deduce the type for the function:
func<int>( myVec );
BTW, the issue is called "two stage lookup"
When I try to compile this:
#include <map>
#include <string>
template <class T>
class ZUniquePool
{
typedef std::map< int, T* > ZObjectMap;
ZObjectMap m_objects;
public:
T * Get( int id )
{
ZObjectMap::const_iterator it = m_objects.find( id );
if( it == m_objects.end() )
{
T * p = new T;
m_objects[ id ] = p;
return p;
}
return m_objects[ id ];
}
};
int main( int argc, char * args )
{
ZUniquePool< std::string > pool;
return 0;
}
I get this:
main.cpp: In member function ‘T* ZUniquePool<T>::Get(int)’:
main.cpp:12: error: expected `;' before ‘it’
main.cpp:13: error: ‘it’ was not declared in this scope
I'm using GCC 4.2.1 on Mac OS X.
It works in VS2008.
I'm wondering whether it might be a variation of this problem:
Why doesn't this C++ template code compile?
But as my error output is only partially similar, and my code works in VS2008, I am not sure.
Can anyone shed some light on what I am doing wrong?
You need typename:
typename ZObjectMap::const_iterator it = m_objects.find( id )
Since the type of ZObjectMap is dependent on the template parameter, the compiler has no clue what ZObjectMap::const_iterator is (it could be a member variable). You need to use typename to inform the compiler to assume it will be some sort of type.
Also to mention, GCC 4.5.0 gives the following output which would help you to solve the problem:
main.cpp: In member function 'T* ZUniquePool<T>::Get(int)':
main.cpp:12:7: error: need 'typename' before 'ZUniquePool<T>::ZObjectMap:: const_iterator' because 'ZUniquePool<T>::ZObjectMap' is a dependent scope
main.cpp:12:34: error: expected ';' before 'it'
main.cpp:13:11: error: 'it' was not declared in this scope
main.cpp: At global scope:
main.cpp:23:5: warning: second argument of 'int main(int, char*)' should be 'char **'
I have a template class where I want to use objects of that class (along with the parameterized type) inside a map. So far this is the solution that I've been able to arrive at:
class IStatMsg;
template <typename T>
class ITier
{
public:
// Methods
ITier(TierType oType) : o_Type(oType){};
virtual ~ITier(){};
typename ITier<T> ParamITier; // line 60
ITier* Get(T oKey)
{
std::map<T, ParamITier*>::iterator it = map_Tiers.find(oKey); // line 64
if (it != map_Tiers.end())
return it->second;
return NULL;
}
void Set(T oKey, ITier* pTier)
{
map_Tiers.insert(pair<T, ParamITier*>(oKey, pTier)); // line 74
}
TierType GetType() { return o_Type; }
protected:
// Methods
// Attributes
std::map<T, ParamITier*> map_Tiers; // line 83
TierType o_Type;
private:
// Methods
// Attributes
};
But when I try to compile this code I get a long list of errors:
/home/gayanm/street/src/QueryServer_NEW/ITier.h:60:
error: expected nested-name-specifier
/home/gayanm/street/src/QueryServer_NEW/ITier.h:60:
error: ITier<T>' specified as
declarator-id
/home/gayanm/street/src/QueryServer_NEW/ITier.h:60:
error: perhaps you wantITier'
for a constructor
/home/gayanm/street/src/QueryServer_NEW/ITier.h:60:
error: two or more data types in
declaration of ITier<T>'
/home/gayanm/street/src/QueryServer_NEW/ITier.h:60:
error: expected;' before
"ParamITier"
/home/gayanm/street/src/QueryServer_NEW/ITier.h:83:
error: ParamITier' was not declared
in this scope
/home/gayanm/street/src/QueryServer_NEW/ITier.h:83:
error: template argument 2 is invalid
/home/gayanm/street/src/QueryServer_NEW/ITier.h:83:
error: template argument 4 is invalid
/home/gayanm/street/src/QueryServer_NEW/ITier.h:83:
error: ISO C++ forbids declaration of
map_Tiers' with no type
/home/gayanm/street/src/QueryServer_NEW/ITier.h:
In member function ITier<T>*
ITier<T>::Get(T)':
/home/gayanm/street/src/QueryServer_NEW/ITier.h:64:
error:ParamITier' undeclared (first
use this function)
/home/gayanm/street/src/QueryServer_NEW/ITier.h:64:
error: (Each undeclared identifier is
reported only once for each function
it appears in.)
/home/gayanm/street/src/QueryServer_NEW/ITier.h:64:
error: template argument 2 is invalid
/home/gayanm/street/src/QueryServer_NEW/ITier.h:64:
error: template argument 4 is invalid
/home/gayanm/street/src/QueryServer_NEW/ITier.h:64:
error: expected ;' before '::' token
/home/gayanm/street/src/QueryServer_NEW/ITier.h:66:
error:it' undeclared (first use this
function)
/home/gayanm/street/src/QueryServer_NEW/ITier.h:66:
error: request for member end' in
((ITier)this)->ITier::map_Tiers',
which is of non-class type int'
/home/gayanm/street/src/QueryServer_NEW/ITier.h:
In member functionvoid
ITier::Set(T, ITier)':
/home/gayanm/street/src/QueryServer_NEW/ITier.h:74:
error: request for member insert' in
((ITier*)this)->ITier::map_Tiers',
which is of non-class type int'
/home/gayanm/street/src/QueryServer_NEW/ITier.h:74:
error:pair' undeclared (first use
this function)
/home/gayanm/street/src/QueryServer_NEW/ITier.h:74:
error: expected primary-expression
before ',' token
/home/gayanm/street/src/QueryServer_NEW/ITier.h:74:
error: ParamITier' undeclared (first
use this function)
/home/gayanm/street/src/QueryServer_NEW/ITier.h:74:
error: expected primary-expression
before '>' token
/home/gayanm/street/src/QueryServer_NEW/ITier.h:
At global scope:
/home/gayanm/street/src/QueryServer_NEW/ITier.h:93:
error: baseITier' with
only non-default constructor in class
without a constructor
/home/gayanm/street/src/QueryServer_NEW/ITier.h:109:
error: expected class-name before '{'
token
Could you please point out how to fix these?
Thank You.
Line 60 does not access a depending name. What you use is ITier<T> of which the compiler knows it's a template given an argument. Instead of typename you want to use typedef ;)
Line 64 does access the depending name iterator which is a type-name, so you have to put typename before std::map. I put the two disambiguations, template and typename on this answer: Disambiguations of dependent names.
Line 74 would be right, if you fix the bug in Line 60, as far as i can see.
Line 83 is alright in itself as far as i can see.
Also, I would recommend that you pass const T& to the functions instead of T, since you cannot be sure (it's a template parameter!) that it'll be a "cheap" copy.
Thanks a lot litb. I was able to fix my code with the guidelines you provided.
class IStatMsg;
template <typename T>
class ITier
{
public:
// Methods
ITier(){};
ITier(TierType oType) : o_Type(oType){};
virtual ~ITier(){};
//typename ITier<T> ParamITier;
ITier<T>* Get(T oKey)
{
typename std::map<T, ITier<T>*>::iterator it = map_Tiers.find(oKey);
if (it != map_Tiers.end())
return it->second;
return NULL;
}
void Set(T oKey, ITier<T>* pTier)
{
map_Tiers.insert(std::pair<T, ITier<T>*>(oKey, pTier));
}
TierType GetType() { return o_Type; }
protected:
// Methods
// Attributes
std::map<T, ITier<T>*> map_Tiers;
TierType o_Type;
private:
// Methods
// Attributes
};