Connecting Tech Pros Worldwide Help | Site Map

my first template

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 22nd, 2005, 06:00 PM
Bart Blommerde
Guest
 
Posts: n/a
Default my first template

Compiling the code below results in the following g++ error :

' cannot declare member function
sourceA::DataBuffer<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[i] = v[i];
}
}
};


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;
}
}
}
};


  #2  
Old July 22nd, 2005, 06:00 PM
Bart Blommerde
Guest
 
Posts: n/a
Default Re: my first template (readable)

Compiling the code below results in the following g++ error :

' cannot declare member function
sourceA::DataBuffer<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[i] = v[i];
}
}
};


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;
}
}
}
};

  #3  
Old July 22nd, 2005, 06:01 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: my first template (readable)

Bart Blommerde wrote:[color=blue]
> Compiling the code below results in the following g++ error :
>
> ' cannot declare member function
> sourceA::DataBuffer<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>[/color]

There is no such standard header.
[color=blue]
> #include <string>
>
> template<class KeyType, class ValType> class DataBuffer
> {
> private :
> hash_map<KeyType, ValType> buf;[/color]

'hash_map' is undefined.
[color=blue]
>
> public :
> DataBuffer(){}
> ~DataBuffer(){}
>
> void buffer(DataSource<KeyType, ValType> &ds,[/color]

'DataSource' is undefined [here].
[color=blue]
> const KeyType &lower, const KeyType &upper);
> ValType * getElement(const KeyType &id);
> };
> [...the rest of the non-compilable source snipped...][/color]

Perhaps if you post the complete compilable (except for the error you're
asking about) piece of code, we could help you. The code you posted is
full of syntax and semantic errors. Did you just type it in? Don't.
Copy it from your source and paste into the posting.

Victor
  #4  
Old July 22nd, 2005, 06:01 PM
Bart Blommerde
Guest
 
Posts: n/a
Default Re: my first template (readable)

>> #include <hash_map>[color=blue]
>
>
> There is no such standard header.[/color]

Say what?

http://www.sgi.com/tech/stl/hash_map.html
[color=blue]
> Perhaps if you post the complete compilable (except for the error you're
> asking about) piece of code, we could help you. The code you posted is
> full of syntax and semantic errors. Did you just type it in? Don't.
> Copy it from your source and paste into the posting.[/color]

The code is copied from different source files. Excuse me for mixing up
the order of declarations, but the code is just meant as an example of
incorrect template usage. The problem can better be identified by
looking at the template definitions than by compiling the code.


Bart

  #5  
Old July 22nd, 2005, 06:01 PM
Pete
Guest
 
Posts: n/a
Default Re: my first template (readable)

Bart Blommerde wrote:
[color=blue][color=green][color=darkred]
>>> #include <hash_map>[/color]
>>
>>
>> There is no such standard header.[/color]
>
> Say what?
>
> http://www.sgi.com/tech/stl/hash_map.html[/color]

From http://www.sgi.com/tech/stl/FAQ.html :

Are hash tables part of the C++ standard?
No. The hash table classes (hash_set, hash_map hash_multiset hash_multimap
hash) are an extension. They may be added to a future revision of the C++
standard.

The rope and slist classes are also extensions.

  #6  
Old July 22nd, 2005, 06:01 PM
Bart Blommerde
Guest
 
Posts: n/a
Default Re: my first template (Compilable)

The code below can be compiled by g++ 2.95 (and probably g++ 3.x),
except of course for the before mentioned error.


******************* NONROBUST TESTING CODE ***************


#include <map>
#include <hash_map>
#include <string>

using namespace std;

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[i] = v[i];
}
}
};


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;
}
}
}
};


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]);
}

  #7  
Old July 22nd, 2005, 06:01 PM
Bart Blommerde
Guest
 
Posts: n/a
Default Re: my first template (readable)

[color=blue]
> Are hash tables part of the C++ standard?
> No.[/color]

Okay, wasn't aware of that.

A bit confusing though, since Stroustrup (The C++ programming language,
3rd edition) mentions the hash_map in his overview of standard
containers.




Bart

  #8  
Old July 22nd, 2005, 06:01 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: my first template (Compilable)

Bart Blommerde wrote:[color=blue]
> The code below can be compiled by g++ 2.95 (and probably g++ 3.x),[/color]

'probably'? So, you haven't tried, have you?
[color=blue]
> except of course for the before mentioned error.[/color]

No it doesn't. There is a curly brace missing, and at least three other
mistakes. See below.
[color=blue]
>
>
> ******************* NONROBUST TESTING CODE ***************
>
>
> #include <map>
> #include <hash_map>
> #include <string>
>
> using namespace std;
>
> 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++)
> {
>
> {[/color]

Do you see the two curly braces above this line? Remove one of them.
[color=blue]
> data[i] = v[i];[/color]

As soon as you get your curly braces in order, 'data' is _undefined_ here.
Did you mean to name your 'hash_map' argument that?
[color=blue]
> }
> }
> };
>
>
> 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)[/color]

The line above doesn't compile. Did you mean

while (cur != end && (*cur).first <= upper)
[color=blue]
> {
> if (*cur >= lower)[/color]

Same here
if ((*cur).first >= lower)
[color=blue]
> {
> data[cur->first] = cur->second;
> ++cur;
> }
> }
> }
> };
>
>
> 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);\[/color]

Note that 'lower' and 'upper' are _const_ here.
[color=blue]
> ValType * getElement(const KeyType &id);
> };
>
>
> template<class KeyType, class ValType> void DataBuffer<KeyType,
> ValType>::buffer(DataSource<KeyType, ValType> &ds,
> KeyType &lower, KeyType &upper)[/color]

Note that 'lower' and 'upper' are NOT _const_ here. This is not the right
definition for 'buffer' function.
[color=blue]
> {
> ds.store(lower, upper, buf);
> }
>
>
> template<class KeyType, class ValType> ValType *
> DataBuffer<KeyType, ValType>::getElement(KeyType &id)
> {
> return &(buf[id]);
> }
>[/color]

I really you shouldn't over-use the newsgroup as your code clean-up tool.

Victor
  #9  
Old July 22nd, 2005, 06:01 PM
Bart Blommerde
Guest
 
Posts: n/a
Default Re: my first template (Compilable)

[color=blue]
> No it doesn't. There is a curly brace missing, and at least three other
> mistakes. See below.[/color]

Yur right. Sorry for wasting your time. The reported error was just an
obscure way of g++ telling me I had some syntax errors.

TOPIC CLOSED!!



regards,
Bart

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.