Hi, below is the code I copy from c++ tutorial, it is about template
specialization, but when I compile, I got error message:
error: template-id `module<>' for `int mypair<int>::module()' does
not match any template declaration
tmp.cpp:28: error: syntax error before `{' token
I guess it might be that my compiler does not recognize this type of
using template??? I am using g++-3.3 compiler under Ubuntu linux. any
help will be appreciated
here is the code :
1 //Template specialization
2 #include <iostream>
3
4 using namespace std;
5
6 template <class T>
7 class mypair {
8 T value1, value2;
9 public:
10 mypair (T first, T second)
11 {value1=first; value2=second;}
12 T module () {return 0;}
13 };
14
15 template <>
16 class mypair <int> {
17 int value1, value2;
18 public:
19 mypair (int first, int second)
20 {
21 value1=first;
22 value2=second;
23 }
24 int module ();
25 };
26
27 template <>
28 int mypair<int>::module() {
29 return value1%value2;
30 }
31
32 int main () {
33 mypair <int> myints (100,75);
34 mypair <float> myfloats (100.0,75.0);
35 cout << myints.module() << '\n';
36 cout << myfloats.module() << '\n';
37 return 0;
38 } 6 5496
blueblueblue2005 wrote: Hi, below is the code I copy from c++ tutorial, it is about template specialization, but when I compile, I got error message:
Don't put numbers on each line, it prevents us from compiling your
code. Just put a comment the offending line.
error: template-id `module<>' for `int mypair<int>::module()' does not match any template declaration tmp.cpp:28: error: syntax error before `{' token
I guess it might be that my compiler does not recognize this type of using template??? I am using g++-3.3 compiler under Ubuntu linux. any help will be appreciated
here is the code :
1 //Template specialization 2 #include <iostream> 3 4 using namespace std; 5 6 template <class T> 7 class mypair { 8 T value1, value2; 9 public: 10 mypair (T first, T second) 11 {value1=first; value2=second;} 12 T module () {return 0;} 13 }; 14 15 template <> 16 class mypair <int> { 17 int value1, value2; 18 public: 19 mypair (int first, int second) 20 { 21 value1=first; 22 value2=second; 23 } 24 int module (); 25 }; 26 27 template <> 28 int mypair<int>::module() { 29 return value1%value2; 30 }
Drop the "template <>". That's the syntax for an explicit
instantiation, not for a member function definition.
Remember that what goes before the :: is the class name. If the class
name is
template <class T>
class C;
then you'll have
template <class T>
void C<T>::f()
{
}
because the class name is "template <class T> C<T>".
If the class name is
template <>
class C<int>
then it is not a template class anymore. The name of that class is
C<int>:
void C<int>::f()
{
}
31 32 int main () { 33 mypair <int> myints (100,75); 34 mypair <float> myfloats (100.0,75.0); 35 cout << myints.module() << '\n';
36 cout << myfloats.module() << '\n'; 37 return 0; 38 }
Jonathan
hi, I solved the problem by comment out the definition at line 27
through line 30, instead, I moved the function definition inside the
class definition, and there is no compiler error, but why the compiler
does allow me to define the function outside the class definition???
here is the modefied code:
1 //Template specialization
2 #include <iostream>
3
4 using namespace std;
5
6 template <class T>
7 class mypair {
8 T value1, value2;
9 public:
10 mypair (T first, T second)
11 {value1=first; value2=second;}
12 T module () {return 0;}
13 };
14
15 template <>
16 class mypair <int> {
17 int value1, value2;
18 public:
19 mypair (int first, int second)
20 {
21 value1=first;
22 value2=second;
23 }
24 int module(){ return value1%value2; }
25 };
26
27 /*
28 template <>
29 int mypair<int>::module() {
30 return value1%value2;
31 }
32 */
33
34 int main () {
35 mypair <int> myints (100,75);
36 mypair <float> myfloats (100.0,75.0);
37 cout << myints.module() << '\n';
38 cout << myfloats.module() << '\n';
39 return 0;
40 }
Jonathan Mcdougall wrote: [..] Drop the "template <>". That's the syntax for an explicit instantiation, not for a member function definition.
No, it's the syntax for a full specialization. An explicit
instantiation does not have the angle brackets.
[..]
V
blueblueblue2005 wrote: hi, I solved the problem by comment out the definition at line 27 through line 30, instead, I moved the function definition inside the class definition, and there is no compiler error, but why the compiler does allow me to define the function outside the class definition???
Are you just starting with C++, moving over from Java? That's the C++
way -- declare in one place, define in another. You don't have to put
all your member function implementations into the class definition.
V
> but why [doesn't] the compiler does allow me to define the function outside the class definition???
because you're not listening to Jonathan?
If the class name is
template <> class C<int>
then it is not a template class anymore. The name of that class is C<int>:
void C<int>::f() {
}
sorry, I put my msg before I got Jonathan's post. thanks for all the
help. yeah, I did java programming before, now started c++, and always
interfered by java. thanks a lot again. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Dave Miller |
last post by:
How can you pass a string through a template parameter? I am trying
to avoid using the stream objects and need to pass in class specific
information at creation.
Any information would be...
|
by: Duane Morin |
last post by:
I've inherited an XSL transform that I need to squeeze every last
millisecond out of (since it's running several hundred thousand
times). I've noticed that there are 26 match clauses in the file....
|
by: bjam |
last post by:
Hi, today I was able to abstract out into a separate xsl file a
template that I specifically perform a call templates for, this worked
with import no problem. However, when trying to do the same...
|
by: Alex Buell |
last post by:
This is what I'm trying to compile with GCC 3.4.4 under Linux. The
error I'm getting is:
g++ -s -O3 -Wall class2.cc -o class2
class2.cc:25: error: template-id `uppercase<>' for `char...
|
by: Joseph Turian |
last post by:
I have a class Feature defined, which is a kind of Vocab:
template <class T, unsigned I>
class Vocab : boost::totally_ordered<Vocab<T,I {
public:
Vocab();
Vocab(const T& t);
template<typename...
| |
by: lawrence k |
last post by:
Wierd. Go to this page:
http://www.ihanuman.com/search.php
and search for "yoga"
This query gets run:
SELECT * FROM albums WHERE MATCH(name,description) AGAINST ('yoga')
ORDER BY id DESC
|
by: Alan |
last post by:
Does a template class declaration, like
template <class T>
have to come immediately prior to the declaration of the function,
e.g.,
T do_something (T something)
{ . . . }
that uses it?
|
by: Adam Nielsen |
last post by:
Hi everyone,
Yet another syntax problem that's baffling me with templates. I want to
instantiate a template with a single parameter as per normal, however
the parameter is actually a template...
|
by: ruebezaehler |
last post by:
Hello,
I should separate the definition and declaration of template code. This
works fine for non-specialized templates. But I do not know how to do
this for specialized templates.
Example:
...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |