N
neildferguson
I am using templates with a little project I am working on. My compiler (GCC)
is finding a particular construct ambiguous. Can anyone suggest something I
might change in the declaration of class Length so that I can use operator+ the
way I'd like?
//=========================================
// File lentest.h:
#ifndef FDIMENS_LENGTH_INCL
#define FDIMENS_LENGTH_INCL
class EU {};
class MT : public EU {};
class KM : public EU {};
class FT : public EU {};
class YD : public EU {};
template <class U>
class Length
{
public :
Length<U>()
{
}
Length<U>(double inUnits)
{
}
template <class O>
Length<U>(const Length<O> &other)
{
}
template <class O>
Length<U> &operator=(const Length<O> &other)
{
return *this;
}
template <class O>
friend Length<U> operator+(const Length<U> &lhs, const Length<O>
&rhs)
{
Length<U> ret;
return ret;
}
template <class O>
friend Length<U> operator+(double lhs, const Length<O> &rhs)
{
Length<U> ret;
return ret;
}
operator double() const { return m_lenUnits; }
private :
static double m_m2u;
static double m_u2m;
};
template<> double Length<MT>::m_m2u = 1.0;
template<> double Length<MT>::m_u2m = 1.0;
template<> double Length<KM>::m_m2u = 0.001;
template<> double Length<KM>::m_u2m = 1000.0;
template<> double Length<FT>::m_m2u = 3.28083986538;
template<> double Length<FT>::m_u2m = 0.3048;
template<> double Length<YD>::m_m2u = (3.28083986538/3.0);
template<> double Length<YD>::m_u2m = (0.3048*3.0);
#endif // FDIMENS_LENGTH_INCL
//=============================================
// File lentest.cpp
// lentest.cpp : Defines the entry point for the console application.
//
#include <stdio.h>
#include "lentest.h"
int main()
{
Length<FT> ft1(34.0);
Length<FT> ft2 = (Length<FT>)27.0 + ft1;
#if 0 // this line is ambiguous
Length<FT> ft3 = 27.0 + ft2;
#endif
return 0;
}
is finding a particular construct ambiguous. Can anyone suggest something I
might change in the declaration of class Length so that I can use operator+ the
way I'd like?
//=========================================
// File lentest.h:
#ifndef FDIMENS_LENGTH_INCL
#define FDIMENS_LENGTH_INCL
class EU {};
class MT : public EU {};
class KM : public EU {};
class FT : public EU {};
class YD : public EU {};
template <class U>
class Length
{
public :
Length<U>()
{
}
Length<U>(double inUnits)
{
}
template <class O>
Length<U>(const Length<O> &other)
{
}
template <class O>
Length<U> &operator=(const Length<O> &other)
{
return *this;
}
template <class O>
friend Length<U> operator+(const Length<U> &lhs, const Length<O>
&rhs)
{
Length<U> ret;
return ret;
}
template <class O>
friend Length<U> operator+(double lhs, const Length<O> &rhs)
{
Length<U> ret;
return ret;
}
operator double() const { return m_lenUnits; }
private :
static double m_m2u;
static double m_u2m;
};
template<> double Length<MT>::m_m2u = 1.0;
template<> double Length<MT>::m_u2m = 1.0;
template<> double Length<KM>::m_m2u = 0.001;
template<> double Length<KM>::m_u2m = 1000.0;
template<> double Length<FT>::m_m2u = 3.28083986538;
template<> double Length<FT>::m_u2m = 0.3048;
template<> double Length<YD>::m_m2u = (3.28083986538/3.0);
template<> double Length<YD>::m_u2m = (0.3048*3.0);
#endif // FDIMENS_LENGTH_INCL
//=============================================
// File lentest.cpp
// lentest.cpp : Defines the entry point for the console application.
//
#include <stdio.h>
#include "lentest.h"
int main()
{
Length<FT> ft1(34.0);
Length<FT> ft2 = (Length<FT>)27.0 + ft1;
#if 0 // this line is ambiguous
Length<FT> ft3 = 27.0 + ft2;
#endif
return 0;
}