Connecting Tech Pros Worldwide Help | Site Map

templates c++

  #1  
Old July 23rd, 2005, 04:57 AM
Marcin Szewczyk (Wodny)
Guest
 
Posts: n/a
Hello everyone. Normally I would search the entire internet to find a
solution, but this time I've got a situation. I've been trying to solve
this myself for a couple of days. Please help.

=============================================
Errors:
=============================================
In instantiation of `lista_iter<char*>':
lista_iter.cpp:22: instantiated from here
lista_iter.cpp:13: template-id
`operator+<>' for `lista_iter<char*> operator+(const lista_iter<char*>&,
const lista_iter<char*>&)' does not match any template declaration
lista_iter.cpp:12: template-id
`operator<< <>' for `std::basic_ostream<char, std::char_traits<char> >&
operator<<(std::basic_ostream<char, std::char_traits<char> >&, const
lista_iter<char*>&)' does not match any template declaration
=============================================
Program:
=============================================

#include <iostream>

using namespace std;

template <class T>
class lista_iter{
protected:
int wezel_;
public:
lista_iter() : wezel_(0) { };
int szukaj(T, int);
friend ostream& operator<< <> (ostream&, const lista_iter<T> &);
friend lista_iter<T> operator+ <> (const lista_iter<T> &, const
lista_iter<T> &);
};

template <class T>
int lista_iter<T>::szukaj(T a, int b){
return 0;
}

template <>
int lista_iter<char*>::szukaj(char* a, int b){
return 1;
}

template <class T>
ostream& operator<<(ostream& strumien, const lista_iter<T>& lista){
return strumien;
}

template <>
ostream& operator<<(ostream& strumien, const lista_iter<char*>& lista){
return strumien;
}

template <class T>
lista_iter<T> operator+(const lista_iter<T> & a, const lista_iter<T> & b){
return a;
}

int main(){
lista_iter<int> a;
lista_iter<char*> b;
return 0;
}

--
Wodny
Marcin Szewczyk
http://www.wodny.prv.pl
wodny@21%_w_atmosferze.pl
GG:4624915
  #2  
Old July 23rd, 2005, 04:57 AM
Victor Bazarov
Guest
 
Posts: n/a

re: templates c++


Marcin Szewczyk (Wodny) wrote:[color=blue]
> Hello everyone. Normally I would search the entire internet to find a
> solution, but this time I've got a situation. I've been trying to solve
> this myself for a couple of days. Please help.
> [...][/color]

The code as posted compiles fine with Visual C++ .NET 2003 and with
Comeau online test-drive compiler. Perhaps you need to update the
compiler you've been using...

V
  #3  
Old July 23rd, 2005, 04:57 AM
Marcin Szewczyk (Wodny)
Guest
 
Posts: n/a

re: templates c++


Victor Bazarov wrote:[color=blue]
> Marcin Szewczyk (Wodny) wrote:
>[color=green]
>> Hello everyone. Normally I would search the entire internet to find a
>> solution, but this time I've got a situation. I've been trying to
>> solve this myself for a couple of days. Please help.
>> [...][/color]
>
>
> The code as posted compiles fine with Visual C++ .NET 2003 and with
> Comeau online test-drive compiler. Perhaps you need to update the
> compiler you've been using...[/color]

OK. Thanks. But still it has to compile at school, where g++ is used.

--
Wodny
Marcin Szewczyk
http://www.wodny.prv.pl
wodny@21%_w_atmosferze.pl
GG:4624915
  #4  
Old July 23rd, 2005, 04:57 AM
Victor Bazarov
Guest
 
Posts: n/a

re: templates c++


Marcin Szewczyk (Wodny) wrote:[color=blue]
> Victor Bazarov wrote:
>[color=green]
>> Marcin Szewczyk (Wodny) wrote:
>>[color=darkred]
>>> Hello everyone. Normally I would search the entire internet to find a
>>> solution, but this time I've got a situation. I've been trying to
>>> solve this myself for a couple of days. Please help.
>>> [...][/color]
>>
>>
>>
>> The code as posted compiles fine with Visual C++ .NET 2003 and with
>> Comeau online test-drive compiler. Perhaps you need to update the
>> compiler you've been using...[/color]
>
>
> OK. Thanks. But still it has to compile at school, where g++ is used.[/color]

Our school used to have really crappy chalk (different country, different
epoch). Teachers and students struggled with it to the point when nobody
couldn't read nothing off the friggin' blackboard. So I brought my own
chalk (let's not go into where I got it). Our lessons went smoother and
when pieces of the chalk I brought remained after our lessons, others
benefited too. True story.

V
  #5  
Old July 23rd, 2005, 04:58 AM
Larry I Smith
Guest
 
Posts: n/a

re: templates c++


Marcin Szewczyk (Wodny) wrote:[color=blue]
> Hello everyone. Normally I would search the entire internet to find a
> solution, but this time I've got a situation. I've been trying to solve
> this myself for a couple of days. Please help.
>
> =============================================
> Errors:
> =============================================
> In instantiation of `lista_iter<char*>':
> lista_iter.cpp:22: instantiated from here
> lista_iter.cpp:13: template-id
> `operator+<>' for `lista_iter<char*> operator+(const lista_iter<char*>&,
> const lista_iter<char*>&)' does not match any template declaration
> lista_iter.cpp:12: template-id
> `operator<< <>' for `std::basic_ostream<char, std::char_traits<char> >&
> operator<<(std::basic_ostream<char, std::char_traits<char> >&, const
> lista_iter<char*>&)' does not match any template declaration
> =============================================
> Program:
> =============================================
>
> #include <iostream>
>
> using namespace std;
>
> template <class T>
> class lista_iter{
> protected:
> int wezel_;
> public:
> lista_iter() : wezel_(0) { };
> int szukaj(T, int);
> friend ostream& operator<< <> (ostream&, const lista_iter<T> &);
> friend lista_iter<T> operator+ <> (const lista_iter<T> &, const
> lista_iter<T> &);
> };
>
> template <class T>
> int lista_iter<T>::szukaj(T a, int b){
> return 0;
> }
>[/color]

Move the following specialization so that it appears after
all of the operator<< and operator+ templates have been defined
(i.e. move it just before main()). The compiler is trying to
instantiate a lista_iter<char*>, but the compiler has not yet
read the code for all of the required operators because they
appear later in the source file.
[color=blue]
> template <>
> int lista_iter<char*>::szukaj(char* a, int b){
> return 1;
> }
>
> template <class T>
> ostream& operator<<(ostream& strumien, const lista_iter<T>& lista){
> return strumien;
> }
>
> template <>
> ostream& operator<<(ostream& strumien, const lista_iter<char*>& lista){
> return strumien;
> }
>
> template <class T>
> lista_iter<T> operator+(const lista_iter<T> & a, const lista_iter<T> & b){
> return a;
> }
>
> int main(){
> lista_iter<int> a;
> lista_iter<char*> b;
> return 0;
> }
>[/color]

Regards,
Larry

--
Anti-spam address, change each 'X' to '.' to reply directly.
  #6  
Old July 23rd, 2005, 04:58 AM
Marcin Szewczyk (Wodny)
Guest
 
Posts: n/a

re: templates c++


Larry I Smith wrote:[color=blue]
> Marcin Szewczyk (Wodny) wrote:[color=green]
>>=============================================
>>Errors:
>>=============================================
>>In instantiation of `lista_iter<char*>':
>>lista_iter.cpp:22: instantiated from here
>>lista_iter.cpp:13: template-id
>> `operator+<>' for `lista_iter<char*> operator+(const lista_iter<char*>&,
>> const lista_iter<char*>&)' does not match any template declaration
>>lista_iter.cpp:12: template-id
>> `operator<< <>' for `std::basic_ostream<char, std::char_traits<char> >&
>> operator<<(std::basic_ostream<char, std::char_traits<char> >&, const
>> lista_iter<char*>&)' does not match any template declaration
>>=============================================
>>Program:
>>=============================================
>>
>>#include <iostream>
>>
>>using namespace std;
>>
>>template <class T>
>>class lista_iter{
>> protected:
>> int wezel_;
>> public:
>> lista_iter() : wezel_(0) { };
>> int szukaj(T, int);
>> friend ostream& operator<< <> (ostream&, const lista_iter<T> &);
>> friend lista_iter<T> operator+ <> (const lista_iter<T> &, const
>>lista_iter<T> &);
>>};
>>
>>template <class T>
>>int lista_iter<T>::szukaj(T a, int b){
>> return 0;
>>}
>>[/color]
>
>
> Move the following specialization so that it appears after
> all of the operator<< and operator+ templates have been defined
> (i.e. move it just before main()). The compiler is trying to
> instantiate a lista_iter<char*>, but the compiler has not yet
> read the code for all of the required operators because they
> appear later in the source file.[/color]

Thank You very much. Why is it so simple :-) ?!

--
Wodny
Marcin Szewczyk
http://www.wodny.prv.pl
wodny@21%_w_atmosferze.pl
GG:4624915
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there any strong reason for not using Templates? NewToCPP answers 28 July 31st, 2006 09:25 PM
Overuse of Templates Ted answers 25 March 26th, 2006 09:15 AM
Genericity is *not* templates E. Robert Tisdale answers 22 July 22nd, 2005 10:04 AM
Genericity is *not* templates E. Robert Tisdale answers 22 July 22nd, 2005 09:35 AM
XPath and XSL templates Tom Alsberg answers 5 July 20th, 2005 08:18 AM