gpt4 book ai didi

c++ - 继承的模板化单例类 C++ 中的未定义构造函数

转载 作者:行者123 更新时间:2023-11-28 04:49:59 31 4
gpt4 key购买 nike

我今天遇到了一个问题,使用了我之前制作的工具:单例模板类。

template <class C>
class Singleton {
/*! \class Singleton
* \brief Singleton Template
*/
public:
/*!
* \brief Public access interface
* \return Singleton instance
*/
static C *getInstance (){
// Create new instance if not initialised
if (_singleton == NULL){
_singleton = new C;
}
return (static_cast<C*> (_singleton));
}

/*!
* \brief Public access destructor
*/
static void kill(){
// Clean Singleton object and free memory
if (_singleton != NULL){
delete _singleton;
_singleton = NULL;
}
}
protected:
/*!
* \brief Empty constructor
*/
Singleton() = default;
/*!
* \brief Destructor
*/
virtual ~Singleton() = default;

private:
static C *_singleton; /*!< Unique instance */
};

template <class C>
C *Singleton<C>::_singleton = NULL;

当我尝试在(非常基本的)继承类上调用 TextureManager::getInstance() 时,问题出现了:

class TextureManager : public Singleton<TextureManager> {
friend class Singleton<TextureManager>;
/*! \class TextureManager
* \brief Textures Container
*/
public:
protected:
/*!
* \brief Empty constructor
*/
TextureManager();
/*!
* \brief Destructor
*/
~TextureManager() override;
private:
};

在编译过程中,链接器向我显示了一个我理解但我并不真正理解原因的错误:

build/main.o: In function `Singleton<TextureManager>::getInstance()':
main.cpp (.text._ZN9SingletonI14TextureManagerE11getInstanceEv[_ZN9SingletonI14TextureManagerE11getInstanceEv]+0x24): undefined reference to `TextureManager::TextureManager()'

似乎找不到TextureManager.cpp中构造函数的定义

TextureManager::TextureManager() = default;
TextureManager::~TextureManager() = default;

此代码适用于我使用 premake 编译的旧项目,所以我猜测我当前的 makefile 是垃圾,但我找不到原因...

INCLUDE = -I/usr/X11R6/include/
LIBDIR = -L/usr/X11R6/lib

SRCDIR := sources
BUILDDIR := build
TARGET := bin/game

SRCEXT := cpp
SOURCES := $(shell find $(SRCDIR) -type f -name *.$(SRCEXT))
OBJECTS := $(patsubst $(SRCDIR)/%,$(BUILDDIR)/%,$(SOURCES:.$(SRCEXT)=.o))

FLAGS = -std=c++11 -Wall
CC = g++
CFLAGS = $(FLAGS) $(INCLUDE)
LIBS = -lglut -lGL -lGLU -lGLEW -lm

$(TARGET): $(OBJECTS)
@echo "Linking..."
@echo "$(CC) $(CFLAGS) $^ -o $@ $(LIBDIR) $< $(LIBS)"; $(CC) $(CFLAGS) -o $@ $(LIBDIR) $< $(LIBS)

$(BUILDDIR)/%.o: $(SRCDIR)/%.$(SRCEXT)
@mkdir -p $(@D)
@echo "$(CC) $(CFLAGS) -o $@ $(LIBDIR) $< $(LIBS)"; $(CC) $(CFLAGS) -c -o $@ $(LIBDIR) $< $(LIBS)

clean:
@echo "Cleaning..."
@echo "$(RM) -r $(BUILDDIR) $(TARGET)"; $(RM) -r $(BUILDDIR) $(TARGET)

.PHONY: clean

现在卡了几个小时,我所有的希望都寄托在你身上:p

编辑:完整的输出

g++ -std=c++11 -Wall -I/usr/X11R6/include/ -o build/main.o -L/usr/X11R6/lib sources/main.cpp -lglut -lGL -lGLU -lGLEW -lm
g++ -std=c++11 -Wall -I/usr/X11R6/include/ -o build/Tools/ThreadManager.o -L/usr/X11R6/lib sources/Tools/ThreadManager.cpp -lglut -lGL -lGLU -lGLEW -lm
g++ -std=c++11 -Wall -I/usr/X11R6/include/ -o build/Graphics/GraphicalEngine.o -L/usr/X11R6/lib sources/Graphics/GraphicalEngine.cpp -lglut -lGL -lGLU -lGLEW -lm
g++ -std=c++11 -Wall -I/usr/X11R6/include/ -o build/Graphics/TextureManager.o -L/usr/X11R6/lib sources/Graphics/TextureManager.cpp -lglut -lGL -lGLU -lGLEW -lm
Linking...
g++ -std=c++11 -Wall -I/usr/X11R6/include/ build/main.o build/Tools/ThreadManager.o build/Graphics/GraphicalEngine.o build/Graphics/TextureManager.o -o bin/game -L/usr/X11R6/lib build/main.o -lglut -lGL -lGLU -lGLEW -lm
build/main.o: In function `Singleton<TextureManager>::getInstance()':
main.cpp:(.text._ZN9SingletonI14TextureManagerE11getInstanceEv[_ZN9SingletonI14TextureManagerE11getInstanceEv]+0x24): undefined reference to `TextureManager::TextureManager()'
collect2: error: ld returned 1 exit status
makefile:18: recipe for target 'bin/game' failed
make: *** [bin/game] Error 1

最佳答案

问题是您只使用了 $(TARGET) 规则中的第一个依赖项:

$(CC) $(CFLAGS) -o $@ $(LIBDIR) $< $(LIBS)
# ^^

我的评论实际上是错误的,因为你在 @echo 和实际命令之间有不同的命令:

@echo "$(CC) $(CFLAGS) $^ -o $@ $(LIBDIR) $< $(LIBS)"
# ^^ ^^

我看不出在此处添加此 echo 行有什么意义,我会按以下步骤操作:

$(TARGET): $(OBJECTS)
@echo "Linking..."
$(CC) $(CFLAGS) $^ -o $@ $(LIBDIR) $(LIBS)
# ^^

如果你这样做了,我想你会更快地发现你的错误。

关于c++ - 继承的模板化单例类 C++ 中的未定义构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48421640/

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