错误LNK2005“ void __cdecl Command :: addCommand(struct Command :: Command)”(?addCommand @ Command @@ YAXU11 @@ Z)已在main.cpp.obj D:\ Code \ cpp \中定义discord \ DexunBot \ MainBot \ CMakeLists.txt D:\ Code \ cpp \ discord \ DexunBot \ MainBot \ dexun_client.cpp.obj 1
错误LNK2005“ void __cdecl CommandHandler :: RegisterCommands(void)”(?RegisterCommands @ CommandHandler @@ YAXXZ)已在main.cpp.obj D:\ Code \ cpp \ discord \ DexunBot \ MainBot \ CMakeLists中定义.txt D:\ Code \ cpp \ discord \ DexunBot \ MainBot \ dexun_client.cpp.obj 1
错误LNK2005“ void __cdecl CommandHandler :: CheckExecuteCommand(class DexunClientClass&,struct SleepyDiscord :: Message)”(?CheckExecuteCommand @ CommandHandler @@ YAXAEAVDexunClientClass @@ UMessage @ SleepyDiscord @@@@ Z)已在main中定义。 cpp.obj D:\ Code \ cpp \ discord \ DexunBot \ MainBot \ CMakeLists.txt D:\ Code \ cpp \ discord \ DexunBot \ MainBot \ dexun_client.cpp.obj 1
错误LNK2005“ class std :: unordered_map,class std :: allocator>,struct Command :: Command,struct std :: hash,class std :: allocator>>,struct std :: equal_to,class std :: allocator>>,class std :: allocator,class std :: allocator> const,struct Command :: Command>>> Command :: all“(?all @ Command @@ 3V?$ unordered_map @ V?$ basic_string @ DU?$ char_traits @ D @ std @@ V?$ allocator @ D @ 2 @@ std @@ UCommand @ 3 @ U?$ hash @ V?$ basic_string @ DU?$ char_traits @ D @ std @@ V?$ allocator @ D @ 2 @@ std @@@ 2 @ U?$ equal_to @ V?$ basic_string @ DU?$ char_traits @ D @ std @@ V?$ allocator @ D @ 2 @@ std @@@@ 2 @ V ?$ allocator @ U?$ pair @ $$ CBV?$ basic_string @ DU?$ char_traits @ D @ std @@ V?$ allocator @ D @ 2 @@ std @@ UCommand @ 3 @@ std @@@@ 2 @ @ std @@ A)已在main.cpp.obj中定义D:\ Code \ cpp \ discord \ DexunBot \ MainBot \ CMakeLists.txt D:\ Code \ cpp \ discord \ DexunBot \ MainBot \ dexun_client.cpp.obj 1 / p>
尝试编译我的项目时,出现一些链接器错误。
我已经尝试将command.hpp也分离到command.cpp文件中,但是会出现诸如std :: function不存在或类似错误的错误消息。
// command.hpp
class DexunClientClass;
namespace Command {
using Verb = std::function<
void(
DexunClientClass&,
SleepyDiscord::Message&,
std::queue<std::string>&
)
>;
struct Command {
std::string name;
std::vector<std::string> params;
std::string description;
Verb verb;
};
using MappedCommands = std::unordered_map<std::string, Command>;
using MappedCommand = MappedCommands::value_type;
MappedCommands all;
void addCommand(Command command) {
all.emplace(command.name, command);
}
}
// command_handler.hpp
#include "sleepy_discord/sleepy_discord.h"
#include "commands/help_command.hpp"
#include "dexun_client.hpp"
namespace CommandHandler {
void RegisterCommands() {
HelpCommand helpCommand;
helpCommand.registerHelpCommand();
}
void CheckExecuteCommand(DexunClientClass& client, SleepyDiscord::Message message) {
client.sendMessage(message.channelID, "WFAWFWF");
}
}
// dexun_client.hpp
#include "sleepy_discord/websocketpp_websocket.h"
class DexunClientClass : public SleepyDiscord::DiscordClient {
public:
using SleepyDiscord::DiscordClient::DiscordClient;
void onMessage(SleepyDiscord::Message message) override;
};
// dexun_client.cpp
#include "dexun_client.hpp"
#include "command.hpp"
#include "command_handler.hpp"
std::queue<std::string> split(const std::string& source) {
std::stringstream ss(source);
std::string item;
std::queue<std::string> target;
while (std::getline(ss, item, ' '))
if (!item.empty())
target.push(item);
return target;
}
void DexunClientClass::onMessage(SleepyDiscord::Message message) {
if (message.startsWith("dexun") || message.startsWith("dex")) {
CommandHandler::CheckExecuteCommand(*this, message);
std::queue<std::string> parameters = split(message.content);
const std::string mention = "<@" + message.author.ID + ">";
const std::string mentionNick = "<@!" + message.author.username + ">";
if (
//only allow if has more then 1 parameter
parameters.size() <= 1 &&
//only allow if starts with a mention
(parameters.front() != mention || parameters.front() != mentionNick)
)
return;
//remove the parameters as we go
parameters.pop();
if (parameters.empty())
return;
//get command
Command::MappedCommands::iterator foundCommand =
Command::all.find(parameters.front());
if (foundCommand == Command::all.end()) {
sendMessage(message.channelID, "Error: Command not found");
return;
}
parameters.pop();
if (
parameters.size() <
foundCommand->second.params.size()
) {
sendMessage(message.channelID, "Error: Too few parameters");
return;
}
//call command
foundCommand->second.verb(*this, message, parameters);
}
}
// main.cpp
#include "dexun_client.hpp"
#include "command_handler.hpp"
int main() {
DexunClientClass client("token", 2);
CommandHandler::RegisterCommands();
std::cout << "Registered Commands" << std::endl;
client.run();
}```
答案 0 :(得分:0)
我注意到的一件事是您没有一次使用标头卫士或pagma。第二,您可以通过包含标准函数库轻松修复“ std :: function不存在”。
无论如何,当包含dexun_client.hpp时,您还将包含sleepy_discord / websocketpp_websocket。到目前为止没有问题。但让我们包括command_handler,它还包括sleepy_discord / sleepy_discord命令/ help_command以及所有包含的内容,以及dexun_client。我看不到dexun_client.hpp具有任何标题保护措施或阻止它被包含一次以上的其他方法。但这并没有显示为错误,因此我猜想command.hpp也包含在help_command.hpp中。
此外,我有一种感觉,您在不了解其工作原理的情况下复制了我的代码。但是,这使我意识到我没有使用任何头保护,但是如果您看看https://github.com/yourWaifu/sleepy-discord/tree/develop/examples/sound-player。它不需要任何内容,因为它只有一个头文件,并且只包含一次。
Tl dr:我想是因为您缺少标头卫兵,而不是一次#pagma。