Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 19th, 2005, 05:19 PM
Itchy
Guest
 
Posts: n/a
Default 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];

};
  #2  
Old July 19th, 2005, 05:19 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: template function within template class

"Itchy" <tobhe067@student.liu.se> wrote...[color=blue]
> 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()[/color]

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.
[color=blue]
>
> /Itchy
>[/color]

/*****************************************/
#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


 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles