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.
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'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.
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>
{
};
}
I've never implemented STL-like iterators and I try to understand how to implement a very basic thing based on pointers. Once I will have this class I will be able to modify it to do more complicated things. Consequently, this is a first step, and I need it to be rock solid to understand how to write my own iterators (without boost).
I have written the following code and I know that there are errors in it. Can you help me to design correctly a Random Access Iterator class inspired from that :
template<Type> class Container<Type>::Iterator : public std::iterator<random_access_iterator_tag, Type>
{
// Lifecycle:
public:
Iterator() : _ptr(nullptr) {;}
Iterator(Type* rhs) : _ptr(rhs) {;}
Iterator(const Iterator &rhs) : _ptr(rhs._ptr) {;}
// Operators : misc
public:
inline Iterator& operator=(Type* rhs) {_ptr = rhs; return *this;}
inline Iterator& operator=(const Iterator &rhs) {_ptr = rhs._ptr; return *this;}
inline Iterator& operator+=(const int& rhs) {_ptr += rhs; return *this;}
inline Iterator& operator-=(const int& rhs) {_ptr -= rhs; return *this;}
inline Type& operator*() {return *_ptr;}
inline Type* operator->() {return _ptr;}
inline Type& operator[](const int& rhs) {return _ptr[rhs];}
// Operators : arithmetic
public:
inline Iterator& operator++() {++_ptr; return *this;}
inline Iterator& operator--() {--_ptr; return *this;}
inline Iterator& operator++(int) {Iterator tmp(*this); ++_ptr; return tmp;}
inline Iterator& operator--(int) {Iterator tmp(*this); --_ptr; return tmp;}
inline Iterator operator+(const Iterator& rhs) {return Iterator(_ptr+rhs.ptr);}
inline Iterator operator-(const Iterator& rhs) {return Iterator(_ptr-rhs.ptr);}
inline Iterator operator+(const int& rhs) {return Iterator(_ptr+rhs);}
inline Iterator operator-(const int& rhs) {return Iterator(_ptr-rhs);}
friend inline Iterator operator+(const int& lhs, const Iterator& rhs) {return Iterator(lhs+_ptr);}
friend inline Iterator operator-(const int& lhs, const Iterator& rhs) {return Iterator(lhs-_ptr);}
// Operators : comparison
public:
inline bool operator==(const Iterator& rhs) {return _ptr == rhs._ptr;}
inline bool operator!=(const Iterator& rhs) {return _ptr != rhs._ptr;}
inline bool operator>(const Iterator& rhs) {return _ptr > rhs._ptr;}
inline bool operator<(const Iterator& rhs) {return _ptr < rhs._ptr;}
inline bool operator>=(const Iterator& rhs) {return _ptr >= rhs._ptr;}
inline bool operator<=(const Iterator& rhs) {return _ptr <= rhs._ptr;}
// Data members
protected:
Type* _ptr;
};
Thank you very much.
Your code has the following issues:
You do not follow the Rule of Three/Five. The best option in your situation is not to declare any custom destructors, copy/move constructors or copy/move assignment operators. Let's follow so called Rule of Zero.
Iterator(Type* rhs) could be private and the Container could be marked as Iterator's friend, but that's not strictly necessary.
operator=(Type* rhs) is a bad idea. That's not what type safety is about.
Post-in(de)crementation should return Iterator, not Iterator &.
Adding two iterators has no meaning.
Subtracting two iterators should return a difference, not a new iterator.
You should use std::iterator<std::random_access_iterator_tag, Type>::difference_type instead of const int &.
If a method does not modify an object, it should be marked const.
Useful resource: RandomAccessIterator # cppreference.com
Here is a fixed version of your code:
template<typename Type>
class Container<Type>::Iterator : public std::iterator<std::random_access_iterator_tag, Type>
{
public:
using difference_type = typename std::iterator<std::random_access_iterator_tag, Type>::difference_type;
Iterator() : _ptr(nullptr) {}
Iterator(Type* rhs) : _ptr(rhs) {}
Iterator(const Iterator &rhs) : _ptr(rhs._ptr) {}
/* inline Iterator& operator=(Type* rhs) {_ptr = rhs; return *this;} */
/* inline Iterator& operator=(const Iterator &rhs) {_ptr = rhs._ptr; return *this;} */
inline Iterator& operator+=(difference_type rhs) {_ptr += rhs; return *this;}
inline Iterator& operator-=(difference_type rhs) {_ptr -= rhs; return *this;}
inline Type& operator*() const {return *_ptr;}
inline Type* operator->() const {return _ptr;}
inline Type& operator[](difference_type rhs) const {return _ptr[rhs];}
inline Iterator& operator++() {++_ptr; return *this;}
inline Iterator& operator--() {--_ptr; return *this;}
inline Iterator operator++(int) const {Iterator tmp(*this); ++_ptr; return tmp;}
inline Iterator operator--(int) const {Iterator tmp(*this); --_ptr; return tmp;}
/* inline Iterator operator+(const Iterator& rhs) {return Iterator(_ptr+rhs.ptr);} */
inline difference_type operator-(const Iterator& rhs) const {return _ptr-rhs.ptr;}
inline Iterator operator+(difference_type rhs) const {return Iterator(_ptr+rhs);}
inline Iterator operator-(difference_type rhs) const {return Iterator(_ptr-rhs);}
friend inline Iterator operator+(difference_type lhs, const Iterator& rhs) {return Iterator(lhs+rhs._ptr);}
friend inline Iterator operator-(difference_type lhs, const Iterator& rhs) {return Iterator(lhs-rhs._ptr);}
inline bool operator==(const Iterator& rhs) const {return _ptr == rhs._ptr;}
inline bool operator!=(const Iterator& rhs) const {return _ptr != rhs._ptr;}
inline bool operator>(const Iterator& rhs) const {return _ptr > rhs._ptr;}
inline bool operator<(const Iterator& rhs) const {return _ptr < rhs._ptr;}
inline bool operator>=(const Iterator& rhs) const {return _ptr >= rhs._ptr;}
inline bool operator<=(const Iterator& rhs) const {return _ptr <= rhs._ptr;}
private:
Type* _ptr;
};
In general your approach is right. The postfix increment/decrement operator should return by value, not by reference. I also have doubts about:
Iterator(Type* rhs) : _ptr(rhs) {;}
This tells everyone that this iterator class is implemented around pointers. I would try making this method only callable by the container. Same for assignment to a pointer.
Adding two iterators makes no sense to me (I would leave "iterator+int"). Substracting two iterators pointing to the same container might make some sense.
Have a look at how Boost do it, the iterators in boost/container/vector.hpp - vector_const_iterator and vector_iterator are reasonably easy to understand pointer based iterators.
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.