Jump to content
  • Advertisement
Sign in to follow this  
Lavinski

C++ templates

This topic is 3557 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!