PDA

View Full Version : [c++] ambiguos base



amreo
14th November 2015, 19:48
I've a error: movable.h:14: error: 'AbstractLocated' is an ambiguous base of 'Movable'
void setX(int x) {_x = x; emit locationChanged(*this); }
(similiar for other function)

Why?



#ifndef ABSTRACTLOCATED_H
#define ABSTRACTLOCATED_H

/**
* @brief This class is the base class of any non-movable objects in 2D space (without full implementation)
* @author amreo
* @version 0.1
* @since 0.4
*/
class AbstractLocated
{
public:

/**
* @brief Return the X coordinate
* @return X coordinate
*/
virtual int x() const = 0;
/**
* @brief Return the Y coordinate
* @return Y coordinate
*/
virtual int y() const = 0;

/**
* @brief Check if the location are equal
* @param First location
* @param Second location
* @return True if are in the same location (x and y), else false
*/
static bool isLocationEqual(const AbstractLocated& loc1, const AbstractLocated& loc2)
{ return loc1.x() == loc2.x() && loc1.y() == loc2.y(); }

/**
* @brief Check if the location are equal
* @param Second location
* @return True if are in the same location (x and y), else false
*/
bool operator ==(const AbstractLocated& loc2) const
{ return x() == loc2.x() && y() == loc2.y(); }

/**
* @brief Check if the location are inequal
* @param Second location
* @return True if are in the same location (x and y), else false
*/
bool operator !=(const AbstractLocated& loc2) const
{ return x() != loc2.x() || y() != loc2.y(); }
};

#endif // ABSTRACTLOCATED_H



#ifndef ABSTRACTMOVABLE_H
#define ABSTRACTMOVABLE_H
#include <QObject>
#include "abstractlocated.h"

/**
* @brief This class is the base class of any movable objects in 2D space (without full implementation)
* @author amreo
* @version 0.1
* @since 0.4
*/
class AbstractMovable : public AbstractLocated, QObject
{
public:

/**
* @brief Move the coordinte by the offset loc
* @param offset x and y
*/
AbstractMovable& operator >>(const AbstractLocated& loc) { move(loc); return *this; }
/**
* @brief Move the coordinte by the offset -loc
* @param offset x and y
*/
AbstractMovable& operator <<(const AbstractLocated& loc) { move(-loc.x(), -loc.y()); return *this; }

signals:
/**
* @brief This signal is emitted whenever the location is changed
* @param New location
*/
void locationChanged(const AbstractLocated& newLocation);

public slots:
/**
* @brief Set the coordinate X
* @param New coordinte X
*/
virtual void setX(int x) = 0;
/**
* @brief Set the coordinate Y
* @param New coordinte Y
*/
virtual void setY(int y) = 0;
/**
* @brief Set the coordinate x and y
* @param New coordinate x
* @param New coordinate y
*/
virtual void setMovable(int x, int y) = 0;
/**
* @brief Set the coordinate x and y from loc
* @param Located object
*/
inline void setMovable(const AbstractLocated& loc) { setMovable(loc.x(), loc.y()); }

/**
* @brief Move the coordinate by offset
* @param offset x
* @param offset y
*/
inline void move(int offsetX, int offsetY) { setMovable(this->x()+offsetX, this->y()+offsetY); }
/**
* @brief Move the coordinate by the offset
* @param offset X and Y
*/
inline void move(const AbstractLocated& offset) { setMovable(this->x()+offset.x(), this->y()+offset.y()); }

};

#endif // ABSTRACTMOVABLE_H



#ifndef LOCATED_H
#define LOCATED_H

#include "src_global.h"
#include "abstractlocated.h"

/**
* @brief This class is the base class of any non-movable objects in 2D space
* @author amreo
* @version 0.2
* @since 0.1
*/
class SRCSHARED_EXPORT Located : public AbstractLocated
{

public:
/**
* @brief Create a new instance of fixed position from located object
* @param Located object
*/
Located(const AbstractLocated& loc) : _x(loc.x()), _y(loc.y()) {}

/**
* @brief Return the X coordinate
* @return X coordinate
*/
inline int x() const { return _x; }
/**
* @brief Return the Y coordinate
* @return Y coordinate
*/
inline int y() const { return _x; }

protected:
/**
* @brief Coordinate x
*/
int _x;
/**
* @brief Coordinate y
*/
int _y;

/**
* @brief Create a new instance of fixed position
* @param Coordinate x
* @param Coordinate y
*/
Located(int x = 0, int y = 0) : _x(x), _y(y) {}
};

#endif // LOCATED_H




#ifndef MOVABLE_H
#define MOVABLE_H
#include "abstractmovable.h"
#include "located.h"

class Movable : virtual public Located, virtual public AbstractMovable
{
public:

/**
* @brief Set the coordinate X
* @param New coordinte X
*/
void setX(int x) {_x = x; emit locationChanged(*this); }
/**
* @brief Set the coordinate Y
* @param New coordinte Y
*/
void setY(int y) {_y = y; emit locationChanged(*this); }
/**
* @brief Set the coordinate x and y
* @param New coordinate x
* @param New coordinate y
*/
void setMovable(int x, int y) {_x = x; _y = y; emit locationChanged(*this); }
};

#endif // MOVABLE_H


Why?

Sintegrial
15th November 2015, 21:39
Located and AbstractMovable have to be ihnerited with virtual keyword as well.

amreo
15th November 2015, 21:50
Thanks 10^3

anda_skoa
16th November 2015, 09:58
Also inherit from QObject publically if you want to use this as a QObject (e.g in connect()) and make the class inherit from QObject first, MOC only checks the first base class.

Cheers,
_