gpt4 book ai didi

c++ - qt 错误 : multiple definition of `Protocol::Protocol()'

转载 作者:行者123 更新时间:2023-11-30 01:43:31 30 4
gpt4 key购买 nike

我在 Protocal.h 文件中写了一个类。

#ifndef PROTOCOL_H
#define PROTOCOL_H

class Protocol{
public:
Protocol();

void analyse();
};

Protocol::Protocol() {}

void Protocol::analyse() {
}

#endif // PROTOCOL_H

在sinffer.h文件中,我使用了这个头文件 enter image description here

当我构建项目时,出现了一些错误,我不知道为什么。 error message image

在我的 .pro 文件中 enter image description here

只写一次protocol.h文件。

最佳答案

您可以使用以下方式定义协议(protocol)构造函数的主体:

在类定义中(隐式内联)

 // protocol.h    
class Protocol {
public:
Protocol() {
// in the class definition
}
...
};

显式内联

 // protocol.h    
class Protocol {
public:
Protocol();
...
};
//
inline Protocol::Protocol() {
// inline prevents double definition error when you include protocol.h
}

放入cpp文件

 // protocol.h    
class Protocol {
public:
Protocol();
...
};
// protocol.cpp
#include "protocol.h"
Protocol::Protocol() {
}

关于c++ - qt 错误 : multiple definition of `Protocol::Protocol()' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37627768/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com