多重声明错误-虚函数

发布于 2024-10-10 17:01:29 字数 1708 浏览 8 评论 0原文

我有 Observer.h 、 client.h 和 field.h 文件。

在observer.h中有一个Subject类,其中有

// observer.h

class Subject  {
public:
 virtual ~Subject(){};
 Subject(){};
 virtual void Attach(Observer*);
 virtual void Detach(Observer*);
 virtual void Notify(bool _value);
 virtual bool getCheckedIn(){};
private:
 vector < Observer* >  _observers;
};

 

#ifndef CLIENT_H
#define CLIENT_H

#include "Field.h"

class Client : public Subject {
public:
 Client(string _name, Field *_field) : client_name(_name) ,field(_field) , checked_in(false) {}

 void setCheckedIn(bool _value){
  checked_in = _value;
  Notify(_value);
 }

 void enterRow(string _row_name){
  field->deneme();
  setCheckedIn(true);
 }

 bool getCheckedIn(){ return checked_in;}
private:
 bool checked_in;
 string client_name;

 Field *field;

};

#endif // CLIENT_H

 

#ifndef Field_H
#define Field_H
#include "CreateRow_absFac.h"
#include "observer_pattern.h"
#include <vector>
#include <string>
using namespace std;

// Template Class 
class Field{
public:
 Field(); 
 // Template method 
 void field_creator();
 virtual void setAbstractRow() = 0;
protected:
 FarmFactory *abstract_row1;
 FarmFactory *abstract_row2;
 FarmFactory *abstract_row3;

 Rows *row1 ;
 Rows *row2 ;
 Rows *row3 ;
 Sensor sensor1; 
};

编译时,出现此错误:

ld:/Users/barisatamer/Desktop/se311/PROJECT/build/PROJECT.build/Debug/PROJECT.build/Objects-normal/x86_64/Field中重复符号Subject::Notify(bool)。 o 和 /Users/barisatamer/Desktop/se311/PROJECT/build/PROJECT.build/Debug/PROJECT.build/Objects-normal/x86_64/main.o

如果我删除虚拟函数,它编译时不会出错。虚函数有什么问题?

I have observer.h , client.h and field.h files.

In observer.h there is Subject class which has

// observer.h

class Subject  {
public:
 virtual ~Subject(){};
 Subject(){};
 virtual void Attach(Observer*);
 virtual void Detach(Observer*);
 virtual void Notify(bool _value);
 virtual bool getCheckedIn(){};
private:
 vector < Observer* >  _observers;
};

 

#ifndef CLIENT_H
#define CLIENT_H

#include "Field.h"

class Client : public Subject {
public:
 Client(string _name, Field *_field) : client_name(_name) ,field(_field) , checked_in(false) {}

 void setCheckedIn(bool _value){
  checked_in = _value;
  Notify(_value);
 }

 void enterRow(string _row_name){
  field->deneme();
  setCheckedIn(true);
 }

 bool getCheckedIn(){ return checked_in;}
private:
 bool checked_in;
 string client_name;

 Field *field;

};

#endif // CLIENT_H

 

#ifndef Field_H
#define Field_H
#include "CreateRow_absFac.h"
#include "observer_pattern.h"
#include <vector>
#include <string>
using namespace std;

// Template Class 
class Field{
public:
 Field(); 
 // Template method 
 void field_creator();
 virtual void setAbstractRow() = 0;
protected:
 FarmFactory *abstract_row1;
 FarmFactory *abstract_row2;
 FarmFactory *abstract_row3;

 Rows *row1 ;
 Rows *row2 ;
 Rows *row3 ;
 Sensor sensor1; 
};

When compiled , got this error :

ld: duplicate symbol Subject::Notify(bool) in /Users/barisatamer/Desktop/se311/PROJECT/build/PROJECT.build/Debug/PROJECT.build/Objects-normal/x86_64/Field.o and /Users/barisatamer/Desktop/se311/PROJECT/build/PROJECT.build/Debug/PROJECT.build/Objects-normal/x86_64/main.o

If I remove virtual functions it compiles without error. What is the problem with virtual functions ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼眸里的那抹悲凉 2024-10-17 17:01:29

我们实际上在这里看不到它,但问题可能是您在头文件中定义了 Subject::notify(bool) (您的observer.h只是声明了它,它没有定义它)并且您将该头文件包含在 Field.cpp 和 main.cpp 中,因此您可以获得多个定义。解决方法是将定义移至源文件中,使其仅定义一次。

一般规则——在头文件中声明内容,在非头文件中定义它们。请注意,包含保护在这里无关紧要——它们防止在单个编译单元中多次声明某些内容,但需要的是避免在不同的编译单元中多次定义某些内容。

We can't actually see it here, but the problem is probably that you defined Subject::notify(bool) in a header file (your observer.h just declares it, it doesn't define it) and you included that header file in both Field.cpp and main.cpp, so you get multiple definitions. The fix is to move the definition into a source file so its only defined once.

General rule -- DECLARE things in header files, DEFINE them in non-header source files. Note that include guards are irrelevant here -- they prevent something being declared multiple times in a single compilation unit, but what's needed is to avoid defining something multiple times in different compilation units.

喜爱皱眉﹌ 2024-10-17 17:01:29

尝试为您的 observer.h 保留标头保护。顺便说一句,为什么不重写派生类中的虚拟函数?

Try keeping header guards even for your observer.h. BTW, Why aren't you overriding virtual functions in the derived class ?

南薇 2024-10-17 17:01:29

显然您违反了 ODR。为什么你放弃非虚函数呢?可能是因为您内联定义了它们(例如在课堂上)。正如建议的那样,检查包含防护和函数定义。

Apparently you have an ODR violation. Why did you get away with non-virtual functions? Possibly because you defined them inline (e.g. in class). As it was suggested, check the include guards and function definitions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文