template & no matching function for call to

burlen burlen.loring@gmail.com
Tue Feb 23 01:50:00 GMT 2010


Hi,

I'm getting the error that gcc cannot match my templated member function 
call. I'm not seeing where the ambiguity lies, can someone explain why 
g++ doesn't find it?

Here are the error

$g++ minimal.cpp
minimal.cpp: In member function ‘T Value<T>::GetValue() const [with T = 
int]Â’:
minimal.cpp:59: instantiated from here
minimal.cpp:46: error: no matching function for call to 
‘ValueTraits<int>::Access(const ValueUnion&) const’
minimal.cpp:24: note: candidates are: int 
ValueTraits<int>::Access(ValueUnion&) const


Here is the code from minimal.cpp:

#include <iostream>
using std::cerr;
using std::endl;

//=============================================================================
typedef union
{
int Int;
double Double;
}
ValueUnion;

//=============================================================================
template <typename T>
class ValueTraits {};

//=============================================================================
template<>
class ValueTraits<int>
{
public:
int Access(ValueUnion &vu) const { return vu.Int; }
void Assign(ValueUnion &vu, int val){ vu.Int=val; }
};

//=============================================================================
template<>
class ValueTraits<double>
{
public:
double Access(ValueUnion &vu) const { return vu.Double; }
void Assign(ValueUnion &vu, double val){ vu.Double=val; }
};

//=============================================================================
template<typename T>
class Value
{
public:
virtual T GetValue() const { return this->Traits.Access(this->Data); }
virtual void SetValue(T data) { this->Traits.Assign(this->Data,data); }

private:
ValueTraits<T> Traits;
ValueUnion Data;
};

//-----------------------------------------------------------------------------
int main(int argc, char **ragv)
{
Value<int> iv;
iv.SetValue(2);
cerr << "iv=" << iv.GetValue() << endl;

Value<double> dv;
dv.SetValue(2.2);
cerr << "dv=" << dv.GetValue() << endl;

return 0;
}




More information about the Gcc-help mailing list