C++ templates

Started by
5 comments, last by rip-off 15 years, 5 months ago
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
Wise men don't need advice. Fools don't take it. Lavinski - EXtremeSolutions
Advertisement
Moved to For Beginners.
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)
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;
}
Wise men don't need advice. Fools don't take it. Lavinski - EXtremeSolutions
Same change must be made for the other function implementations, of course.
You probably also want to change from using assignment to an initialization list<a>.
[TheUnbeliever]
You will get linker errors on the majority of compilers unless your template implementation is available to the client code when it is being compiled.

This topic is closed to new replies.

Advertisement