1

I get the error "First Defined Here" for all my overloaded operators.

I have not declared those overloaded operators anywhere else and I cannot find out what is happening

My code (this is the header file):

#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

The declarations in the .cpp file are only for the constructor, the getters and the setters so I cannot understand where I have declared the overloaded operators another time.

  • Move the function definitions into a .cpp file. [How can a C++ header file include implementation?](//stackoverflow.com/a/14517957) – 001 Mar 30 '20 at 12:19
  • 4
    "First defined here" isn't an error message. It's information about the **preceding** error message, which should have been included in the question. Usually it's something like "Error: redefinition of xxxx, first defined at yyyy". So the line where the error occurred is the second definition, and the "first defined at" is the first one. From there you have to figure out what went wrong. – Pete Becker Mar 30 '20 at 12:20
  • 1
    If you put definitions in a header file, they need to be `inline`. So either do that, or move them to a .cpp file as Johnny Mopp suggests. – MSalters Mar 30 '20 at 12:22
  • Thank you all. I moved them into the .cpp file and it works now. I was confused because in the professor's slides they looked like they were in the .h file, but I guess that's just a miscommunication haha. – Bill Siopis Mar 30 '20 at 12:30
  • Does this answer your question? [multiple definition in header file](https://stackoverflow.com/questions/2727582/multiple-definition-in-header-file) – Davis Herring Mar 30 '20 at 21:11

0 Answers0