473,395 Members | 1,675 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

Jul 22 '05 #1
8 1608
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;
}
}
}
};

Jul 22 '05 #2
Bart Blommerde wrote:
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>
There is no such standard header.
#include <string>

template<class KeyType, class ValType> class DataBuffer
{
private :
hash_map<KeyType, ValType> buf;
'hash_map' is undefined.

public :
DataBuffer(){}
~DataBuffer(){}

void buffer(DataSource<KeyType, ValType> &ds,
'DataSource' is undefined [here].
const KeyType &lower, const KeyType &upper);
ValType * getElement(const KeyType &id);
};
[...the rest of the non-compilable source snipped...]


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
Jul 22 '05 #3
>> #include <hash_map>


There is no such standard header.
Say what?

http://www.sgi.com/tech/stl/hash_map.html
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.


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

Jul 22 '05 #4
Bart Blommerde wrote:
#include <hash_map>

There is no such standard header.


Say what?

http://www.sgi.com/tech/stl/hash_map.html


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.

Jul 22 '05 #5
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]);
}

Jul 22 '05 #6
Are hash tables part of the C++ standard?
No.


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

Jul 22 '05 #7
Bart Blommerde wrote:
The code below can be compiled by g++ 2.95 (and probably g++ 3.x),
'probably'? So, you haven't tried, have you?
except of course for the before mentioned error.
No it doesn't. There is a curly brace missing, and at least three other
mistakes. See below.


******************* 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++)
{

{
Do you see the two curly braces above this line? Remove one of them.
data[i] = v[i];
As soon as you get your curly braces in order, 'data' is _undefined_ here.
Did you mean to name your 'hash_map' argument that?
}
}
};
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)
The line above doesn't compile. Did you mean

while (cur != end && (*cur).first <= upper)
{
if (*cur >= lower)
Same here
if ((*cur).first >= 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);\
Note that 'lower' and 'upper' are _const_ here.
ValType * getElement(const KeyType &id);
};
template<class KeyType, class ValType> void DataBuffer<KeyType,
ValType>::buffer(DataSource<KeyType, ValType> &ds,
KeyType &lower, KeyType &upper)
Note that 'lower' and 'upper' are NOT _const_ here. This is not the right
definition for 'buffer' function.
{
ds.store(lower, upper, buf);
}
template<class KeyType, class ValType> ValType *
DataBuffer<KeyType, ValType>::getElement(KeyType &id)
{
return &(buf[id]);
}


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

Victor
Jul 22 '05 #8
No it doesn't. There is a curly brace missing, and at least three other
mistakes. See below.


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

Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: martin.tschofen | last post by:
How do I select only the first node from a for-each loop that contains a the element "photo". the following xsl finds all "art" elements that have a "photo" element. The problem is that I only...
4
by: | last post by:
I have a datagrid with a template column that has a hyperlink and a label. The hyperlink text is bound to Title from my dataset and the label text is bound to Author in the dataset. The grid...
4
by: hawat.thufir | last post by:
Just for posterity: $ pwd /home/thufir/xalon/w3school $ whoami thufir $ ll total 24 -rw-rw-r-- 1 thufir thufir 239 Feb 19 09:16 foo.xml
5
by: Martin | last post by:
HI, I have some data like: <x> <y Position='Top'/> <y/> <x/> and a template like: <xsl:template match="/x/y">
2
by: IcedDante | last post by:
Working with a sorted group, the inability to use following-sibling (which uses Document Order) and convert an RTF (not avaible with the Parser that we are using) hampered our ability to solve the...
4
by: Efi Merdler | last post by:
Hi, I have an xml file with the following structure: <Dictionary> .... <Nested> <Entry> .... </Entry> <Entry> ....
1
by: saurcurban | last post by:
Hi, Following is the xml file: ------------------------------------- <?xml version="1.0" encoding="UTF-8"?> <wc> Delhi is the capital of India
2
by: ofuuzo1 | last post by:
Hei, I have the following xml file and I have tried to write xslt to extract only the values of the first "record" node. It does not work. I need some help. I used ---- to represent indent. ...
5
by: sniipe | last post by:
Hi, I have a problem with unicode string in Pylons templates(Mako). I will print first char from my string encoded in UTF-8 and urllib.quote(), for example string 'Łukasz': ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.