PDA

View Full Version : Warning and error



amreo
19th November 2015, 20:27
When I compile my program I get some warnings and a ld error. What am I wrong?


#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 : virtual public AbstractLocated, public 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 virtual 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 Copy costructor
* @param Located object
*/
Located(const Located& 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"

/**
* @brief This class is the base class of any movable objects in 2D space
* @author amreo
* @version 0.2
* @since 0.1
*/
class Movable : virtual public Located, virtual public AbstractMovable
{
public:
/**
* @brief Create a new instance of movable position from located object
* @param Located object
*/
explicit Movable(const AbstractMovable& loc) : Located(loc) {}
/**
* @brief Create a new instance of movable position from located object
* @param Located object
*/
explicit Movable(const Movable& loc) : Located(loc) {}

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

protected:

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

#endif // MOVABLE_H




#ifndef COORD_H
#define COORD_H
#include "movable.h"
#include <QPoint>

/**
* @brief This class rappresent a 2D coord
* @author amreo
* @version 0.2
* @since 0.1
*/
class Coord : public virtual Movable
{
Q_OBJECT

public:
/**
* @brief Costructor of coord
* @param Coordinate X
* @param Coordinate Y
* @param Parent object
*/
Coord(int x = 0, int y = 0) : Movable(x,y) {}

/**
* @brief Costructor of coord
* @param point
*/
explicit Coord(const QPoint& point) : Movable(point.x(), point.y()) {}
/**
* @brief Copy costructor of coord
* @param location
*/
Coord(const Coord& loc) : Movable(loc) {}

// /**
// * @brief Sum the left and the right
// * @param left
// * @param right
// * @return the sum of left and right
// */
// static inline Coord sum(const AbstractLocated& left, const AbstractLocated& right)
// { return Coord(left.x()+right.x(), left.y()+right.y()); }

signals:

public slots:

};

#endif // COORD_H





#include "coord.h"
int main()
{
Coord l(0,0);
}


Errors/Warnings:


In file included from ../build/moc/moc_coord.cpp:9:0:
../build/moc/../../../gridb/src/coord.h: In copy constructor 'Coord::Coord(const Coord&)':
../build/moc/../../../gridb/src/coord.h:34:9: warning: base class 'class Located' should be explicitly initialized in the copy constructor [-Wextra]
Coord(const Coord& loc) : Movable(loc) {}
^

In file included from ../../gridb/test/tests.h:8:0,
from ../../gridb/test/main.cpp:2:
../../gridb/src/coord.h: In copy constructor 'Coord::Coord(const Coord&)':
../../gridb/src/coord.h:34:9: warning: base class 'class Located' should be explicitly initialized in the copy constructor [-Wextra]
Coord(const Coord& loc) : Movable(loc) {}
^

In file included from ../../gridb/test/tests.h:8:0,
from moc_tests.cpp:9:
../../gridb/src/coord.h: In copy constructor 'Coord::Coord(const Coord&)':
../../gridb/src/coord.h:34:9: warning: base class 'class Located' should be explicitly initialized in the copy constructor [-Wextra]
Coord(const Coord& loc) : Movable(loc) {}
^


/home/amreo/src/repo/gridb/build-gridb-Desktop-Debug/test/../bin//libgridb.so: undefined reference to `AbstractMovable::locationChanged(AbstractLocated const&)'
collect2: error: ld returned 1 exit status

anda_skoa
19th November 2015, 20:45
You forgot to post the errors and warnings.
You also did not move the QObject inheritance to be the first as suggested in the other thread.
Why all the "virtual" inheritance?

Cheers,
_

amreo
19th November 2015, 21:21
Updated. I mistaked some error but I don't resolved the warnings/errors

ChrisW67
20th November 2015, 08:07
The linker error about "AbstractMovable::locationChanged()" is because there is no implementation of the function anywhere and something is trying to use it. You are trying to use it as a Qt signal and you are assuming that the implementation will be generated for you. For moc to do that for you AbstractMovable needs to be:

Publicly derived from a QObject class
QObject must be the first base class (http://doc.qt.io/qt-5/moc.html#multiple-inheritance-requires-qobject-to-be-first) if multiple inheritance is used
Have the Q_OBJECT macro
Be listed in the PRO file HEADERS

amreo
20th November 2015, 13:50
I modified it but I've got the similiar errors:



/home/amreo/src/repo/gridb/build-gridb-Desktop-Debug/test/../bin//libgridb.so: undefined reference to `AbstractMovable::qt_metacast(char const*)'
/home/amreo/src/repo/gridb/build-gridb-Desktop-Debug/test/../bin//libgridb.so: undefined reference to `VTT for Movable'
/home/amreo/src/repo/gridb/build-gridb-Desktop-Debug/test/../bin//libgridb.so: undefined reference to `vtable for Movable'
/home/amreo/src/repo/gridb/build-gridb-Desktop-Debug/test/../bin//libgridb.so: undefined reference to `virtual thunk to Movable::metaObject() const'
/home/amreo/src/repo/gridb/build-gridb-Desktop-Debug/test/../bin//libgridb.so: undefined reference to `typeinfo for AbstractMovable'
/home/amreo/src/repo/gridb/build-gridb-Desktop-Debug/test/../bin//libgridb.so: undefined reference to `virtual thunk to Movable::qt_metacall(QMetaObject::Call, int, void**)'
/home/amreo/src/repo/gridb/build-gridb-Desktop-Debug/test/../bin//libgridb.so: undefined reference to `Movable::staticMetaObject'
/home/amreo/src/repo/gridb/build-gridb-Desktop-Debug/test/../bin//libgridb.so: undefined reference to `AbstractMovable::metaObject() const'
/home/amreo/src/repo/gridb/build-gridb-Desktop-Debug/test/../bin//libgridb.so: undefined reference to `AbstractMovable::locationChanged(AbstractLocated const&)'
/home/amreo/src/repo/gridb/build-gridb-Desktop-Debug/test/../bin//libgridb.so: undefined reference to `Movable::qt_metacast(char const*)'
/home/amreo/src/repo/gridb/build-gridb-Desktop-Debug/test/../bin//libgridb.so: undefined reference to `AbstractMovable::qt_metacall(QMetaObject::Call, int, void**)'
/home/amreo/src/repo/gridb/build-gridb-Desktop-Debug/test/../bin//libgridb.so: undefined reference to `typeinfo for Movable'
/home/amreo/src/repo/gridb/build-gridb-Desktop-Debug/test/../bin//libgridb.so: undefined reference to `vtable for AbstractMovable'
/home/amreo/src/repo/gridb/build-gridb-Desktop-Debug/test/../bin//libgridb.so: undefined reference to `Movable::metaObject() const'
/home/amreo/src/repo/gridb/build-gridb-Desktop-Debug/test/../bin//libgridb.so: undefined reference to `Movable::qt_metacall(QMetaObject::Call, int, void**)'
collect2: error: ld returned 1 exit status
make[1]: *** [test] Error 1