c++ - Googletest 编译错误 : ‘xyzTest’ was not declared in this scope

标签 c++ compiler-errors g++ googletest

我正在学习 TDD,使用 GoogleTest 框架。我已经成功构建了 Gtest,并且能够构建和运行示例。但是,当我尝试编写一个简单的示例时,我遇到了编译错误。

这是我使用的源代码和构建命令:

// ################################################
//proj1.h
#ifndef  __SCRATCH_PROJ1_H
#define  __SCRATCH_PROJ1_H

int addOne(int i);

#endif /*__SCRATCH_PROJ1_H */

// ################################################

//proj1.cpp
#include "proj1.h"

int addOne(int i){
    return i+1;
}


// ################################################
//proj1_unittest.cpp

#include "proj1.h"
#include "gtest/gtest.h"

// Test Function
TEST(addOneTest, Positive) {
    EXPECT_EQ(1,addOneTest(0));            // <- Line # 24
    EXPECT_EQ(2,addOneTest(1));            // <- Line # 25
    EXPECT_EQ(40320, addOneTest(40319));   // <- Line # 26
}

TEST(addOneTest, Negative) {
    EXPECT_FALSE(addOneTest(-1));          // <- Line # 30
}


GTEST_API_ int main(int argc, char **argv) {
  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}

控制台输出:

g++ -isystem ${GTEST_DIR}/include -pthread -c /home/user1/scratch/proj1_unittest.cpp /home/user1/scratch/proj1_unittest.cpp: In member function ‘virtual void addOneTest_Positive_Test::TestBody()’: /home/user1/scratch/proj1_unittest.cpp:24:5: error: ‘addOneTest’ was not declared in this scope /home/user1/scratch/proj1_unittest.cpp:25:5: error: ‘addOneTest’ was not declared in this scope /home/user1/scratch/proj1_unittest.cpp:26:5: error: ‘addOneTest’ was not declared in this scope /home/user1/scratch/proj1_unittest.cpp: In member function ‘virtual void addOneTest_Negative_Test::TestBody()’: /home/user1/scratch/proj1_unittest.cpp:30:5: error: ‘addOneTest’ was not declared in this scope



从错误消息中的行号来看,似乎没有定义 EXPECT_* 宏 - 但是,我已将 gtest/gtest.h 包含在编译单元中。

是什么导致了这些错误 - 我该如何解决?

最佳答案

正如它所说,addOneTest没有在任何地方宣布。我猜你打算调用 addOne反而。

关于c++ - Googletest 编译错误 : ‘xyzTest’ was not declared in this scope,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18263464/

相关文章:

c - 结构和指针有问题(不兼容的指针类型)

c++ - 错误: some class is not a template

java - 我的Java程序已编译但无法运行

c++ - Flex(Lex) 输出不正确

c++ - 使用 g++ 编译时出现非法指令

c++ - 在 C++ 中,打开包含值的 .txt 文件后,读取该文件会给出 ""

c++ - Push_back和pop_back的实现

c++ - 将 boost::log::expressions::attr< std::string > 转换为 std::string

c++ - 为什么不能在声明中包含多个 decl-specifier-seq?

c++ - g++ 将返回的字符串文字视为 const char 指针而不是 const char 数组