i get this error in Visual studio 2008:
Error 1 error C2664: 'BaseUtil::Type::CDouble::CDouble(const BaseUtil::Type::CDouble &)' : cannot convert parameter 1 from 'boost::icl::no_type' to 'const BaseUtil::Type::CDouble &'
Here my class interface:
class CDouble
{
public:
CDouble();
CDouble(const CDouble& _obj);
CDouble(const double& _val);
bool operator==(const CDouble& _obj) const;
bool operator==(const double& _obj) const;
bool operator!=(const CDouble& _obj) const;
bool operator<=(const CDouble& _obj) const;
bool operator>=(const CDouble& _obj) const;
bool operator< (const CDouble& _obj) const;
bool operator> (const CDouble& _obj) const;
CDouble& operator= (const CDouble& _obj);
CDouble& operator+=(const CDouble& _obj);
CDouble& operator-=(const CDouble& _obj);
const CDouble operator+(const CDouble& _obj) const;
const CDouble operator-(const CDouble& _obj) const;
const double operator/(const CDouble& _obj) const;
CDouble& operator= (double _value);
CDouble& operator+=(double _value);
CDouble& operator-=(double _value);
CDouble& operator*=(double _value);
CDouble& operator/=(double _value);
const CDouble operator+(double _value) const;
const CDouble operator-(double _value) const;
const CDouble operator*(double _value) const;
const CDouble operator/(double _value) const;
operator double() const {return m_value;}
private:
CDouble& operator*=(const CDouble& _obj);
const CDouble operator*(const CDouble& _obj) const;
CDouble& operator/=(const CDouble& _obj);
double m_value;
};
The code that trigger the compile error:
template <class BoundType>
class Interval
{
public:
BoundType Length() const
{
return boost::icl::length(
boost::icl::construct<boost::icl::interval<BoundType>::type>(m_LowerBound, m_UpperBound, m_IntervalType())
);
}
private:
BoundType m_LowerBound, m_UpperBound;
typedef boost::icl::interval_bounds (*IntervalType)();
IntervalType m_IntervalType;
}
int main()
{
Interval<CDouble> typeDouble(-1.0, 1.0);
typeDouble.Length(); //<-- COMPILE ERROR
}
I don't understand the error and don't know how to solve it.
It work wells with basic type(int, double, ..)
Anyone can help ?
Here's the length fonction from boost 1.52 header files:
template<class Type>
inline typename boost::enable_if<is_continuous_interval<Type>,
typename difference_type_of<interval_traits<Type> >::type>::type
length(const Type& object)
{
typedef typename difference_type_of<interval_traits<Type> >::type DiffT;
return icl::is_empty(object) ? identity_element<DiffT>::value()
: upper(object) - lower(object);
}
Found in the file: boost\icl\type_traits\difference_type_of.hpp
template <class Type>
struct get_difference_type<Type, false, false>
{
typedef no_type type;
};
So I'm assuming that the boost header files defaut implementation for a type that support difference numerical operator is no_type.
What must be done, is to provide, at compile time, a definition of a difference type that match one of your contructor. Ie, the contructor copy for instance is your case.
Although, your type seems like a wapper on a numeric value, maybe boost header files doesn't get it. Please test this snippet in one of your header files, out of proprietary namespaces.
#include <boost_1_52_0\boost\icl\type_traits\is_numeric.hpp>
namespace boost{ namespace icl
{
template <>
struct is_numeric<CDouble>
{
typedef is_numeric type;
BOOST_STATIC_CONSTANT(bool, value = true );
};
} }
If it doesn't works as is, the trick is to tell to boost that your type has a diffence type (a CDouble) so that copy constructor does works.
Thank you for your answer but i used this instead:
namespace std
{
template <>
class numeric_limits<BaseUtil::Type::CDouble> : public numeric_limits<double>
{
};
}
Related
Let's consider class like that with less than operator:
struct Test
{
int value;
constexpr bool operator<(const Test& p_rhs) const
{
return value < p_rhs.value;
}
};
I would like to generate bool operator>(const Test&, const Test&),bool operator<=(const Test&, const Test&) and bool operator>=(const Test&, const Test&) without using CRTP(or any other inheritance) on Test class on generic way, that could be also used on other classes that have bool operator<(const T&,const T&). I tried sth like that:
template<typename Lhs, typename Rhs = Lhs>
struct GenerateOtherComparisonsFromLessThan
{
constexpr friend bool operator>(const Rhs& p_rhs, const Lhs& p_lhs)
{
return p_lhs < p_rhs;
}
//and rest of other comparison operators
};
struct InstantiateTestComparisons : GenerateOtherComparisonsFromLessThan<Test>
{};
But it looks like these overloads are not taken(at least on gcc 8.3 on ideone).
I am getting the compiler error undefined reference to when attempting to inline operator!= as a friend function.
Here is an example:
// color.hpp
class Color
{
friend bool operator==(const Color& lhs, const Color& rhs);
inline friend bool operator!=(const Color& lhs, const Color& rhs);
};
// color.cpp
bool operator==(const Color& lhs, const Color& rhs)
{
}
inline bool operator!=(const Color& lhs, const Color& rhs)
{
}
I cannot implement the operators in the header file, as this creates multiple definition errors.
I am compiling with --std=c++11, g++ 5.2.
Remove the inline from the class definition.
After the class definition, in the header file, add the following:
inline bool
operator!=(const Color& lhs, const Color& rhs)
{
return !(lhs == rhs);
}
Remove the definition in the source file.
I'm trying to implement math library but I'm stuck on exporting. I have a template class for 2-dimensional vector:
vector2.h:
template <typename T>
class GE_API Vector2
{
public:
T x;
T y;
// Indexation
T& operator [] (const size_t i);
const T& operator [] (const size_t i) const;
bool operator == (const Vector2& v) const;
bool operator != (const Vector2& v) const;
// Negation
const Vector2 operator - () const;
// Assignement
const Vector2& operator = (const Vector2& v);
const Vector2& operator += (const Vector2& v);
const Vector2& operator -= (const Vector2& v);
template <typename S>
const Vector2& operator *= (const S& s);
template <typename S>
const Vector2& operator /= (const S& s);
const Vector2 operator + (const Vector2& v) const;
const Vector2 operator + (const T& s) const;
const Vector2 operator - (const Vector2& v) const;
const Vector2 operator - (const T& s) const;
template <typename S>
const Vector2 operator * (const S& s) const;
const Vector2 operator * (const Vector2& v) const;
template <typename S>
const Vector2 operator / (const S& s) const;
const Vector2 operator / (const Vector2& v) const;
};
template <typename T>
GE_API const T dot(const Vector2<T>& a, const Vector2<T>& b);
template <typename T>
GE_API const T length(const Vector2<T>& v);
//..and other functionality
#include <vector2.inl>
Definitions of operators and functions are in separate file vector2.inl which is included in header file. GE_API is standard dllimport/export macro. Problem is that when I try to export this class and functions defined in vector2.h header file I'm getting errors on definitions of operators that definition of dllimport function is not allowed. Why is that and how to fix this?
I think in this case you don't need to specify dllimport/export for your class.
Because all source code will be avaliable to the user and only anoe thing that he heeds is to include vactor2.h header into his project.
Here is the .hpp file:
template<typename T>
LinkedQueue<T> operator=(const LinkedQueue<T> & lhs, const LinkedQueue<T> & rhs)
{
m_data = rhs.m_data;
m_next = rhs.m_next;
}
The error says that first line must be a nonstatic member function. Here is the class it is in:
template<typename T>
class LinkedQueue:public AbstractQueue<T>
{
public:
T m_data;
LinkedQueue *m_next;
LinkedQueue<T> operator=(const LinkedQueue<T> & rhs);
LinkedQueue();
void clear();
void enqueue(T x);
void dequeue();
const T& front() const;
bool isEmpty() const;
};
Any idea as to what silly thing I am doing wrong?
You should add a class qualifier to the function definition, and remove the unused lhs parameter:
template<typename T>
LinkedQueue<T>& LinkedQueue::operator=(const LinkedQueue<T> & rhs)
// ^--- & should be added to the declaration, too
{
m_data = rhs.m_data;
m_next = rhs.m_next;
return *this;
}
you should write look like this;
template<typename T>
class LinkedQueue:public AbstractQueue<T>
{
public:
T m_data;
LinkedQueue *m_next;
LinkedQueue<T> & operator=(const LinkedQueue<T> & rhs)
{
if (this != &rhs)
{
m_data = rhs.m_data;
m_next = rhs.m_next;
}
return *this;
}
LinkedQueue();
void clear();
void enqueue(T x);
void dequeue();
const T& front() const;
bool isEmpty() const;
};
I'm pretty much out of ideas now with porting a big bunch of old C++ code from MS Visual C++ 7.0 to iOS 4 iPhone g++ 4.2.1 compiler. I get some ambiquity errors compiling this:
complex_d* cp;
complex_d qSt;
double zi;
// complex_d += complex_d * double
*cp += qSt * dVal; // ISO C++ says that these are ambiguous
with the class definition of complex_d as:
#include <math.h>
#include "CObject.h" // emulated MFC class, used for some types like BOOL
#include "CString.h" // emulated MFC class, also needed for some types not on iOS
// interface for complex calculations
//
/////////////////////////////////////////////////////////////////////////////
class polar_d; // forward declaration
class complex_d
{
// attributes
protected:
double re;
double im;
// construction
public:
complex_d(double re = 0, double im = 0);
complex_d(const complex_d& x);
virtual ~complex_d() { };
// implementation
public:
double real(void) const;
double imag(void) const;
double& setReal(void); // needed because we don't have Serialize() here
double& setImag(void); // as above
double Abs(void) const;
double Phi(void) const;
complex_d Conjugate(void);
polar_d Polar(void);
BOOL IsZero(void) const;
BOOL IsReal(void) const;
BOOL IsImag(void) const;
complex_d& operator=(const complex_d& rhs);
complex_d& operator+=(const complex_d& rhs);
complex_d& operator-=(const complex_d& rhs);
complex_d& operator*=(const complex_d& rhs);
complex_d& operator/=(const complex_d& rhs);
complex_d operator+(const complex_d& rhs);
complex_d operator-(const complex_d& rhs);
complex_d operator*(const complex_d& rhs); // ambiguous error here...
complex_d operator/(const complex_d& rhs);
complex_d operator-(void); // unary
complex_d& operator=(const double& rhs);
friend complex_d operator+(const complex_d& lhs, double rhs);
friend complex_d operator+(double lhs, const complex_d& rhs);
friend complex_d operator-(const complex_d& lhs, double rhs);
friend complex_d operator-(double lhs, const complex_d& rhs);
friend complex_d operator*(const complex_d& lhs, double rhs); // ... and here also ambigous
friend complex_d operator*(double lhs, const complex_d& rhs);
friend complex_d operator/(const complex_d& lhs, double rhs);
friend complex_d operator/(double lhs, const complex_d& rhs);
friend BOOL operator==(const complex_d& lhs, double rhs);
friend BOOL operator==(double lhs, const complex_d& rhs);
friend BOOL operator!=(const complex_d& lhs, double rhs);
friend BOOL operator!=(double lhs, const complex_d& rhs);
friend BOOL operator==(const complex_d& lhs, const complex_d& rhs);
friend BOOL operator!=(const complex_d& lhs, const complex_d& rhs);
};
The two operators in question are marked as ambigous but I don't see why. Originally this class was written as a template which in fact only was instantiated with double type. So I de-templated the complex_d class which results in the definition above. It compiled w/out errors and warnings in MSC environment using MS Visual C++ .NET 2002 but I get these ambiguity errors with g++ 4.2.1 now.
I'm quite long off writing code with overloading operators in C++ and I experimented a lot rewriting the two definitions of the * operators. The main problem is I don't understand why this is ambiguous. For:
qSt * dVal
a complex_d has to be multiplied with a double variable value and the result has to be returned as complex_d. Therefore the friend operator * has to be evaluated. When I replace the operator
friend complex_d operator*(const complex_d& lhs, double rhs);
with
complex_d operator*(double rhs);
I get another error telling me that a class member or enum is needed as parameter. It is also not possible to omit the second operator in question because it is also needed at another place in the code.
Is anyone out there who can tell me how to get out of this dilemma?
I see two ways to fix this (there's probably more):
Add explicit to the constructor:
explicit complex_d(double re = 0, double im = 0);
Remove the friend operator*().
The C++ std::lib went with solution #2 for std::complex.