templates c++

  • Thread starter Marcin Szewczyk (Wodny)
  • Start date
M

Marcin Szewczyk (Wodny)

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
 
V

Victor Bazarov

Marcin said:
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.
[...]

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
 
M

Marcin Szewczyk (Wodny)

Victor said:
Marcin said:
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.
[...]


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...

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
 
V

Victor Bazarov

Marcin said:
Victor said:
Marcin said:
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.
[...]



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...


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

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
 
L

Larry I Smith

Marcin said:
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;
}

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

Regards,
Larry
 
M

Marcin Szewczyk (Wodny)

Larry said:
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.

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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,667
Latest member
DaniloB294

Latest Threads

Top