gpt4 book ai didi

c++ - 获取 LNK2005,我看不出我能做些什么来解决这个问题

转载 作者:行者123 更新时间:2023-11-28 08:09:19 25 4
gpt4 key购买 nike

我在每个文件中省略了 #include "stdafx.h

stdafx.h(预编译头文件)

#include a.h
#include b.h
class stuff;
stuff * s

啊啊

class thing{float f; void fun()};

a.cc

void thing::fun(){}
thing::thing():
f(b->f) {} // lnk 2005 linking error

b.h

struct stuff
{
float f;
thing * t;
};

b.cc

stuff::stuff(): f(3.4) { t = new thing; }

主.cc

int main()
{
s = new stuff;
s -> fun();
}

如您所见,我尝试访问在 stdafx.h 中预先声明的 s

我正在做这个设计,所以我不必依赖单例(我有一个主类,我想在其他较小的对象中访问它)

我是否需要以某种方式使用 extern 关键字?预编译 header 是否导致问题?

最佳答案

在 stdafx.h 中,您声明了 s,但从未定义过它。我会:

  1. extern添加到stdafx.h中的声明
  2. 像这样在 main.cc 中添加 s 的定义:

    stuff * s = NULL;

关于c++ - 获取 LNK2005,我看不出我能做些什么来解决这个问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9506113/

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