B
Bart Blommerde
Compiling the code below results in the following g++ error :
' cannot declare member function
sourceA:
ataBuffer<KeyType,ValType>::buffer' within `SourceA' '
What I'm trying to do here is defining a template for databuffers
containing elements of any given DataSource. DataSource is a template
for any database or fileStream containing keys and values. SourceA and
SourceB are example specializations for this template (though literal
values are used instead of file or database output).
It must be something very trivial that I'm doing wrong. Anyone can tell
me what it is?
******************** NONROBUST TESTING CODE **************************
#include <map>
#include <hash_map>
#include <string>
template<class KeyType, class ValType> class DataBuffer
{
private :
hash_map<KeyType, ValType> buf;
public :
DataBuffer(){}
~DataBuffer(){}
void buffer(DataSource<KeyType, ValType> &ds,
const KeyType &lower, const KeyType &upper);
ValType * getElement(const KeyType &id);
};
template<class KeyType, class ValType> void DataBuffer<KeyType,
ValType>::buffer(DataSource<KeyType, ValType> &ds,
KeyType &lower, KeyType &upper)
{
ds.store(lower, upper, buf);
}
template<class KeyType, class ValType> ValType *
DataBuffer<KeyType, ValType>::getElement(KeyType &id)
{
return &(buf[id]);
}
template<class KeyType, class ValType> class DataSource
{
public :
DataSource();
virtual ~DataSource();
virtual void store(const KeyType &lower,
const KeyType &upper,
hash_map<KeyType, ValType> &data);
};
class SourceA : public DataSource<int, int>
{
private :
vector<int> v;
public :
SourceA(){
{
v.resize(3);
v[0] = 2;
v[1] = -1;
v[2] = 14;
}
~SourceA(){}
void store(const int lower, const int upper,
hash_map<int, int>)
{
for (int i=lower; i <= upper; i++)
{
{
data = v;
}
}
};
class SourceB : public DataSource<string, int>
{
private :
map<string, int> m;
public :
SourceB()
{
m["aa"] = 1;
m["cc"] = 3;
m["cd"] = 5;
}
~SourceB(){}
void store(const string &lower, const string &upper,
hash_map<string, int> &data)
{
map<string, int>::iterator cur = m.begin();
map<string, int>::iterator end = m.end();
while (cur != end && *cur <= upper)
{
if (*cur >= lower)
{
data[cur->first] = cur->second;
++cur;
}
}
}
};
' cannot declare member function
sourceA:
What I'm trying to do here is defining a template for databuffers
containing elements of any given DataSource. DataSource is a template
for any database or fileStream containing keys and values. SourceA and
SourceB are example specializations for this template (though literal
values are used instead of file or database output).
It must be something very trivial that I'm doing wrong. Anyone can tell
me what it is?
******************** NONROBUST TESTING CODE **************************
#include <map>
#include <hash_map>
#include <string>
template<class KeyType, class ValType> class DataBuffer
{
private :
hash_map<KeyType, ValType> buf;
public :
DataBuffer(){}
~DataBuffer(){}
void buffer(DataSource<KeyType, ValType> &ds,
const KeyType &lower, const KeyType &upper);
ValType * getElement(const KeyType &id);
};
template<class KeyType, class ValType> void DataBuffer<KeyType,
ValType>::buffer(DataSource<KeyType, ValType> &ds,
KeyType &lower, KeyType &upper)
{
ds.store(lower, upper, buf);
}
template<class KeyType, class ValType> ValType *
DataBuffer<KeyType, ValType>::getElement(KeyType &id)
{
return &(buf[id]);
}
template<class KeyType, class ValType> class DataSource
{
public :
DataSource();
virtual ~DataSource();
virtual void store(const KeyType &lower,
const KeyType &upper,
hash_map<KeyType, ValType> &data);
};
class SourceA : public DataSource<int, int>
{
private :
vector<int> v;
public :
SourceA(){
{
v.resize(3);
v[0] = 2;
v[1] = -1;
v[2] = 14;
}
~SourceA(){}
void store(const int lower, const int upper,
hash_map<int, int>)
{
for (int i=lower; i <= upper; i++)
{
{
data = v;
}
}
};
class SourceB : public DataSource<string, int>
{
private :
map<string, int> m;
public :
SourceB()
{
m["aa"] = 1;
m["cc"] = 3;
m["cd"] = 5;
}
~SourceB(){}
void store(const string &lower, const string &upper,
hash_map<string, int> &data)
{
map<string, int>::iterator cur = m.begin();
map<string, int>::iterator end = m.end();
while (cur != end && *cur <= upper)
{
if (*cur >= lower)
{
data[cur->first] = cur->second;
++cur;
}
}
}
};