gpt4 book ai didi

c++ - 命名空间中变量的未解析外部符号

转载 作者:搜寻专家 更新时间:2023-10-31 01:08:57 27 4
gpt4 key购买 nike

我遇到 LNK2001 问题: Unresolved external symbol 错误。只有当我的所有类都在我的命名空间中并且引用我使用多个文件的全局变量时,它才会显示。这是我的代码的示例代码:

Engine.h

#pragma once
namespace Engine{
#include "Core.h"
#include "Display.h"
#include "Box.h"
// ... some code...
}
using namespace Engine;

核心.cpp

#include "Engine.h"
// ...some code...

Core.h

extern Matrix matrix;

// ... some code...

显示.cpp

#include "Engine.h"
Matrix matrix;

// ... some code...

Display.h

// ... some code...

Box.cpp

void Box::draw(PxShape *shape){
// matrix=.. some code...
}

Box.h

// ... some code...

错误信息

1>Box.obj : error LNK2001: unresolved external symbol "struct Engine::Matrix Engine::matrix" (?matrix@Engine@@3UMatrix@1@A)

当我评论命名空间时,一切正常。这是我第一次想使用命名空间,但我不知道该怎么做。

最佳答案

您的 #include 指令(以及您的接口(interface)定义)在 namespace Engine 中,但您的实现似乎不在。这给了你链接错误。

您还需要将代码主体包装在 namespace Engine 中的每个 .cpp 文件中。

即:

 #include "engine.h"
namespace Engine
{
// implementation goes here
}

关于c++ - 命名空间中变量的未解析外部符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17505275/

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