template function within template class

Discussion in 'C++' started by Itchy, Aug 12, 2003.

  1. Itchy

    Itchy Guest

    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>::eek:perator[]((bits - pos) % bits);
    }

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

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

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

    class DES
    {
    public:

    //protected:
    void apply_pc1()
    {
    for (int i = 1; i <= 56; i++) newkey = 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];

    };
     
    Itchy, Aug 12, 2003
    #1
    1. Advertising

  2. "Itchy" <> 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>::eek:perator[]((bits - pos) % bits);
    }

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

    Block& operator>>=(size_t pos)
    {
    Block b(*this);
    for (int i = 1; i <= bits; i++) (*this);
    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);
    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
     
    Victor Bazarov, Aug 12, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    490
    Rob Williscroft
    Nov 18, 2004
  2. Replies:
    2
    Views:
    357
  3. christopher diggins
    Replies:
    16
    Views:
    761
    Pete Becker
    May 4, 2005
  4. jrwats
    Replies:
    0
    Views:
    309
    jrwats
    Feb 28, 2010
  5. jrwats
    Replies:
    1
    Views:
    899
    Anthony Williams
    Mar 1, 2010
Loading...

Share This Page