473,395 Members | 1,504 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,395 software developers and data experts.

template function within template class

Hi!

I just don't understand why my compiler complains about this line in
my program (see below). Maybe you can help me?

You can skip all the overloaded operators. I've marked the problem
areas. I get error in function generate_subkeys()

/Itchy

/*****************************************/
#include <bitset>

using namespace std;

template <size_t bits>
class Block : public bitset<bits>
{
public:
bool operator[](size_t pos) const
{
return bitset<bits>::operator[]((bits - pos) % bits);
}

reference operator[](size_t pos)
{
return bitset<bits>::operator[]((bits - pos) % bits);
}

Block& operator<<=(size_t pos)
{
Block b(*this);
for (int i = 1; i <= bits; i++) b[i - pos] = (*this)[i];
return (*this = b);
}

Block& operator>>=(size_t pos)
{
Block b(*this);
for (int i = 1; i <= bits; i++) b[i + pos] = (*this)[i];
return (*this = b);
}

Block operator<<(size_t pos) { return (Block(*this) <<= pos); }
Block operator>>(size_t pos) { return (Block(*this) <<= pos); }

/********* LOOK HERE!! PROBLEM
**********************************/
template <size_t subbits>
Block<subbits> subset(size_t left, size_t right)
{
Block<subbits> b;
for (int i = left; i <= right; i++) b[i - left + 1] = (*this)[i];
return b;
}
};

class DES
{
public:

//protected:
void apply_pc1()
{
for (int i = 1; i <= 56; i++) newkey[i] = key[PC1[i - 1]];
}

void generate_subkeys()
{
Block<28> left;
bool b;
//, right;
for (int r = 1; r <= 16; r++)
{
/********* LOOK HERE!! PROBLEM
**********************************/
// IT CANT FIND MY TEMPLATE FUNCTION. COMPILER SAYS IT RETURNS
BOOL?!?!
left = newkey.subset<28>(1, 28); // <----- ERROR

}
}

private:
Block<64> key;
Block<56> newkey;
Block<28> subkeys[16];

};
Jul 19 '05 #1
1 3492
"Itchy" <to******@student.liu.se> wrote...
I just don't understand why my compiler complains about this line in
my program (see below). Maybe you can help me?

You can skip all the overloaded operators. I've marked the problem
areas. I get error in function generate_subkeys()
As soon as I removed the second operator[] ('reference' was undefined)
and changed all other places were it was used, the code compiles. It
is possible that your compiler is not good enough. Visual C++ v6 is
like that. You should consider getting a better compiler if yours is
VC++ v6 and you want to be able to handle member templates.

/Itchy


/*****************************************/
#include <bitset>

using namespace std;

template <size_t bits>
class Block : public bitset<bits>
{
public:
bool operator[](size_t pos) const
{
return bitset<bits>::operator[]((bits - pos) % bits);
}

Block& operator<<=(size_t pos)
{
Block b(*this);
for (int i = 1; i <= bits; i++) (*this)[i];
return (*this = b);
}

Block& operator>>=(size_t pos)
{
Block b(*this);
for (int i = 1; i <= bits; i++) (*this)[i];
return (*this = b);
}

Block operator<<(size_t pos) { return (Block(*this) <<= pos); }
Block operator>>(size_t pos) { return (Block(*this) <<= pos); }

/********* LOOK HERE!! PROBLEM **********************************/
template <size_t subbits>
Block<subbits> subset(size_t left, size_t right)
{
Block<subbits> b;
for (int i = left; i <= right; i++) (*this)[i];
return b;
}
};

int PC1[10];

class DES
{
public:

//protected:
void apply_pc1()
{
for (int i = 1; i <= 56; i++) key[PC1[i - 1]];
}

void generate_subkeys()
{
Block<28> left;
bool b;
//, right;
for (int r = 1; r <= 16; r++)
{
/********* LOOK HERE!! PROBLEM
**********************************/
// IT CANT FIND MY TEMPLATE FUNCTION. COMPILER SAYS IT RETURNS
BOOL?!?!
left = newkey.subset<28>(1, 28); // <----- ERROR

}
}

private:
Block<64> key;
Block<56> newkey;
Block<28> subkeys[16];

};

int main()
{
DES des;
des.generate_subkeys();

return 0;
}

Victor
Jul 19 '05 #2

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

Similar topics

12
by: Surya Kiran | last post by:
Hi all, I've written a function template. say template <class T> fn (T var) { ... } Is there any way, from within the function, can we check what type of argument we've passed on to the...
5
by: Trevor Lango | last post by:
What is the appropriate syntax for placing a friend function that includes as one of it's parameters a pointer to the class object itself within the template class? I have the following: ...
0
by: Chris F Clark | last post by:
In our C++ project we have some internal bug reporting macros that we use to get useful information when the program does something unexpected. Essentially at the point of the error, we invoke an...
8
by: vpadial | last post by:
Hello, I want to build a library to help exporting c++ functions to a scripting languagge. The scripting language provides a function to register functions like: ANY f0() ANY f1(ANY) ANY...
9
by: Ann Huxtable | last post by:
I have the following code segment - which compiles fine. I'm just worried I may get run time probs - because it looks like the functions are being overloaded by the return types?. Is this Ok: ? ...
5
by: Steve | last post by:
Hi, Does C++ allow the programmer to declare a template with in a template so that a generic function can instantiate the embedded template? For example, could code such as this exist: ...
6
by: Neal | last post by:
Hi All, I used an article on XSLT and XML and creating a TOC written on the MSDN CodeCorner. ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/dncodecorn/html/corner042699.htm However, it did'nt...
12
by: aaragon | last post by:
Hello all. I have a simple question that seems trivial but I can't make it to work. I have a class that takes as a template argument, another class. The idea is as follows: #include...
7
by: aaragon | last post by:
Hi everyone, The idea is quite simple: generate a container with random values in it. For that, I decided to create a class that I called RandomContainer that inherits from a container (with...
21
by: H9XLrv5oXVNvHiUI | last post by:
Hi, I have a question about injecting friend functions within template classes. My question is specific to gcc (version 3.4.5) used in combination with mingw because this code (or at least code...
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: 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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
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 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.