cpp 链接器失败,无法追踪错误源

cpp linker failed, can't trace source of error

我是 cpp 的新手,我偶然发现了一个我无法追踪的链接器故障。

我正在尝试 Google Test, and I tried to rewrite the inline header sample (sample-3inl.h) 到单独的源文件和头文件。

我的文件夹结构如下所示:

Project structure:
├── src
│   ├── queue.cpp
│   ├── queue.h
│   ├── CMakeLists.txt
├── test
│   ├── lib
│   │   ├── gtest
│   │   │   ├── googletest   
│   ├── unit_test
│   │   ├── unit_test.cpp
│   │   ├── CMakeLists.txt
│   ├── CMakeLists.txt
├── main.cpp
├── CMakeLists.txt

Google Test works fine, I have tried it with the original sample-3inl.h.

链接器失败错误:

Undefined symbols for architecture x86_64:
  "Queue<int>::Enqueue(int const&)", referenced from:
      QueueTest::SetUp() in dummy.cpp.o
  "Queue<int>::Queue()", referenced from:
      QueueTest::QueueTest() in dummy.cpp.o
  "Queue<int>::~Queue()", referenced from:
      QueueTest::QueueTest() in dummy.cpp.o
      QueueTest::~QueueTest() in dummy.cpp.o
  "Queue<int>::Size() const", referenced from:
      QueueTest_IsEmptyInitially_Test::TestBody() in dummy.cpp.o
ld: symbol(s) not found for architecture x86_64

源代码(myqueue.cpp、myqueue.h 和 unit_test.cpp)可以在下面的 page 中找到,为简单起见。

几个小时以来一直试图找出导致错误的原因,但我找不到,真的很想知道我做错了什么。 感谢您提前抽出时间。

I was trying out Google Test, and I tried to rewrite the inline header sample (sample-3inl.h) to a separate source and header file.

您不能使用模板来做到这一点。有关详细信息,请参阅 Why can templates only be implemented in the header file?