Sign in to follow this  
Lavinski

C++ templates

Recommended Posts

Hey, I'm trying to learn C++ templates, i'm also failr new to C++ Could you take a look at this and see if you can see anything i'm missing. What I'm trying to do is pass a template type to the base class which uses it as a template type as well. Thanks.
// Tile: Key Value Pair | HeaderFile
#ifndef Gard_SX_Comparable
#define Gard_SX_Comparable

namespace SXFramework
{
    namespace Collections
    {
        template <class Type>
        class Comparable
        {
        public:
            //Comparable();
            virtual int CompareTo(Type comparable) = 0;
        };
    }
}

#endif

// Tile: Key Value Pair | HeaderFile
#ifndef Gard_SX_KeyValuePair
#define Gard_SX_KeyValuePair

#include "comparable.h"

namespace SXFramework
{
    namespace Collections
    {
        template <class KeyType, class ValueType>
        class KeyValuePair : public Comparable<KeyType>
        {
        public:
            // Constructors
            KeyValuePair(KeyType key, ValueType value);

            // Destructors
            ~KeyValuePair();

            KeyType     getKey() const;
            ValueType   getValue() const;
            void        setVlaue(ValueType value);

        private:
            KeyType key;
            ValueType value;
        };
    }
}

#endif

// Tile: Key Value Pair | Code File

#include "KeyValuePair.h"

namespace SXFramework
{
    namespace Collections
    {
        KeyValuePair::KeyValuePair(KeyType key, ValueType value) // <-- Errors
        {
            this->key = key;
            this->value = value;
        }

        KeyType KeyValuePair::getKey() const
        {
            return this->key;
        }

        ValueType KeyValuePair::getValue() const
        {
            return this->value;
        }

        void KeyValuePair::setVlaue(ValueType value)
        {
            this->value = value;
        }

        int KeyValuePair::CompareTo(KeyValuePair value)
        {
            if (this > value) return 1;
            else if (this < value) return -1;
            else return 0;
        }
    }
}

error C2955: use of class template requires template argument list error C2955: use of class template requires template argument list error C2065: 'KeyType' : undeclared identifier error C2146: syntax error : missing ')' before identifier 'key' error C2761: KeyValuePair<KeyType,ValueType>::KeyValuePair(KeyType,ValueType)' : member function redeclaration not allowed

Share this post


Link to post
Share on other sites
Since you are no longer in the class declaration scope, you have to change

KeyValuePair::KeyValuePair (KeyType key, ValueType value)

to

template <class KeyType, class ValueType>
KeyValuePair<Keytype, ValueType>::KeyValuePair (KeyType key, ValueType value)

Share this post


Link to post
Share on other sites
Thanks Mattijs2 that did it.

Quote:

template <class KeyType, class ValueType>
KeyValuePair<KeyType, ValueType>::KeyValuePair(KeyType key, ValueType value)
{
this->key = key;
this->value = value;
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this