简体   繁体   English

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

[英]Error LNK2005 and LNK1169 in the context of classes

So... I am supposed to implement a hierarchy of classes which represent geometrical figures. 所以......我应该实现一个代表几何图形的类层次结构。 However I've run into these errors while running the program: 但是我在运行程序时遇到了这些错误:

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

This is supposed to be the abstract class: Shape.h 这应该是抽象类: 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

The class Circle.h that inherits Shape: 继承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

Implementation of class: Circle.cpp 类的实现: 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 : 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 : 家庭作业.cpp

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

using namespace std;

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

How can I get rid of the errors? 我怎样才能摆脱错误? The code has overwelmed me... 代码已经过度了我...

If a function body appears in a header file, it must be inline (or sometimes C-style static, but ignore this for now). 如果函数体出现在头文件中,它必须是内联的(或者有时是C样式的静态,但现在忽略它)。 Function bodies inside class definitions are automatically inline. 类定义中的函数体是自动内联的。 However, when you have a pure virtual destructor, you cannot put the body inside the class definition, but a body is still required. 但是,当您拥有纯虚拟析构函数时,不能将正文放在类定义中,但仍需要正文。

Either move Shape :: ~Shape() {} to a shape.cpp file, or keep it in the header file as inline Shape :: ~Shape() {} Shape :: ~Shape() {}到shape.cpp文件,或将其作为inline Shape :: ~Shape() {} 〜Shape Shape :: ~Shape() {}保存在头文件中

The same for your operator<< , which could also be moved into the class body if you append friend . 对于您的operator<<也是如此,如果您追加friend ,也可以将其移动到类主体中。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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