在类的上下文中出错LNK2005和LNK1169

时间:2014-08-20 16:55:33

标签: c++ inheritance abstract-class pure-virtual multiple-definition-error

所以......我应该实现一个代表几何图形的类层次结构。但是,我在运行程序时遇到了这些错误:

1>Homework.obj : error LNK2005: "public: virtual __thiscall Shape::~Shape(void)" (??1Shape@@UAE@XZ) already defined in Circle.obj
1>Homework.obj : error LNK2005: "class std::basic_istream<char,struct std::char_traits<char> > & __cdecl operator>>(class std::basic_istream<char,struct std::char_traits<char> > &,class Point &)" (??5@YAAAV?$basic_istream@DU?$char_traits@D@std@@@std@@AAV01@AAVPoint@@@Z) already defined in Circle.obj
1>Homework.obj : error LNK2005: "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Point const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABVPoint@@@Z) already defined in Circle.obj
1>Homework.obj : error LNK2005: "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Shape const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABVShape@@@Z) already defined in Circle.obj
1>D:\Programe\Salvari\Homework\Debug\Homework.exe : fatal error LNK1169: one or more multiply defined symbols found

这应该是抽象类: Shape.h

#ifndef SHAPE_H__
#define SHAPE_H__

#include <ostream>
#include <istream>

class Shape {
public:
virtual void output (std :: ostream&) const = 0;
virtual void moveBy (int, int) = 0;
virtual void readFrom (std :: istream&) = 0;
virtual ~Shape() = 0;
friend std :: ostream &operator<< (std::ostream &, const Shape &);
};

Shape :: ~Shape() {};

std :: ostream& operator<< (std :: ostream& out, const Shape& sh) {
sh.output(out); return out;};

#endif

继承Shape的 Circle.h 类:

#ifndef CIRCLE_H__
#define CIRCLE_H__

#include <ostream>
#include <istream>
#include <string>

#include "Shape.h"
#include "Point.h"

class Circle : public Shape {
private:
Point c;
int r;
public:
static const std::string identifier;
Circle (const Point& = Point(), const int = 0);
~Circle() {};
void output (std::ostream &) const;
void readFrom (std::istream &);
void moveBy (int, int);
friend std :: ostream& operator<< (std :: ostream&, const Circle&);
};

#endif

课程的实施: Circle.cpp

#include "Circle.h"

std :: string const Circle :: identifier = "Circle";

Circle :: Circle (const Point& pct, const int n) : c(pct), r(n) {}

void Circle :: output (std :: ostream& out) const {
out << '\t' << identifier << " with the point of coordinates: " << c << std :: endl;
out << "and radius: " << r << std :: endl;}

void Circle :: readFrom (std :: istream& in) {in >> c; in >> r;}

void Circle :: moveBy (int x, int y) {c.moveBy(x,y);}

std :: ostream& operator<< (std :: ostream& out, const Circle& circle) {
circle.output(out); return out;}

Point.h

#ifndef POINT_H__
#define POINT_H__

#include <iostream>
#include <istream>
#include <ostream>

class Point {
private:
int x, y;
public:
Point (int x = 0, int y = 0) : x(x), y(y) {};
int getX() const {return x;};
int getY() const {return y;};
void setX (int x) {this->x = x;};
void setY (int y) {this->y = y;};
void moveBy (int x, int y) {this->x += x; this->y += y;};
friend std::istream &operator>> (std::istream &, Point &);
friend std::ostream &operator<< (std::ostream &, const Point &);
};

std :: istream& operator>> (std :: istream& in, Point& pct) {
std :: cout << "\tRead the point of coordinates:\n";
std :: cout << "x: "; in >> pct.x;
std :: cout << "y: "; in >> pct.y;
return in;};

std :: ostream& operator<< (std :: ostream& out, const Point& pct) {
out << "(" << pct.x << " ," << pct.y << ")\n"; return out;};

#endif

Homework.cpp

#include "Circle.h"
#include "stdafx.h"

using namespace std;

int main() {
Point p1(3,5);
Circle c(p1,5);
return 0;
}

如何摆脱错误?代码已经超过了我......

1 个答案:

答案 0 :(得分:3)

如果函数体出现在头文件中,它必须是内联的(或者有时是C样式的静态,但现在忽略它)。类定义中的函数体是自动内联的。但是,当你有一个纯虚析构函数时,你不能将正文放在类定义中,但仍然需要一个正文。

Shape :: ~Shape() {}移至shape.cpp文件,或将其作为inline Shape :: ~Shape() {}

保存在头文件中

对于operator<<也是如此,如果您追加friend,也可以将其移至课程正文中。

class Shape
{
    ..
    friend std :: ostream& operator<< (std :: ostream& out, const Shape& sh) {..}
};