1

我正在尝试创建一个堆栈并在使用 C++ Google 测试框架的实验中使用 Google 测试对其进行测试。我已经设置了结构,所以我有一个 stack.h 和 stack.cpp 用于实现,然后我有一个带有以下代码的 tests.cpp。我有几个问题:首先,是否可以像我所做的那样从 main 调用我的测试函数?另外,我是否正确地包括了所有内容?为什么会发生构建错误?抱歉,我是 C++ 新手,提前感谢您的帮助。这是代码和错误:

#include "stack.h"
#include "gtest/gtest.h"

TEST (StackTest, PushAndPeek) {
    Stack<int> intStack;
    int a = 12;
    int b = 15;
    EXPECT_EQ (12, intStack.push(a));
    EXPECT_EQ (15, intStack.push(b));
    EXPECT_EQ (15, intStack.peek()); //make sure adding in LIFO Order
    EXPECT_EQ (15, intStack.peek()); //Should still be there
}

TEST (StackTest, PushAndPop) {
    Stack<int> intStack;
    int a = 12;
    int b = 15;
    EXPECT_EQ (12, intStack.push(a));
    EXPECT_EQ (15, intStack.push(b));
    EXPECT_EQ (15, intStack.pop()); //make sure adding in LIFO Order
    EXPECT_EQ (12, intStack.pop()); //Should have removed 15, then removed 12
    EXPECT_EQ (-1, intStack.pop()); //Should return -1 because there is nothing on the stack
}

然后在我的 main.cpp 我有:

#include <iostream>
#include <string>
#include "gtest/gtest.h"
#include "stack.h"
using namespace std;

int main(int argc, char * argv[])
{
    string input;
    cout << "Hey there! If you wanna run the tests, type in tests. \nOther wise just hit enter to continue...\n";
    getline (cin, input);
    if(input == "tests"){
        ::testing::InitGoogleTest(&argc, argv);
        return RUN_ALL_TESTS();
    }

    return 0;
}

但是,我正在使用 XCode 进行编译,但由于以下原因无法构建:

Undefined symbols for architecture x86_64:
  "Stack<int>::pop()", referenced from:
      StackTest_PushAndPop_Test::TestBody() in tests.o
  "Stack<int>::peek()", referenced from:
      StackTest_PushAndPeek_Test::TestBody() in tests.o
  "Stack<int>::push(int&)", referenced from:
      StackTest_PushAndPeek_Test::TestBody() in tests.o
      StackTest_PushAndPop_Test::TestBody() in tests.o
  "Stack<int>::Stack()", referenced from:
      StackTest_PushAndPeek_Test::TestBody() in tests.o
      StackTest_PushAndPop_Test::TestBody() in tests.o
  "Stack<int>::~Stack()", referenced from:
      StackTest_PushAndPeek_Test::TestBody() in tests.o
      StackTest_PushAndPop_Test::TestBody() in tests.o
  "testing::internal::EqFailure(char const*, char const*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, bool)", referenced from:
      testing::AssertionResult testing::internal::CmpHelperEQ<int, int>(char const*, char const*, int const&, int const&) in tests.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

编辑:通过包含 stack.cpp 我解决了与此相关的错误,但我仍然有以下错误:

Undefined symbols for architecture x86_64:
  "testing::internal::EqFailure(char const*, char const*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, bool)", referenced from:
      testing::AssertionResult testing::internal::CmpHelperEQ<int, int>(char const*, char const*, int const&, int const&) in tests.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
4

1 回答 1

2

乍一看,你好像在模板上犯了一个初学者的错误:模板必须在头文件中实现,你不能像普通的类和函数那样在头文件和 .cpp 文件中将它们分开。只需在 SO 上搜索“未定义的引用”和“模板”,您就会得到很多关于这件事的信息 :-)

于 2013-02-27T08:26:08.600 回答