### Why This code doesn't work?

I'm getting this error
.../Game/point.h:11: error: unknown type name 'Vector'
^
What am I missing?

Main.cpp

 ``1234567891011121314151617181920212223`` ``````#include #include "vector.h" #include "point.h" int main(void) { Point p; p.x = 1; p.y = 0; Vector v; v.x = 2; v.y = 3; Point p2 = p.AddVector(v); std::cout << "Result: (" << p2.x << ", " << p2.y << ")" << std::endl; std::cin.get(); return 0; }``````

Point.h
 ``123456789101112131415`` ``````#ifndef POINT_H #define POINT_H #include "vector.h" class Point { public: float x, y; Point AddVector(Vector v); }; #endif // POINT_H ``````

Point.cpp
 ``123456789101112`` ``````#include "point.h" Point Point::AddVector(Vector v) { Point p2; p2.x = x + v.x; p2.y = y + v.y; return p2; }``````

Vector.h
 ``1234567891011121314`` ``````#ifndef VECTOR_H #define VECTOR_H #include "point.h" class Vector { public: float x, y; Vector operator-(Point b); }; #endif // VECTOR_H ``````

Vector.cpp
 ``1234567891011`` ``````#include "vector.h" Vector Vector::operator-(Point b) { Vector v; v.x = this->x - b.x; v.y = this->y - b.y; return v; }``````
Last edited on
In vector.cpp Line 3 there is no class named vector1. I think you need to use some forward declarations.

This compiles for me.

vector.cpp
 ``1234567891011`` ``````#include "vector.h" Vector Vector::operator-(Point b) { Vector v; v.x = this->x - b.x; v.y = this->y - b.y; return v; }``````

vector.h
 ``1234567891011121314`` ``````#ifndef VECTOR_H #define VECTOR_H #include "point.h" class Point; //<--forward declaration class Vector { public: float x, y; Vector operator-(Point b); }; #endif // VECTOR ``````

Point.h
 ``123456789101112131415`` `````` #ifndef POINT_H #define POINT_H #include "vector.h" class Vector;//<---forward declaration class Point { public: float x, y; Point AddVector(Vector v); }; #endif / ``````

Point.cpp
 ``123456789101112`` ``````#include "point.h" Point Point::AddVector(Vector v) { Point p2; p2.x = x + v.x; p2.y = y + v.y; return p2; }``````
Last edited on
@Yanson,
Thank you for your help. You are right but this didn't solve the problem. At the beginning I though because I'm using vector as a name of a class that may somehow conflict with the standard vector class, so I rename the class as vector1 but I forgot to remove it here.
@Yanson,
thank you so much. I missed this part and it worked now.
Topic archived. No new replies allowed.