如何从C++; 我尝试创建一个堆栈,并在C++谷歌测试框架的实验中使用谷歌测试来测试它。我已经设置了这个结构,所以我有一个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 } #包括“stack.h” #包括“gtest/gtest.h” 测试(堆叠测试、推拉测试){ Stack-intStack; INTA=12; int b=15; 期望_等式(12,intStack.push(a)); 期望_等式(15,intStack.push(b)); EXPECT_EQ(15,intStack.peek());//确保按后进先出顺序添加 EXPECT_EQ(15,intStack.peek());//应该仍然存在 } 测试(堆叠测试、推送和弹出){ Stack-intStack; INTA=12; int b=15; 期望_等式(12,intStack.push(a)); 期望_等式(15,intStack.push(b)); EXPECT_EQ(15,intStack.pop());//确保按后进先出顺序添加 EXPECT_EQ(12,intStack.pop());//应该删除15,然后删除12 EXPECT_EQ(-1,intStack.pop());//应该返回-1,因为堆栈上没有任何内容 }

如何从C++; 我尝试创建一个堆栈,并在C++谷歌测试框架的实验中使用谷歌测试来测试它。我已经设置了这个结构,所以我有一个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 } #包括“stack.h” #包括“gtest/gtest.h” 测试(堆叠测试、推拉测试){ Stack-intStack; INTA=12; int b=15; 期望_等式(12,intStack.push(a)); 期望_等式(15,intStack.push(b)); EXPECT_EQ(15,intStack.peek());//确保按后进先出顺序添加 EXPECT_EQ(15,intStack.peek());//应该仍然存在 } 测试(堆叠测试、推送和弹出){ Stack-intStack; INTA=12; int b=15; 期望_等式(12,intStack.push(a)); 期望_等式(15,intStack.push(b)); EXPECT_EQ(15,intStack.pop());//确保按后进先出顺序添加 EXPECT_EQ(12,intStack.pop());//应该删除15,然后删除12 EXPECT_EQ(-1,intStack.pop());//应该返回-1,因为堆栈上没有任何内容 },c++,unit-testing,tdd,stack,googletest,C++,Unit Testing,Tdd,Stack,Googletest,在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

在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;
}
#包括
#包括
#包括“gtest/gtest.h”
#包括“stack.h”
使用名称空间std;
int main(int argc,char*argv[])
{
字符串输入;

cout乍一看,似乎你在模板方面犯了一个初学者的错误:模板必须在头文件中实现,你不能像普通类和函数那样在头文件和.cpp文件中分离它们。只要这样,你就会得到很多关于这个问题的信息:-)

嗨,谢谢,这消除了一些错误,但是现在我还剩下两个。我已经用它们更新了原始帖子。我真的不确定问题是什么,可能是我运行测试的方式。看起来你没有链接gtest库。对不起!我刚刚解决了问题,如果有人遇到相同的问题,你需要在编译gtest和测试时使用相同的编译器。(即,明确选择GCC)对于任何C++的库,它们通常在不同编译器之间是不兼容的。甚至可以在同一编译器的不同版本之间存在差异。最后,我决定放弃GoGoestEt,因为它与XCode的使用完全过时了。我设法使其编译,但实际上用它来进行测试。收到dylib致命错误,所以我假设还有其他链接问题。我希望谷歌能尽快更新它。
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)
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)