0

i'm working on an exercise at uni and every time i try to compile the main.cpp i got always the same error.

actor.h:

class Actor {
public:
Actor();
Actor(double x0, double y0);
void move();
double pos_x();
double pos_y();

static const int ARENA_W = 500;
static const int ARENA_H = 500;
};

plane.h (subclass of actor):

class Plane:Actor
{
public:
Plane();
Plane(double x0, double y0);
void move();
double pos_x();
double pos_y();

//int dx = 5;
static const int W = 50;
static const int H = 20;

private:
double x, y;
};

plane.cpp

#include "plane.h"
#include "actor.h"

Plane::Plane(double x0, double y0)
{
this ->x = x0;
this ->y = y0;
//this -> dx;
}

void Plane::move()
{
x = x + 2.5 ;
}

 double Plane::pos_x()
{
return x;
}
double Plane::pos_y()
{
return y;
}

main.cpp

include "plane.h"
include"actor.h"

using namespace std;

int main(int argc, char *argv[])
{
Plane plane1(25.0, 5.0);
plane1.move();
double x = plane1.pos_x();
double y = plane1.pos_y();
cout << x << " , " << y<<endl;
}

i saw there are many questions about this problem but i didn't fix it. can you help me please()? thank you

Mark.
  • 49
  • 1
  • 12
  • Please, post exact error – Starl1ght Nov 29 '16 at 15:55
  • Exactly what symbol(s) were not found? – aschepler Nov 29 '16 at 15:56
  • Possible duplicate of [Make Error: Undefined symbols for architecture x86\_64](http://stackoverflow.com/questions/33488801/make-error-undefined-symbols-for-architecture-x86-64) – TheDarkKnight Nov 29 '16 at 15:58
  • 1
    Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Danh Nov 29 '16 at 15:59
  • You're missing the implementation of the `Actor::Actor()`, which is needed because `Plane` is a child class of `Actor` and you need to construct `Actor` before you construct `Plane` - as it's trivial; you can use `Actor::Actor() {}` in `actor.h` and it should address the – Anya Shenanigans Nov 29 '16 at 16:03
  • Undefined symbols for architecture x86_64: "Actor::Actor()", referenced from: Plane::Plane(double, double) in plane.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) make: *** [4_3_lista_personaggi] Error 1 16:42:54: The process "/usr/bin/make" exited with code 2. Error while building/deploying project 4_3_lista_personaggi (kit: Desktop Qt 5.7.0 clang 64bit) When executing step "Make" -----> this is the error – Mark. Nov 29 '16 at 16:09
  • @Petesh i didn't understand what you mean, could you explain me better? – Mark. Nov 29 '16 at 16:10
  • @Petesh i added Actor::Actor() {} in actor.h but its still not working – Mark. Nov 29 '16 at 16:13
  • Added, or replaced the line `Actor();`? You should have replaced it with `Actor() {}` – Anya Shenanigans Nov 29 '16 at 16:16
  • @Petesh i replaced it. Now the error is: - 4_3_lista_personaggi/actor.h:7: error: extra qualification on member 'Actor' Actor::Actor() { ~~~~~~~^ – Mark. Nov 29 '16 at 16:19

1 Answers1

0

You've declared a class Actor in actor.h:

class Actor {
    public: Actor();
};

This means that you're going to write some code that will define this construction. This would typically end up in an Actor.cpp file.

If you attempt to construct an Actor without having this implementation, you will get an error from the linker because you're missing the default constructor.

Now you've declared a Plane that's a subclass of an Actor:

class Plane : Actor {
};

and you've defined a non-default constructor:

Plane::Plane(double, double) {
   // does something
}

As Plane is a subclass of Actor, there's an implicit construction of a default Actor as part of the construction of Plane, and as you declared that there would be an implementation, the linker is expecting it. As you never defined it in the code, the linker fails at this point.

The somewhat simplistic solution is to add a trivial constructor in actor.h; namely:

class Actor {
public:
Actor() {} // replace the ; with {}
Actor(double x0, double y0);
void move();
double pos_x();
double pos_y();

static const int ARENA_W = 500;
static const int ARENA_H = 500;
};

Now, as for behaviours here - none of the move, pos_x or pos_y methods are declared virtual, so they're not being overloaded in Plane; they're simply being replaced. This may come up later in your course.

Anya Shenanigans
  • 91,618
  • 3
  • 107
  • 122
  • so should i write an actor.cpp file ? – Mark. Nov 29 '16 at 16:26
  • If you intend to implement proper constructors, and have implementations of `move` and `pos_x` and `pos_y`, then adding an `actor.cpp` file would be an approach; however as I've commented in the answer already; because the code is trivial, implementations in the `.h` file are perfectly acceptable as written. – Anya Shenanigans Nov 29 '16 at 16:28
  • Make sure you didn't write `Actor::Actor() {}` in the `actor.h` file, but simply wrote `Actor() {}` in the file - this is the reason for the `extra qualification` warning (and I can't edit my comment to replace the mis-written text). – Anya Shenanigans Nov 29 '16 at 16:31