473,399 Members | 3,656 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,399 software developers and data experts.

problem with templates (?)

Hello!

I am implementing a dictionary, in C++, for my school assignment. But
when I am testing my Item class (this is what the dictionary is
supposed to store), I get a strange error message from my compiler
(read complainer) g++:

/tmp/ccC63pgw.o: In function `main':
/tmp/ccC63pgw.o(.text+0x43): undefined reference to `Item<int,
std::basic_string<char, std::char_traits<char>, std::allocator<char> >
::Item[in-charge](int, std::basic_string<char,

std::char_traits<char>, std::allocator<char> >)'
collect2: ld returned 1 exit status

Can you tell me where this subtle, or not so subtle, error is located?
Thanks in advance!

Matias
item.hh:

template<typename KeyType, typename ElemType>
class Item {
public:
Item(KeyType, ElemType);
Item(const Item &);
~Item() { delete _key; delete _elem; }

KeyType *getKey() const { return _key; }
ElemType *getElement() const { return _elem; }

private:
KeyType *_key;
ElemType *_elem;
};

item.cc:

#include "item.hh"

template<typename KeyType, typename ElemType>
Item<KeyType, ElemType>::Item(KeyType k, ElemType e) {
_key = new KeyType(k);
_elem = new ElemType(e);
}

template<typename KeyType, typename ElemType>
Item<KeyType, ElemType>::Item(const Item<KeyType, ElemType> &item) {
_key = new KeyType(*(item.getKey()));
_elem = new ElemType(*(item.getElement()));
}

test.cc:

#include "item.hh"

#include <iostream>
#include <string>

int main() {
Item<int, std::string> anItem(5, "a string");

std::cout << "key " << *(anItem.getKey())
<< "element " << *(anItem.getElement()) << "." <<
std::endl;
return 0;
}
Jul 22 '05 #1
4 1330
On 31 May 2004 13:47:02 -0700, s_******@yahoo.com (Matias S) wrote:
Hello!

I am implementing a dictionary, in C++, for my school assignment. But
when I am testing my Item class (this is what the dictionary is
supposed to store), I get a strange error message from my compiler
(read complainer) g++:

/tmp/ccC63pgw.o: In function `main':
/tmp/ccC63pgw.o(.text+0x43): undefined reference to `Item<int,
std::basic_string<char, std::char_traits<char>, std::allocator<char> >
::Item[in-charge](int, std::basic_string<char,

std::char_traits<char>, std::allocator<char> >)'
collect2: ld returned 1 exit status

Can you tell me where this subtle, or not so subtle, error is located?
Thanks in advance!

Put your member function templates in the header file. See:

http://www.parashift.com/c++-faq-lit...html#faq-34.12
-leor

--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #2
s_******@yahoo.com (Matias S) wrote:
I am implementing a dictionary, in C++, for my school assignment. But
when I am testing my Item class (this is what the dictionary is
supposed to store), I get a strange error message
To fix the error, simply move all the code you have in item.cc into
item.hh. I'm also going to cretique your code in general if you don't
mind.
item.hh:

template<typename KeyType, typename ElemType>
class Item {
public:
Item(KeyType, ElemType);
Item(const Item &);
~Item() { delete _key; delete _elem; }

KeyType *getKey() const { return _key; }
ElemType *getElement() const { return _elem; }

private:
KeyType *_key;
ElemType *_elem;
};
Note, I removed the file break so that the below is now in item.hh
template<typename KeyType, typename ElemType>
Item<KeyType, ElemType>::Item(KeyType k, ElemType e) {
_key = new KeyType(k);
_elem = new ElemType(e);
}

template<typename KeyType, typename ElemType>
Item<KeyType, ElemType>::Item(const Item<KeyType, ElemType> &item) {
_key = new KeyType(*(item.getKey()));
_elem = new ElemType(*(item.getElement()));
}
Why are you holding _key and _elem by pointer? Your code would be
drastically simpler and still correct if you hold them by value.
test.cc:

#include "item.hh"

#include <iostream>
#include <string>

int main() {
Item<int, std::string> anItem(5, "a string");

std::cout << "key " << *(anItem.getKey())
<< "element " << *(anItem.getElement()) << "." <<
std::endl;
return 0;
}


I would suggest you make your expectations explicit in the test code
rather than simply outputing text and hoping the reader of that text (a)
knows what he is looking for and (b) can find it among all the text that
will eventually be output. Something like this would be better:

int main( ) {
string s( "a string" );
Item<int, std::string> anItem( 5, s );

assert( anItem.getKey( ) == 5 );
assert( anItem.getElement( ) == s );

cout << "OK\n";
}

With the above, you can simply add more tests as you build up the class
and you won't get overloaded by heaps of output. If everything works as
expected you will see "OK" if not, the part that didn't work as expected
will be output to the screen.
Jul 22 '05 #3
On Mon, 31 May 2004 23:13:06 +0000, Daniel T. wrote:
To fix the error, simply move all the code you have in item.cc into
item.hh. I'm also going to cretique your code in general if you don't
mind.


Not at all. :-)
item.hh:

template<typename KeyType, typename ElemType>
class Item {
public:
Item(KeyType, ElemType);
Item(const Item &);
~Item() { delete _key; delete _elem; }

KeyType *getKey() const { return _key; }
ElemType *getElement() const { return _elem; }

private:
KeyType *_key;
ElemType *_elem;
};

Why are you holding _key and _elem by pointer? Your code would be
drastically simpler and still correct if you hold them by value.


To make sure I got it right: I should instead have
KeyType _key;
ElemType _elem; ?

Thanks for the feedback I got. Now the code compiles.

Matias
Jul 22 '05 #4
In article <pa****************************@yahoo.com>,
"Matias S" <s_******@yahoo.com> wrote:
On Mon, 31 May 2004 23:13:06 +0000, Daniel T. wrote:
Why are you holding _key and _elem by pointer? Your code would be
drastically simpler and still correct if you hold them by value.


To make sure I got it right: I should instead have
KeyType _key;
ElemType _elem; ?


Yes.
Jul 22 '05 #5

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

Similar topics

2
by: nanookfan | last post by:
Hi all, I'm having a bizarre problem converting XML files to HTML using an XSLT. The problem is only occuring in my Netscape 7.0 browser. What makes it more bizarre is that it is only...
7
by: Andy Fish | last post by:
Hi, I'm stuck with an XSL problem - can anyone give me any hints? I have some XML with nested formatting tags like this: <text> this is plain <bold> this is bold
5
by: Clifford W. Racz | last post by:
Has anyone solved the issue of translating lists in Word 2003 (WordML) into xHTML? I have been trying to get the nested table code for my XSLT to work for a while now, with no way to get the...
11
by: ree32 | last post by:
I have a problem with XSL sorting. The problem is that I need to create a 2 column table so I am using this. <xsl:apply-templates select="//Photo"> </xsl:apply-templates> With template...
13
by: Winbatch | last post by:
Hi, If this should be directed to another group, please let me know... I've been working with templates for a few weeks and have been able to develop some nice code on solaris using the Forte C++...
11
by: Rolf Barbakken | last post by:
I have an xml with records like this one: <a:response> <a:href>http://server/public/sol/comp/1049306.eml</a:href> <a:propstat> <a:status>HTTP/1.1 200 OK</a:status> <a:prop>...
4
by: dwergkees | last post by:
Hi, Got a litte problem here. I'm trying to create a XSLT file that will do a transformation from WordML format (MS Word XML format, see...
6
by: B. Williams | last post by:
I have a problem dealing with class template where I was to write a class and after submitting the class, this is the feedback I got back from the instructor. I don't really understand it. Can...
8
by: patrik.nyman | last post by:
Consider the following document: <?xml version="1.0"?> <!DOCTYPE test> <test> <list type="index"> <item>A</item> <item>B</item> <item>C</item> <cb/>
6
by: pauldepstein | last post by:
Let double NR( double x, double(*)(const double&) f ) be the signature of a Newton-Raphson function NR. Here, f is a function which returns a double and accepts a const double&. The aim of...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.