Hi
i tried to do some training and write '==' operator to compare between two vectors
but i got this error

Error   1   error LNK2005: "bool __cdecl operator==(class vector,class vector)" (??8@YA_NVvector@@0@Z) already defined in Source.obj    c:\Users\Sarbast\documents\visual studio 2013\Projects\Overloading Vector\Overloading Vector\Vector.obj Overloading Vector

Vector.h

#include <iostream>

using namespace std;

#ifndef VECTOR_H
#define VECTOR_H

class vector{
private:
    int x, y, z;
public:
    vector();

    vector(int, int, int);
    void DisplayVector();
    int GetX() const {
        return x;
    }
    int GetY() const{
        return y;
    }
    int GetZ() const {
        return z;
    }

};

bool operator==(const vector v1, const vector v2){
    if ((v1.GetX() == v2.GetX()) && 
        (v1.GetY() == v2.GetY()) &&
        (v1.GetZ() == v2.GetZ()))
        return true;
    else
        return false;
}

#endif

Vector.cpp

#include "Vector.h"

vector::vector(){
    x = 0;
    y = 0;
    z = 0;
}

vector::vector(int x1, int y1, int z1){
    x = x1;
    y = y1;
    z = z1;
}

void vector::DisplayVector(){
    cout << "X = " << x << endl;
    cout << "Y = " << y << endl;
    cout << "Z = " << z << endl;
}

Source.cpp

#include <iostream>
#include "Vector.h"

using namespace std;

int main()
{
    vector v1(3, 5, 7);
    v1.DisplayVector();
    vector v2(3, 5, 8);
    v2.DisplayVector();
    if (v1 == v2)
        cout << "The vectors are same " << endl;
    else
        cout << "They are different " << endl;

    system("PAUSE");
    return 0;

}

Recommended Answers

All 4 Replies

Looks like there is already a vector.h defined in your include path.

Try calling it something else myvector.h, myvector.cpp.

Same with class myvector

move the function starting on line 28 into a *.cpp file -- you can't put executable code in header files because the compiler will duplicate the code in each *.cpp file that uses the header file.

The problem is that your operator== function is defined in the header, which means that it will be compiled for each cpp file that includes it, leading to multiple definition errors (see One Definition Rule). What you need to do is declare the function in the header and implement it in the cpp file:

In Vector.h:

//..

bool operator==(const vector& v1, const vector& v2);

//..

In the Vector.cpp:

// ...

bool operator==(const vector& v1, const vector& v2){
    if ((v1.GetX() == v2.GetX()) && 
        (v1.GetY() == v2.GetY()) &&
        (v1.GetZ() == v2.GetZ()))
        return true;
    else
        return false;
}

// ...

Also, notice that I used pass-by-reference instead of values for the function parameters.

Also, you are playing with fire with your code. You should never do using namespace std; in a header file, and you should not create a class called vector in the global namespace as well. This will cause a conflict with the standard vector class template.

Thank you guys problem solved
and thanks Mike 2000 17 for the info

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.