I'm writing a type-parameterized test fixture using gtest, and I'm seeing a not declared in this scope
compiler error when I try to use members from the fixture class.
When I'm not using a type-parameterized fixture, I can access class members just fine. When I am using a type-parameterized fixture, I can use this->(member name)
to access the member. But why can't I use the member name explicitly? Is there a way I can avoid littering my test code with this->
all over the place?
Here's a trivial reproduction:
Test_Sanity.h:
#include "gtest/gtest.h"
/* Using a member in a simple, non-paramed fixture: */
class SimpleFixture : public ::testing::Test {
protected:
int N;
};
TEST_F(SimpleFixture, FooTest) {
N=6;
ASSERT_LE(0,N);
}
/* Using a member in a type-parameterized fixture: */
template <typename PARAM_T>
class TypeParamFixture : public ::testing::Test {
protected:
int N;
};
TYPED_TEST_CASE_P(TypeParamFixture);
TYPED_TEST_P(TypeParamFixture, FooTest) {
N=6; /* COMPILE-ERROR: ‘N’ was not declared in this scope */
ASSERT_LE(0,N);
}
/* As above, but using this->N */
TYPED_TEST_P(TypeParamFixture, FooTestUsingThisPtr) {
this->N=6; /* No compilation error */
ASSERT_LE(0,this->N);
}
/* Registration and instantiation of type-paramed tests */
REGISTER_TYPED_TEST_CASE_P(TypeParamFixture, FooTest, FooTestUsingThisPtr);
struct StructA {
int myInt;
double myDouble;
};
struct StructB {
char myCharArr[42];
};
typedef ::testing::Types<StructA, StructB> MyTypes;
INSTANTIATE_TYPED_TEST_CASE_P(Sanity, TypeParamFixture, MyTypes );
I compile the above with the boilerplate gtest main, and get a compilation error only for the use of N
in TypeParamFixture/FooTest
.