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>(doubl e inUnits)
{
}
template <class O>
Length<U>(const Length<O&other)
{
}
template <class O>
Length<U&operat or=(const Length<O&other)
{
return *this;
}
template <class O>
friend Length<Uoperato r+(const Length<U&lhs, const Length<O>
&rhs)
{
Length<Uret;
return ret;
}
template <class O>
friend Length<Uoperato r+(double lhs, const Length<O&rhs)
{
Length<Uret;
return ret;
}
operator double() const { return m_lenUnits; }
private :
static double m_m2u;
static double m_u2m;
};
template<double Length<MT>::m_m 2u = 1.0;
template<double Length<MT>::m_u 2m = 1.0;
template<double Length<KM>::m_m 2u = 0.001;
template<double Length<KM>::m_u 2m = 1000.0;
template<double Length<FT>::m_m 2u = 3.28083986538;
template<double Length<FT>::m_u 2m = 0.3048;
template<double Length<YD>::m_m 2u = (3.28083986538/3.0);
template<double Length<YD>::m_u 2m = (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<FTft1(34 .0);
Length<FTft2 = (Length<FT>)27. 0 + ft1;
#if 0 // this line is ambiguous
Length<FTft3 = 27.0 + ft2;
#endif
return 0;
}
----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
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>(doubl e inUnits)
{
}
template <class O>
Length<U>(const Length<O&other)
{
}
template <class O>
Length<U&operat or=(const Length<O&other)
{
return *this;
}
template <class O>
friend Length<Uoperato r+(const Length<U&lhs, const Length<O>
&rhs)
{
Length<Uret;
return ret;
}
template <class O>
friend Length<Uoperato r+(double lhs, const Length<O&rhs)
{
Length<Uret;
return ret;
}
operator double() const { return m_lenUnits; }
private :
static double m_m2u;
static double m_u2m;
};
template<double Length<MT>::m_m 2u = 1.0;
template<double Length<MT>::m_u 2m = 1.0;
template<double Length<KM>::m_m 2u = 0.001;
template<double Length<KM>::m_u 2m = 1000.0;
template<double Length<FT>::m_m 2u = 3.28083986538;
template<double Length<FT>::m_u 2m = 0.3048;
template<double Length<YD>::m_m 2u = (3.28083986538/3.0);
template<double Length<YD>::m_u 2m = (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<FTft1(34 .0);
Length<FTft2 = (Length<FT>)27. 0 + ft1;
#if 0 // this line is ambiguous
Length<FTft3 = 27.0 + ft2;
#endif
return 0;
}
----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Comment