Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 16th, 2007, 5:04 AM   #11
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,254
Rep Power: 5 grumpy will become famous soon enough
It might help if you specified what symbol is being reported undefined. Lacking that information, my guess is that you need to specify an extra library for the link phase, or that the missing symbol is in code you haven't shown (eg where you create an instance of your template class).

In addition to Dawei's comment about the type of matrix, presumably in the operator<<() you intended to output g.matrix[i][j];

You also haven't provided a main() function, or any code that creates/uses an instance of your template class. One "feature" of templates is that errors in their implementation often don't show up until you actually use them (as compilers often defer instantiating/specialising templates until it has no other option).
grumpy is offline   Reply With Quote
Old Jan 16th, 2007, 8:01 AM   #12
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 882
Rep Power: 4 The Dark is on a distinguished road
I tried it under Visual C 7 and this is the linker error:
Quote:
test.obj : error LNK2019: unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Gauss<int,5> const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABV?$Gauss@H$04@@@Z) referenced in function _main
It works if you don't have the code as a friend function (and make the matrix public). I'm not suggesting this as a solution, just pointing out that adding the friend to the class stuffs up the linking for some reason.

Here is the code I used:
#include <iostream>

using std::ostream;
using std::cout;

template <class T, int size = 3>
class Gauss
{
private :
int matrix[size][size];

public:

friend ostream& operator<< (ostream &stream, const Gauss<T, size> &g);
};

template <class T, int size>
ostream& operator<<(ostream &stream, const Gauss<T, size> &g)
{
for (int i = 0; i < size; i++)
          for (int j = 0; j < size; j++)
               stream << g.matrix[i][j];

return stream;
}
int main()
{
  Gauss<int, 5> me;

  cout << (Gauss<int, 5>)me;
}

I'd still go with having a public member function that does the output and call that from the global << operator.
The Dark is offline   Reply With Quote
Old Jan 16th, 2007, 9:22 AM   #13
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,254
Rep Power: 5 grumpy will become famous soon enough
OK; the code, apart from my pet peeves about the "using" directives, is actually correct, as I understand it.

I seem to recall come debates a couple of years back about something like this, where some experts claimed Microsoft compilers doing this sort of thing was a bug, but Microsoft claimed that their compiler is correct in rejecting such things because of an argument based on function overloading. Microsoft's argument struck me as spurious at the time.

Unfortunately, I can suggest no workaround other than the one suggested by Dark early on (don't have the operator function as a friend, and have it call a public member).
grumpy is offline   Reply With Quote
Old Jan 18th, 2007, 6:30 AM   #14
pegasus001
Hobbyist Programmer
 
pegasus001's Avatar
 
Join Date: Nov 2006
Location: 163H
Posts: 215
Rep Power: 3 pegasus001 is on a distinguished road
Hello again boys(and girls). Sorry for the late post but i hadn`t much time solving the operator<< earlier because of school. Well i have spent quite a long time reading about templates and found a book "templates : a tutorial and reference"(i think). Well here`s the deal i solved it an for anybody who is curious here is the code :

#include <iostream>

using std::ostream;
using std::cout;

template <class T, int size = 3>
class Gauss
{
private :
int matrix[size][size];

public:

friend ostream& operator<< (ostream &stream, const Gauss<T, size> &g)
{
{
for (int i = 0; i < size; i++)
          for (int j = 0; j < size; j++)
               stream << g.matrix[i][j];

return stream;
}

}
};

int main()
{
  Gauss<int, 5> me;

  cout << (Gauss<int, 5>)me;
}

So as you can see i defined the overloaded function in the template body.(sorry i didn`t try to inline, coz i was very tired). well for me it worked and i hope it will work for you too.

Now about:
Quote:
Originally Posted by DaWei
You should not include source files. Including amounts to a copy and paste operation by the compiler BEFORE COMPILATION. This is the path to multiple definition errors.
It is advisable that the definitions of the functions of a template class should be in the same file. If you want to do it separately than you have to include an
#include "functions.cpp"
in the end of the header file. If you don`t do so the compiler will compile your files but the linker will complain. Because when the header file is compiled it looks for the definition of the functions and if it dowsn`t find them it tells the linker to find them elsewhere but the two files being compiled separately the linker cannot find the definitions anywhere and stops the execution outputing a linker error.
__________________
You never test the depth of a river with both feet.
The believer is happy. The doubter is wise.
Free speech carries with it some freedom to listen.
The next generation will always surpass the previous one. It`s one of the never ending cycles of life.
pegasus001 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Template definitions problem... Soulstorm C++ 5 Nov 30th, 2006 5:26 AM
friends, templates, and other s**t bl00dninja C++ 4 Oct 14th, 2006 2:15 AM
dev c++ software, template problem cairo C++ 11 Jun 2nd, 2006 1:42 PM
Template + operator problem Polyphemus_ C++ 3 Sep 30th, 2005 7:43 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:38 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC