C++ C+中的重载+;
我不确定我是否正确地进行了过载 …\point.h(42):错误C2061:语法错误:标识符“Vec3” 点运算符+(向量3 a)常数 这是我的.h文件:C++ C+中的重载+;,c++,operator-overloading,C++,Operator Overloading,我不确定我是否正确地进行了过载 …\point.h(42):错误C2061:语法错误:标识符“Vec3” 点运算符+(向量3 a)常数 这是我的.h文件: #include <fstream> #include <iostream> #include "vec3.h" using std::ofstream; using std::ifstream; using std::cout; using std::cin;
#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
#包括
#包括
#包括“vec3.h”
使用std::of流;
使用std::ifstream;
使用std::cout;
使用std::cin;
使用std::endl;
使用名称空间std;
#ifndef点H
#定义点
//定义一个基本的二维点类
类点
{
流和操作员的朋友(ifstream&input,Point&p);
\
公众:
点();
点(双x,双y);
点(双x,双y,双z);
双x,y,z;
//操作员
点运算符-(点a)常数;
点算子/(双s)常数;
点运算符*(双s)常数;
//用于矢量点加法
点运算符+(向量3 a)常数;
};
#恩迪夫
这是我的.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;
}
#包括“point.h”
点:点(双x,双y,双z)
{
x=x;
y=_y;
z=z;
}
点::点()
{
x=0.0;
y=0.0;
z=0.0;
}
点::点(双x,双y)
{
x=x;
y=_y;
z=0;
}
点::运算符-(点a)常量
{
返回点(x-a.x,y-a.y,z-a.z);
}
点::运算符/(双s)常量
{
返回点(x/s、y/s、z/s);
}
点::运算符*(双s)常量
{
返回点(x*s,y*s,z*s);
}
//向量点加法
点:运算符+(向量3 a)常量
{
返回点(x+a.x,y+a.y,z+a.z);
}
流程和操作员p.z;
返回输入;
}
这是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
\ifndef VEC3\u H
#定义向量3_H
#包括“h点”
Vec3类
{
流和操作员的朋友(ifstream&input、Vec3&p);
公众:
Vec3();
Vec3(双x,双y);
Vec3(双x,双y,双z);
双x,y,z;
//操作员
Vec3运算符-(Vec3 a)常量;
Vec3算子/(双s)常数;
Vec3运算符*(双s)常数;
//用于做向量Vec3加法
Vec3运算符+(Vec3 a)常量;
点运算符+(点a)常量;
};
#恩迪夫
vec3.h
包括point.h
和point.h
包括vec3.h
。您需要通过向前声明其中一个类来删除循环依赖项。您可以发布vec3.h文件吗?如果没有vec3.h,很难说。您应该注意的一点是,只要您正在传递类和结构,就最好在运算符中对参数进行常量引用。。无论类名是vec3
还是vec3
之类的。。编译器清楚地告诉我们它不能识别Vec3
…@dutt,所以我应该为我的参数传递常量?通常,在头文件中使用声明是不好的风格,因为任何包含头文件的人都可以使用声明来获取这些,无论他们是否需要它们。相反,在头文件中使用完全限定的名称(std::ofstream
,而不仅仅是ofstream
)。定位正确!我刚才也写了一个答案: