Hi there,
I am learning template programming. When testing the partial
specialization, I have some probelms
Here is a full templated class
template <typename T, int n>
class CT
{
public:
T data[n][100][100];
CT() {...}
const T& get(int k, int i, int j)
{
return data[k][i][j];
}
};
I have a partial specialization verion as follow
template <typename T>
class CT<T, 1>
{
public:
T data[1][100][100];
const T& get(int i, int j)
{
return data[0][i][j];
}
};
In main, I have
int main(void)
{
CT<double, 2> ct1;
CT<double, 1> ct2;
cout << ct1.get(1, 5, 8) << endl; // OK! return what I want
cout << ct2.get(5, 8) << endl; // also OK
cout << ct2.get(0, 5, 8) << endl; // ERROR !!!
}
I wonder how this error come ! Both ct1 and ct2 come from a same class
except the different template parameter, why compiler complian no
get(k, i, j) is defined in partial specialization class? If I want a
partial specialization class with all members available, should I
rewrite all the code ?
Thanks in advance 4 1477 wa***@wakun.com wrote: Hi there, I am learning template programming. When testing the partial specialization, I have some probelms
Here is a full templated class template <typename T, int n> class CT { public: T data[n][100][100];
CT() {...}
const T& get(int k, int i, int j) { return data[k][i][j]; } };
I have a partial specialization verion as follow
template <typename T> class CT<T, 1> { public: T data[1][100][100];
const T& get(int i, int j) { return data[0][i][j]; } };
In main, I have
int main(void) { CT<double, 2> ct1; CT<double, 1> ct2;
cout << ct1.get(1, 5, 8) << endl; // OK! return what I want cout << ct2.get(5, 8) << endl; // also OK cout << ct2.get(0, 5, 8) << endl; // ERROR !!! }
I wonder how this error come ! Both ct1 and ct2 come from a same class except the different template parameter, why compiler complian no get(k, i, j) is defined in partial specialization class? If I want a partial specialization class with all members available, should I rewrite all the code ?
Thanks in advance
They are different classes; in fact very differentcout << ct2.get(0, 5, 8) << endl; // ERROR !!!
you specialized class doesn't have an overload of this method that
takes 3 params.
look at faq for more clarification wa***@wakun.com wrote: Hi there, I am learning template programming. When testing the partial specialization, I have some probelms If I want a partial specialization class with all members available, should I rewrite all the code ?
Yes.
Jonathan
Jonathan Mcdougall wrote: wa***@wakun.com wrote: Hi there, I am learning template programming. When testing the partial specialization, I have some probelms If I want a partial specialization class with all members available, should I rewrite all the code ?
Yes.
OK. This time I consider inheritance
template <typename T, int n>
class Base
{
public:
T data[n][100][100];
Base() {...}
void show(void)
{
cout << "Hi!" << endl;
}
const T& get(int k, int i, int j)
{
return data[k][i][j];
}
const T& get2(int k, int i, int j)
{
return data[k][i][j];
}
};
template <typename T, int n>
class CT : public Base<T, n>
{
public:
CT() :Base<T, n> {...}
};
template <typename T>
class CT<T, 1> : public Base<T, 1>
{
public:
CT<T,1>() :Base<T, 1> {...}
const T& get2(int i, int j)
{
return data[0][i][j];
}
};
// main
int main(void)
{
CT<double, 2> ct1;
CT<double, 1> ct2;
cout << ct1.get(1, 2, 3) << endl; // OK, of course
cout << ct1.show() << endl; // OK, of course
cout << ct2.show() << endl; // No problem, show is defined
in Base
cout << ct2.get(0, 1, 2) << endl; // OK. get is kept in both
CT<double, n> and CT<double, 1>
cout << ct2.get2(3, 2) << endl; // OK. overload function
ctou << ct2.get2(0, 1, 2) << endl; // ERROR!!! No such function!?
return 0;
}
Why compiler cannot find get2? wa***@wakun.com wrote: Hi there, I am learning template programming. When testing the partial specialization, I have some probelms
I wonder how this error come ! Both ct1 and ct2 come from a same class except the different template parameter, why compiler complian no get(k, i, j) is defined in partial specialization class? If I want a partial specialization class with all members available, should I rewrite all the code ?
While you certainly have the partially-specialized class template be
more-or-less a duplicate of the general class template, doing so
creates a maintenance hassle, since the two class templates need to be
maintained in parallel.
A simpler approach is to declare the get method that accepts three
parameters in the general template, but provide an implementation only
in a full specialization:
#include <iostream>
using std::cout;
using std::endl;
template <class T, int N>
class CT
{
public:
T data[N][100][100];
CT() {}
const T& get(int k, int i, int j)
{
return data[k][i][j];
}
const T& get(int i, int j); // not implemented here
};
template <>
const double& CT<double, 1>::get(int i, int j)
{
return data[0][i][j];
}
int main()
{
CT<double, 2> ct1;
CT<double, 1> ct2;
cout << ct1.get(1, 5, 8) << endl; // OK! return what I want
cout << ct2.get(5, 8) << endl; // also OK
cout << ct2.get(0, 5, 8) << endl; // OK
}
Greg This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: nospam |
last post by:
Ok, 3rd or is it the 4th time I have asked this question on Partial Types,
so, since it seems to me that Partial Types is still in the design or
development stages at Microsoft, I am going to ask...
|
by: CoolPint |
last post by:
I would be grateful if someone could point out if I am understanding
correctly and suggest ways to improve. Sorry for the long message and
I hope you will kindly bear with it. I have to make it...
|
by: Gomaw Beoyr |
last post by:
Two question about the "partial classes" (in the next wersion of
..NET).
Question 1
==========
Will partial classes (in the next version of C#) have to be
declared "partial" in ALL places.
...
|
by: pawel.pabich |
last post by:
Hajo,
I would like to have 2 my own partial classes.
For example:
Default.aspx.cs
Default2.aspx.cs
and they both will relate to
Default.aspx page.
|
by: ptass |
last post by:
Hi
In asp.net 2.0 an aspx files .cs file is a partial class and all works fine,
however,
I thought I’d be able to create another class file, call it a partial class
and have
that compile and...
|
by: chungiemo |
last post by:
Hi
thought I would do another thread as this one is a bit different from the previous problem
I am looking for a solution to the relating problem
Comparing 2 access databases with 2 tables,...
|
by: SAL |
last post by:
Hello,
I would like to be able to set the WHERE clause of a select statement on the
fly. I have a DataAccess layer designed using the DataSet designer and a
BusinessLogic layer using classes. The...
|
by: JDeats |
last post by:
So I have a class that spans over two partial classes in code, here's
an example (do not read much into this, the code is of no practical
use, this is just a simple example of where my confusion...
|
by: hcaptech |
last post by:
This is my Test.can you help me ?
1.Which of the following statement about C# varialble is incorrect ?
A.A variable is a computer memory location identified by a unique name
B.A variable's name...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |