473,387 Members | 1,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Problem on template definition

Dear All;

Would you please help me to look at the following case:

//! Rotation.
enum Rotation {
NON_CYCLIC,
CYCLIC
};
//! Rotation.
enum Direction {
LEFT,
RIGHT
};

//! This is a class template of TinyVector
template<class _T, size_t _n>
class TinyVector
{
public:
//! Rotate.
template<Direction _dir, Rotation _rot>
TinyVector& Rotate(size_t n);
}

//! Rotate.
template<class _T, size_t _n>
template<Direction _dir, Rotation _rot>
inline typename TinyVector<_T, _n>& TinyVector<_T, _n>::Rotate(size_t
n)
{
...
return *this;
}

//! Rotate<LEFT, NON_CYCLIC>.
template<class _T, size_t _n>
template<>
inline typename TinyVector<_T, _n>& TinyVector<_T, _n>::Rotate<LEFT,
NON_CYCLIC>(size_t n)
{
...
return *this;
} // <-- Error position

Error C2768: 'TinyVector<_T,_n>::Rotate' : illegal use of explicit
template arguments

I realy appreciate your guys' help. It make me to learn c++ much more
comforably.

Shuisheng

Sep 27 '06 #1
7 1739

shuisheng wrote:
Dear All;

Would you please help me to look at the following case:

//! Rotation.
enum Rotation {
NON_CYCLIC,
CYCLIC
};
//! Rotation.
enum Direction {
LEFT,
RIGHT
};

//! This is a class template of TinyVector
template<class _T, size_t _n>
class TinyVector
{
public:
//! Rotate.
template<Direction _dir, Rotation _rot>
TinyVector& Rotate(size_t n);
}

//! Rotate.
template<class _T, size_t _n>
template<Direction _dir, Rotation _rot>
inline typename TinyVector<_T, _n>& TinyVector<_T, _n>::Rotate(size_t
n)
{
...
return *this;
}

//! Rotate<LEFT, NON_CYCLIC>.
template<class _T, size_t _n>
template<>
Reverse those two template declaration. It becomes okay. Quite
interesting.

template<>
template<class _T, size_t _n>

inline typename TinyVector<_T, _n>& TinyVector<_T, _n>::Rotate<LEFT,
NON_CYCLIC>(size_t n)
{
...
return *this;
} // <-- Error position

Error C2768: 'TinyVector<_T,_n>::Rotate' : illegal use of explicit
template arguments

I realy appreciate your guys' help. It make me to learn c++ much more
comforably.

Shuisheng
Sep 27 '06 #2

shuisheng wrote:
shuisheng wrote:
Dear All;

Would you please help me to look at the following case:

//! Rotation.
enum Rotation {
NON_CYCLIC,
CYCLIC
};
//! Rotation.
enum Direction {
LEFT,
RIGHT
};

//! This is a class template of TinyVector
template<class _T, size_t _n>
class TinyVector
{
public:
//! Rotate.
template<Direction _dir, Rotation _rot>
TinyVector& Rotate(size_t n);
}

//! Rotate.
template<class _T, size_t _n>
template<Direction _dir, Rotation _rot>
inline typename TinyVector<_T, _n>& TinyVector<_T, _n>::Rotate(size_t
n)
{
...
return *this;
}

//! Rotate<LEFT, NON_CYCLIC>.
template<class _T, size_t _n>
template<>

Reverse those two template declaration. It becomes okay. Quite
interesting.

template<>
template<class _T, size_t _n>

inline typename TinyVector<_T, _n>& TinyVector<_T, _n>::Rotate<LEFT,
NON_CYCLIC>(size_t n)
{
...
return *this;
} // <-- Error position

Error C2768: 'TinyVector<_T,_n>::Rotate' : illegal use of explicit
template arguments

I realy appreciate your guys' help. It make me to learn c++ much more
comforably.

Shuisheng
Still WRONG. Help!!!

Sep 27 '06 #3

shuisheng wrote:
Dear All;

Would you please help me to look at the following case:

//! Rotation.
enum Rotation {
NON_CYCLIC,
CYCLIC
};
//! Rotation.
enum Direction {
LEFT,
RIGHT
};

//! This is a class template of TinyVector
template<class _T, size_t _n>
class TinyVector
{
public:
//! Rotate.
template<Direction _dir, Rotation _rot>
TinyVector& Rotate(size_t n);
}

//! Rotate.
template<class _T, size_t _n>
template<Direction _dir, Rotation _rot>
inline typename TinyVector<_T, _n>& TinyVector<_T, _n>::Rotate(size_t
n)
{
...
return *this;
}

//! Rotate<LEFT, NON_CYCLIC>.
template<class _T, size_t _n>
template<>
inline typename TinyVector<_T, _n>& TinyVector<_T, _n>::Rotate<LEFT,
NON_CYCLIC>(size_t n)
{
...
return *this;
} // <-- Error position
You cannot specialize a template member function without first
specializing the class template.
so you need to first specialize the class template for a specific type.
>
Error C2768: 'TinyVector<_T,_n>::Rotate' : illegal use of explicit
template arguments

I realy appreciate your guys' help. It make me to learn c++ much more
comforably.

Shuisheng
Sep 27 '06 #4
shuisheng wrote:
//! Rotation.
enum Rotation * * * * * {
NON_CYCLIC,
CYCLIC
};
//! Rotation.
enum Direction * * * * *{
LEFT,
RIGHT
};

//! This is a class template of TinyVector
template<class _T, size_t _n>
class TinyVector
{
public:
//! Rotate.
template<Direction _dir, Rotation _rot>
TinyVector& Rotate(size_t n);
}
missing ";":
};

that one already causes a lot of confusing error messages to go away.
>
//! Rotate.
template<class _T, size_t _n>
template<Direction _dir, Rotation _rot>
inline typename TinyVector<_T, _n>& TinyVector<_T, _n>::Rotate(size_t n)
ditch the "typename"
{
...
return *this;
}

//! Rotate<LEFT, NON_CYCLIC>.
template<class _T, size_t _n>
template<>
inline typename TinyVector<_T, _n>& TinyVector<_T, _n>::Rotate<LEFT,
NON_CYCLIC>(size_t n)
{
...
return *this;
} * // <-- Error position
Finally, this last one cannot fly at all: as of the current standard,
partial specializations of function templates do not exist in C++. You have
to turn that into a class template. The class could be stateless and just
provide a static function.
You could try something like:

#include <cstddef>

typedef int Direction;
typedef int Rotation;

//! This is a class template of TinyVector
template<class T, size_t N >
class TinyVector;

template < Direction dir, Rotation rot >
struct alg;

template<>
struct alg< 0, 0 {

template<class T, size_t N >
static
TinyVector<T,N& rotate ( TinyVector<T,N& arg ) {
return ( arg );
}

}; // alg;
template<class T, size_t N >
class TinyVector {

public:

template<Direction dir, Rotation rot>
TinyVector& Rotate(size_t n) {
return ( alg<dir,rot>::rotate( *this ) );
}

};


Best

Kai-Uwe Bux
Sep 27 '06 #5
shuisheng wrote:
template<class _T, size_t _n>
Identifiers which begin with an underscore are reserved for the
implementation. Use T and n instead. If you really want to use an
underscore, you can use T_ and n_ .

Nate
Sep 28 '06 #6
Nate Barney wrote:
shuisheng wrote:
>template<class _T, size_t _n>

Identifiers which begin with an underscore are reserved for the
implementation.
Only in the global scope. Of course those that begin with a '_'
and a capital letter (like "_T" here) are always reserved, no
matter what scope.
Use T and n instead. If you really want to use an
underscore, you can use T_ and n_ .
I think many who use the leading underscore think that with it
their code looks cool because it looks more like the "internal
stuff" or the "implementation detail", or some such. Or, maybe
they think it would help avoid naming conflict somewhere down
the road (which it actually doesn't).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 28 '06 #7
Victor Bazarov wrote:
Nate Barney wrote:
>Identifiers which begin with an underscore are reserved for the
implementation.

Only in the global scope. Of course those that begin with a '_'
and a capital letter (like "_T" here) are always reserved, no
matter what scope.
Interesting, I didn't know that. In any case, it's probably best to
eschew leading underscores entirely.

Nate
Sep 28 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Me | last post by:
I am not understanding an aspect of how to implement a class template in a ..cpp file. What I do know is that there are at least two problems with my understanding of how to accomplish the...
11
by: Georg Teichtmeister | last post by:
Hello! We are developing a math - library for realtime applications and want to use some given mathlibraries as base(ipp, MTL, .. ). Our library is a wrapper for those and you should be able to...
0
by: tyousaf | last post by:
Hi i am new to mysql and mysql++, i have installed mysql server, it is running fine. i also installed "mysql++-1.7.9gcc3.2-2.i386.rpm" (i have gcc 3.3) , first of all as the readme file says to do...
7
by: Lionel B | last post by:
Greetings. The following code compiles ok and does what I'd expect it to do: ---------- START CODE ---------- // test.cpp
2
by: franklini | last post by:
hello people i. can anybody help me, i dont know what is wrong with this class. it has something to do with the me trying to override the input output stream. if i dont override it, it works fine....
14
by: Murkland | last post by:
Hi. I have a problem with a template class that I'm trying to inherit from. The code for the base class is: template <class T> class BaseClass { public: BaseClass(); ~BaseClass();
1
by: nyl2002 | last post by:
I have written the following very short template class in testclass.h: template<typename C,typename T> class TestClass { public: TestClass() {}; ~TestClass(); private: T testFunction(T...
9
by: Jerome Durand | last post by:
Hello, I'm trying to write something along the following lines but I cannot get this to compile. template <typename derivedstruct Base { typedef typename derived::valueType valueType;...
7
by: StephQ | last post by:
First of all: distinction of keywords typename and class in template arguments. Accoarding to a post in a well known moderated group: "There are three possibilities for template arguments: 1)...
6
by: Gaijinco | last post by:
I'm trying to do a template class Node. My node.hpp is: #ifndef _NODE_HPP_ #define _NODE_HPP_ namespace com { namespace mnya { namespace carlos { template <typename T>
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
marktang
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,...
0
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.