* template-id does not match any template declaration
@ 2004-08-27 9:16 learning c++
2004-08-27 12:11 ` Arno Wilhelm
2004-08-27 15:04 ` Eljay Love-Jensen
0 siblings, 2 replies; 3+ messages in thread
From: learning c++ @ 2004-08-27 9:16 UTC (permalink / raw)
To: gcc-help
Hi,
I am a novice for C++, and I compiled a program with C++ and there are an
error, Pleaes help me.
Thanks,
#include <iostream>
template <class T>
class pair {
T value1, value2;
public:
pair (T first, T second)
{value1=first; value2=second;}
T module () {return 0;}
};
template <>
class pair <int> {
int value1, value2;
public:
pair (int first, int second)
{value1=first; value2=second;}
int module ();
};
template <>
int pair<int>::module() {
return value1%value2;
}
int main () {
pair <int> myints (100,75);
pair <float> myfloats (100.0,75.0);
std::cout << myints.module() << '\n';
std::cout << myfloats.module() << '\n';
return 0;
}
Error:
templatemodule1.cpp:22: error: template-id `module<>' for `int
pair<int>::module()' does not match any template declaration
templatemodule1.cpp:22: error: syntax error before `{' token
_________________________________________________________________
Add photos to your messages with MSN 8. Get 2 months FREE*.
http://join.msn.com/?page=features/featuredemail
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: template-id does not match any template declaration
2004-08-27 9:16 template-id does not match any template declaration learning c++
@ 2004-08-27 12:11 ` Arno Wilhelm
2004-08-27 15:04 ` Eljay Love-Jensen
1 sibling, 0 replies; 3+ messages in thread
From: Arno Wilhelm @ 2004-08-27 12:11 UTC (permalink / raw)
To: learning c++; +Cc: gcc-help
> I am a novice for C++, and I compiled a program with C++ and there are an
> error, Pleaes help me.
Try this:
1 #include <iostream>
2
3
4 template <class T>
5 class pair
6 {
7 public:
8 pair(T first, T second) {value1=first; value2=second;}
9 T module () {return 0;}
10
11 private:
12 T value1;
13 T value2;
14 };
16
17 template <>
18 class pair <int>
19 {
20 public:
21 pair (int first, int second) {value1=first; value2=second;}
22 int module ( ) { return (value1 % value2); }
23 private:
24 int value1;
25 int value2;
26 };
35
36 int main (void)
37 {
38 pair <int> myints (100, 75);
39 pair <float> myfloats (100.0, 75.0);
40
41 std::cout << myints.module() << '\n';
42 std::cout << myfloats.module() << '\n';
43
44 return 0;
45 }
46
Arno
> Thanks,
>
> #include <iostream>
>
> template <class T>
> class pair {
> T value1, value2;
> public:
> pair (T first, T second)
> {value1=first; value2=second;}
> T module () {return 0;}
> };
>
> template <>
> class pair <int> {
> int value1, value2;
> public:
> pair (int first, int second)
> {value1=first; value2=second;}
> int module ();
> };
>
> template <>
> int pair<int>::module() {
> return value1%value2;
> }
> int main () {
> pair <int> myints (100,75);
> pair <float> myfloats (100.0,75.0);
> std::cout << myints.module() << '\n';
> std::cout << myfloats.module() << '\n';
> return 0;
> }
>
> Error:
> templatemodule1.cpp:22: error: template-id `module<>' for `int
> pair<int>::module()' does not match any template declaration
> templatemodule1.cpp:22: error: syntax error before `{' token
>
> _________________________________________________________________
> Add photos to your messages with MSN 8. Get 2 months FREE*.
> http://join.msn.com/?page=features/featuredemail
--
Arno Wilhelm <arno.wilhelm@profile.co.at>
proFILE Computersysteme GmbH
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: template-id does not match any template declaration
2004-08-27 9:16 template-id does not match any template declaration learning c++
2004-08-27 12:11 ` Arno Wilhelm
@ 2004-08-27 15:04 ` Eljay Love-Jensen
1 sibling, 0 replies; 3+ messages in thread
From: Eljay Love-Jensen @ 2004-08-27 15:04 UTC (permalink / raw)
To: learning c++, gcc-help
Hi,
You were almost there! Just a little syntax tweak for your separate
definition of your member function of your explicit template specialization
for pair<int>::module. (See below.)
Also, I changed the return statement on your general pair template
pair::module to something more suitable.
Caution, there is a std::pair. Your ::pair is okay, but could be a little
better protected by putting your pair class in your own namespace. Or,
make sure you never do a "using namespace std" anywhere (I disparage
"pulling in the std uberverse namespace" on general principle).
HTH,
--Eljay
-------------------------------
#include <iostream>
using std::cout;
template <class T>
class pair
{
T value1;
T value2;
public:
pair(T first, T second)
{
value1 = first;
value2 = second;
}
T module()
{
return T();
}
};
template <>
class pair<int>
{
int value1;
int value2;
public:
pair(int first, int second)
{
value1 = first;
value2 = second;
}
int module();
};
int pair<int>::module()
{
return value1 % value2;
}
int main()
{
pair<int> myints(100, 75);
pair<float> myfloats(100.0, 75.0);
cout << myints.module() << '\n';
cout << myfloats.module() << '\n';
}
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2004-08-27 12:27 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-08-27 9:16 template-id does not match any template declaration learning c++
2004-08-27 12:11 ` Arno Wilhelm
2004-08-27 15:04 ` Eljay Love-Jensen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).