DaveR
DaveR

Reputation: 1435

error: invalid conversion from "const ..." to "..." error message when using templates

So I'm trying to figure out how C++ templates work and I'm not having much luck. I have created the following templates:

template<class TValue>
class Value {
public:
    virtual ~Value();

    inline TValue value() const { return _value; }

    virtual int serialize(unsigned char* buffer, int bufferSize) const = 0;

protected:
    Value(TValue value, const ValueType& valueType) : _value(value), _valueType(&valueType) {}

private:
    TValue _value;
    ValueType* _valueType;
};

template<class TValue>
class NumericValue : public Value<TValue> {
protected:
    NumericValue(TValue value, const ValueType& valueType) : Value<TValue>(value, valueType) {}
};

and then I have a class I've created:

class U16Value : public NumericValue<u16> {
public:
    U16Value(u16 value) : NumericValue<u16>(value, ValueType::U16) {}
}

Unfortunately, this won't compile. I get an error on this line:

Value(TValue value, const ValueType& valueType) : _value(value), _valueType(&valueType) {}

That says:

error: invalid conversion from const tnp::ValueType* to tnp::ValueType* [-fpermissive]

Could some tell me why this is happening?

thanks.

Upvotes: 0

Views: 1140

Answers (5)

DaveR
DaveR

Reputation: 1435

I solved the problem by simply having each of the derived classes return their own ValueType object instead of stuffing it in the parent class. That seems to have given me what I was looking for.

Upvotes: 0

kokan
kokan

Reputation: 64

I don't really understand the ValueType, is it exists at all ? But I think you wanted to do something like this:

template<class TValue>
class Value {
public:
    typedef TValue ValueType;
    virtual ~Value();

    inline TValue value() const { return _value; }

    virtual int serialize(unsigned char* buffer, int bufferSize) const = 0;

protected:
    Value(TValue value) : _value(value) {}

private:
    TValue _value;
};

template<class TValue>
class NumericValue : public Value<TValue> {
public:
    typedef typename Value<TValue>::ValueType ValueType;
protected:
    NumericValue(TValue value) : Value<TValue>(value) {}
};

class U16Value : public NumericValue<u16> {
public:
    U16Value(ValueType value) : NumericValue<ValueType>(value) {}
};

Upvotes: 0

Cătălin Pitiș
Cătălin Pitiș

Reputation: 14341

The member _valueType is declared as pointer to non-const object (ValueType*), and the pointer you try to intialize with (&valueType) is a pointer to const object (const ValueType*), since you refer a const reference.

Upvotes: 2

Robadob
Robadob

Reputation: 5349

Your construct takes a const and trys to store it in a none const var. Either remove const from that line or add it to the vars declaration.

const ValueType* _valueType;

Upvotes: 1

Josh Petitt
Josh Petitt

Reputation: 9579

You are trying to assign a const pointer to a non-const pointer.

Change the line to:

const ValueType* _valueType;

Upvotes: 0

Related Questions