在尝试重载<<<<<<<<<<<操作者

时间:2015-05-05 23:17:33

标签: c++ overloading

当我尝试编译此代码时出现链接错误。我需要重载输出操作符以显示三维矢量类我不知道从哪里可以获得任何帮助。

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;
}

错误:

  

错误1错误LNK2005:&#34; class std :: basic_ostream&gt; &安培; __cdecl运算符&lt;&lt;(类std :: basic_ostream&gt;&amp;,类Vect3D const&amp;)&#34; (?? 6 @ YAAAV?$ basic_ostream @ DU?$ char_traits @ D @ std @@@ std @@ AAV01 @ ABVVect3D @@@ Z)已在TestCode.obj中定义G:\ overloadAssignment \ overloadAssignment \ Vect3D.obj

  

错误2错误LNK1169:找到一个或多个多重定义的符号G:\ overloadAssignment \ Debug \ overloadAssignment.exe 1

1 个答案:

答案 0 :(得分:0)

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 echo $host1 echo $host2 echo $host3 definition in for i in $(seq 1 $number_of_hosts); do echo $host$i done and you include this file in both ostream& operator<<(ostream& os, const Vect3D& out) and Vect3D.h. Thus you compile this function twice in TestCode.cpp and Vect3D.cpp. 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.obj to compile it just once.

TestCode.obj

Vect3D.cpp

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