Je ne suis pas sûr si je fais la surcharge correctement.Surcharge en C++
... \ point.h (42): erreur C2061: erreur de syntaxe: identificateur 'Vec3' Opérateur de point + (Vec3 a) const;
Voici mon fichier .h:
#include <fstream>
#include <iostream>
#include "vec3.h"
using std::ofstream;
using std::ifstream;
using std::cout;
using std::cin;
using std::endl;
using namespace std;
#ifndef POINT_H
#define POINT_H
//Define ourselves a basic 2D point class
class Point
{
friend ofstream& operator <<(ofstream& output, const Point& p);
friend ifstream& operator >>(ifstream& input, Point& p);
\
public:
Point();
Point(double _x, double _y);
Point(double _x, double _y, double _z);
double x,y,z;
//Operators
Point operator -(Point a) const;
Point operator /(double s) const;
Point operator *(double s) const;
// Used to do vector point addition
Point operator +(Vec3 a) const;
};
#endif
Voici mon fichier .cpp
#include "point.h"
Point::Point(double _x, double _y, double _z)
{
x= _x;
y= _y;
z= _z;
}
Point :: Point()
{
x = 0.0;
y = 0.0;
z = 0.0;
}
Point::Point(double _x, double _y)
{
x= _x;
y= _y;
z= 0;
}
Point Point::operator -(Point a) const
{
return Point(x-a.x, y-a.y, z-a.z);
}
Point Point::operator /(double s) const
{
return Point(x/s, y/s, z/s);
}
Point Point::operator *(double s) const
{
return Point(x*s, y*s, z*s);
}
// Vector Point Addition
Point Point::operator +(Vec3 a) const
{
return Point(x+a.x, y+a.y, z+a.z);
}
ofstream& operator <<(ofstream& output, const Point& p)
{
output << p.x << " " << p.y << " " << p.z << "\n";
return output;
}
ifstream& operator >>(ifstream& input, Point& p)
{
input >> p.x >> p.y >> p.z;
return input;
}
Voici le Vec3.h
#ifndef VEC3_H
#define VEC3_H
#include "point.h"
class Vec3
{
friend ofstream& operator <<(ofstream& output, const Vec3& p);
friend ifstream& operator >>(ifstream& input, Vec3& p);
public:
Vec3();
Vec3(double _x, double _y);
Vec3(double _x, double _y, double _z);
double x,y,z;
//Operators
Vec3 operator -(Vec3 a) const;
Vec3 operator /(double s) const;
Vec3 operator *(double s) const;
// Used to do vector Vec3 addition
Vec3 operator +(Vec3 a) const;
Point operator +(Point a) const;
};
#endif
Pourriez-vous poster le fichier vec3.h? – JaredPar
Difficile à dire sans vec3.h. Une chose que vous devriez noter est qu'il est bon d'avoir des références const aux arguments dans les opérateurs, aussi longtemps que vous passez des classes et des structs. – dutt
Vérifiez pour ** typo ** dans vec3.h .. si le nom de classe est 'vec3' ou' Vec3' quelque chose lik dat .. Le compilateur dit clairement qu'il ne peut pas identifier 'Vec3' ... – liaK