首先在这里定义

时间:2020-03-30 12:17:48

标签: c++

对于所有重载的运算符,我都会收到错误“首先在此处定义”。

我没有在其他任何地方声明过重载的运算符,也无法知道正在发生什么

我的代码(这是头文件):

#ifndef VECTOR_H
#define VECTOR_H
#include <iostream>

using namespace std;

class Vector
{
    public:
        Vector();
        double Getx();
        double Gety();
        double Getz();
        void Setx(double a);
        void Sety(double b);
        void Setz(double c);
        friend ostream &operator<<(ostream &mystream, Vector &v);
        friend istream &operator>>(istream &mystream, Vector &v);
    private:
        double x, y, z;
};

bool operator==(Vector a, Vector b)
{
    return (a.Getx() == b.Getx() && a.Gety() == b.Gety() && a.Getz() == b.Getz());
}

bool operator!=(Vector a, Vector b)
{
    return !(a==b);
}

Vector operator+(Vector a, Vector b)
{
    double k = a.Getx() + b.Getx();
    double l = a.Gety() + b.Gety();
    double m = a.Getz() + b.Getz();
    Vector v;
    v.Setx(k);
    v.Sety(l);
    v.Setz(m);
    return v;
}

double operator*(Vector a, Vector b)
{
    double r = a.Getx()*b.Getx() + a.Gety()*b.Gety() + a.Getz()*b.Getz();
    return r;
}

Vector operator*(Vector a, float b)
{
    Vector v;
    v.Setx(v.Getx()*b);
    v.Sety(v.Gety()*b);
    v.Setz(v.Getz()*b);
    return v;
}

ostream &operator<<(ostream &mystream, Vector &v)
{
mystream<<v.x<<", "<<v.y<<", "<<v.z<<endl;
return mystream;
}

istream &operator >> (istream &mystream, Vector &v)
{
cout<<"Enter x, y, z: ";
mystream>>v.x>>v.y>>v.z;
return mystream;
}


#endif // VECTOR_H

.cpp文件中的声明仅适用于构造函数,getter和setter,因此我无法理解我在哪里再次声明了重载运算符。

0 个答案:

没有答案