我正在尝试创建一个堆栈,并在使用C ++ Google Test框架的实验中使用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)
答案 0 :(得分:2)
初看起来好像你的初学者错误地使用了模板:模板必须在头文件中实现,你不能在头文件和.cpp文件中将它们分开,就像普通的类和函数一样。只需search for "undefined refernce" and "template"就可以了,您将获得有关此事的大量信息: - )