我正在尝试创建一个堆栈,并在 C++ Google 测试框架的实验中使用 Google 测试对其进行测试。我已经设置了结构,因此我有一个 stack.h 和 stack.cpp 用于实现,然后我有一个包含以下代码的tests.cpp。我有几个问题:首先,是否可以像我所做的那样从主函数调用我的测试函数?另外,我是否正确地包含了所有内容?为什么会发生构建错误?抱歉,我是 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)
最佳答案
乍一看,似乎您在模板方面犯了一个初学者错误:模板必须在头文件中实现,您不能像普通的类和函数一样在头文件和 .cpp 文件中将它们分开。只是search for "undefined refernce" and "template"上 SO,你会得到很多关于此事的信息:-)
关于c++ - 如何从 C++ 中的另一个主函数调用 Google Tests,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15104556/