简体   繁体   中英

Linking errors 2005, and 1169 when trying to overload the << operator

I get a linking error when i try to compile this code. I need to overload the output operator to display a three dimensional vector class I am not sure where to go from here any help is appreciated.

Vect3D.h

#ifndef VECT3D_H
#define VECT3D_H

#include <iostream>

class Vect3D
{
public:
Vect3D();
Vect3D(double xVal, double yVal, double zVal);

double getX() const { return x; }
double getY() const { return y; }
double getZ() const { return z; }
double magnitude() const { return sqrt(x*x + y*y + z*z); }

friend ostream& operator<<(ostream& os, const Vect3D& out);

void setX(double xVal) { x = xVal; }
void setY(double yVal) { y = yVal; }
void setZ(double zVal) { z = zVal; }

private:
double x;
double y;
double z;
};

ostream& operator<<(ostream& os, const Vect3D& out)
{

os << "(" << out.x << ", " << out.y << ", " << out.z << ")";
return os;
}

#endif

Vect3D.cpp

using namespace std;

#include "Vect3D.h"

Vect3D::Vect3D()
    : x(0), y(0), z(0)
{ }  // empty body

Vect3D::Vect3D(double xVal, double yVal, double zVal)
    : x(xVal), y(yVal), z(zVal)
{ }  // empty body

TestCode.cpp

#include <iostream>
#include <cmath>
#include <string>
#include <sstream>
using namespace std;

#include "Vect3D.h"
int main()
{
Vect3D v;
const Vect3D zero;
Vect3D v1(1, 2, 3), v2(7.5, 8.5, 9.5);
const Vect3D con(4, 5, 6);

cout << "Testing overload of << operator" << endl;
cout << v1;
cout << endl;
cout << "Should be:" << endl;
cout << "(" << v1.getX() << ", " << v1.getY() << ", " << v1.getZ() << ")" <<             endl << endl;

cout << "Testing chaining of overload of << operator" << endl;
cout << v1 << endl;
cout << "Should be:" << endl;
cout << "(" << v1.getX() << ", " << v1.getY() << ", " << v1.getZ() << ")" << endl << endl;

cout << "Testing overload of << operator for const Vect3D's" << endl;
cout << con << endl;
cout << "Should be:" << endl;
cout << "(" << con.getX() << ", " << con.getY() << ", " << con.getZ() << ")" << endl << endl;

cout << "Testing ostream parameter passing for the << operator" << endl;
stringstream sout;
sout << con;
string s = sout.str();
cout << s << endl;
cout << "Should be: " << endl;
cout << "(4, 5, 6)" << endl << endl;
cout << endl << endl;
return 0;
}

Errors:

Error 1 error LNK2005: "class std::basic_ostream > & __cdecl operator<<(class std::basic_ostream > &,class Vect3D const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABVVect3D@@@Z) already defined in TestCode.obj G:\\overloadAssignment\\overloadAssignment\\Vect3D.obj


Error 2 error LNK1169: one or more multiply defined symbols found G:\\overloadAssignment\\Debug\\overloadAssignment.exe 1

This directive has been used assiduously in other sections of this tutorial. When the preprocessor finds an #include directive it replaces it by the entire content of the specified header or file. See more information here .

So you have ostream& operator<<(ostream& os, const Vect3D& out) definition in Vect3D.h and you include this file in both TestCode.cpp and Vect3D.cpp . Thus you compile this function twice in Vect3D.obj and TestCode.obj . And when you try to link program, linker says that you have multiple definitions, and linker does not know what definition is right.

You need to put your implementation in Vect3D.cpp to compile it just once.

Vect3D.h

#ifndef VECT3D_H
#define VECT3D_H

#include <iostream>

class Vect3D
{
public:
Vect3D();
Vect3D(double xVal, double yVal, double zVal);

double getX() const { return x; }
double getY() const { return y; }
double getZ() const { return z; }
double magnitude() const { return sqrt(x*x + y*y + z*z); }

friend ostream& operator<<(ostream& os, const Vect3D& out);

void setX(double xVal) { x = xVal; }
void setY(double yVal) { y = yVal; }
void setZ(double zVal) { z = zVal; }

private:
double x;
double y;
double z;
};

ostream& operator<<(ostream& os, const Vect3D& out);

#endif

Vect3D.cpp

using namespace std;

#include "Vect3D.h"

Vect3D::Vect3D()
    : x(0), y(0), z(0)
{ }  // empty body

Vect3D::Vect3D(double xVal, double yVal, double zVal)
    : x(xVal), y(yVal), z(zVal)
{ }  // empty body

ostream& operator<<(ostream& os, const Vect3D& out)
{

os << "(" << out.x << ", " << out.y << ", " << out.z << ")";
return os;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM