![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,254
Rep Power: 5
![]() |
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). |
|
|
|
|
|
#12 | |
|
Expert Programmer
Join Date: Jun 2005
Posts: 882
Rep Power: 4
![]() |
I tried it under Visual C 7 and this is the linker error:
Quote:
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. |
|
|
|
|
|
|
#13 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,254
Rep Power: 5
![]() |
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). |
|
|
|
|
|
#14 | |
|
Hobbyist Programmer
Join Date: Nov 2006
Location: 163H
Posts: 215
Rep Power: 3
![]() |
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:
#include "functions.cpp"
__________________
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. |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |