Some errors when using empty template argument

P

PengYu.UT

Hi,

"call()" calles "show()". I got some error as indicated below in the
comments. I tried to delete the definition and declaration of "call()"
and call "show()" in the main() body, then it got compiled. I'm not
sure what's wrong with the following code. Would you please help me to
fix it?

Best wishes,
Peng


#include <iostream>
#include <complex>

template <typename Tp>
class A{
public:
A(){};
void call();
void show();
};

template <>
void A<int>::call(){
A<int>::show();
}

template <>
void A<double>::call(){
A<double>::show();
}

template <>
void A<double>::show(){//get compile error here
std::cout << "double" << std::endl;
}

template <>
void A<int>::show(){
std::cout << "int" << std::endl;
}

int main(int argc, char *argv[]) {
A<double> a;
A<int> b;
a.call();//a.show();
b.call();//b.show();
}
 
V

Victor Bazarov

"call()" calles "show()". I got some error as indicated below in the
comments. I tried to delete the definition and declaration of "call()"
and call "show()" in the main() body, then it got compiled. I'm not
sure what's wrong with the following code. Would you please help me to
fix it?

Best wishes,
Peng


#include <iostream>
#include <complex>

template <typename Tp>
class A{
public:
A(){};
void call();
void show();
};

template <>
void A<int>::call(){
A<int>::show();
}

template <>
void A<double>::call(){
A<double>::show();
}

template <>
void A<double>::show(){//get compile error here

WHAT compile error?

The Comeau compiler essentially suggest that both 'show' specialisations
should be moved right after the class template definition. Do it, see
if it fixes your "compile error".
std::cout << "double" << std::endl;
}

template <>
void A<int>::show(){
std::cout << "int" << std::endl;
}

int main(int argc, char *argv[]) {
A<double> a;
A<int> b;
a.call();//a.show();
b.call();//b.show();
}

V
 
P

PengYu.UT

I'm using g++3.3.

Following your suggestion, I got it compiler. But why the order of the
function definitions matters?

Thanks,
Peng
 
V

Victor Bazarov

I'm using g++3.3.

I don't think it makes any difference.
Following your suggestion, I got it compiler. But why the order of the
function definitions matters?

The definitions in this case are also declarations. The calls to those
functions in the 'call' member specialisations probably requires that
the specialisation is declared if it's to be used. I am too lazy to
look in the Standard.

V
 
G

Greg Comeau

Following your suggestion, I got it compiler. But why the order of the
function definitions matters?

For the same reason it matters here:

int main()
{
return foo();
}

int foo() { return 0; }
 

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

No members online now.

Forum statistics

Threads
474,294
Messages
2,571,511
Members
48,200
Latest member
SCPKatheri

Latest Threads

Top