1

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)
4

1 に答える 1

2

一見すると、テンプレートに関して初歩的なミスを犯したように見えます: テンプレートはヘッダー ファイルに実装する必要があり、通常のクラスや関数のようにヘッダーと .cpp ファイルでテンプレートを分離することはできません。SOで「未定義の参照」と「テンプレート」を検索するだけで、その問題に関する多くの情報が得られます:-)

于 2013-02-27T08:26:08.600 に答える