1

I'm sure this question has been asked before, but I can't seem to find it.

I have two classes, Vector and Point.

The files are as such (a bit repetitive):

vector.h:

#include <math.h>
#include <stdlib.h>

class Vector {
  friend class Point;

  public:
    ...

    Vector(Point); // Line 16

vector.cpp:

#include <math.h>
#include <stdlib.h>

#include "vector.h"

...

Vector::Vector(Point point) { // Line 29
  x = point.x;
  y = point.y;
  z = point.z;
}

point.cpp and point.h look mostly the same, except you swap vector with point in the definitions.

I include them as such:

#include "structures/vector.cpp"
#include "structures/point.cpp"

When I compile, I get this error:

structures/vector.h:16:17: error: field ‘Point’ has incomplete type
structures/vector.cpp:29:15: error: expected constructor, destructor, or type conversion before ‘(’ token

I think this error is saying that Point hasn't been declared yet, but when I declare it inside of vector.h by importing point.cpp, I get a huge pile of errors.

Could anyone shed some light on this problem?

Thank you!


Upon applying @ildjarn's suggestions, those errors went away and I am left with this single one:

structures/vector.h:16:18: error: expected ‘)’ before ‘const’

And the line:

Vector(Point const);

I define it like so in the .cpp file:

Vector::Vector(Point const &point) {
Blender
  • 289,723
  • 53
  • 439
  • 496
  • 1
    possible duplicate of [C++ cyclic inclusion issue](http://stackoverflow.com/questions/4685220/c-cyclic-inclusion-issue) – sth Oct 06 '11 at 00:12

1 Answers1

5
  1. You shouldn't be including .cpp files, you should be including the .h files.

  2. vector.cpp needs #include "point.h" and (presumably) point.cpp needs #include "vector.h".

  3. A forward declaration is only sufficient if you're not doing anything that requires the type's size or interface. Because Vector's constructor is taking a Point by value, its size must be known; change Vector's constructor to take the Point by const reference instead and a forward declaration will remain sufficient.

  4. Your headers need #include guards (or #pragma once if you don't mind not being 100% portable).

EDIT (in response to OP's edit):

Your declaration and definitions now mismatch -- i.e., your definition is correct but your declaration needs Point const& rather than just Point const.

ildjarn
  • 62,044
  • 9
  • 127
  • 211
  • That error went away, thank you! Now a less-cryptic one has arisen (YES!!!) See my question again for that one. – Blender Oct 06 '11 at 00:15
  • Thanks for the response, I really appreciate the input. I tried it, but I seem to be getting the same results. – Blender Oct 06 '11 at 00:19
  • @Blender : Then please post an accurate copy of your updated code. Note that the forward declaration is still necessary. – ildjarn Oct 06 '11 at 00:20
  • I tried putting the ampersand in almost every place in this line: `Vector(Point const&);`. – Blender Oct 06 '11 at 00:21
  • My code was so bad I just rewrote the whole program from scratch with your suggestions. Thanks for the input! – Blender Oct 07 '11 at 16:51