C++ Google测试多模板-编译器错误
我正在研究这个问题的答案。但是我不能从那里的答案编译代码。为了测试代码,我创建了一个最小的(不幸的是不起作用)示例 编辑:我再次更新代码(考虑Marko Popovic的评论):C++ Google测试多模板-编译器错误,c++,templates,googletest,C++,Templates,Googletest,我正在研究这个问题的答案。但是我不能从那里的答案编译代码。为了测试代码,我创建了一个最小的(不幸的是不起作用)示例 编辑:我再次更新代码(考虑Marko Popovic的评论): #包括 #包括“gtest/gtest.h” 模板 结构whoami{void tellme(){printf(“我不知道!”);}; 模板 结构whoami{void tellme(){printf(“我是一个整数!”;}; 模板 结构whoami{void tellme(){printf(“我是一个角色!”);};
#包括
#包括“gtest/gtest.h”
模板
结构whoami{void tellme(){printf(“我不知道!”);};
模板
结构whoami{void tellme(){printf(“我是一个整数!”;};
模板
结构whoami{void tellme(){printf(“我是一个角色!”);};
模板
结构whoami{void tellme(){printf(“我是一个布尔值!”);};
模板
结构类型定义
{
typedef typename第一个类型;
typedef typename B secondType;
};
模板
类ATestExample:公共测试::测试
{
受保护的:
ATestExample(){}
虚拟~ATestExample(){}
};
typedef::testing::Types MyTypeList;
类型化测试用例(ATestExample,MyTypeList);
类型化测试(ATestExample,DefaultConstructor)
{
whoami信息;
info.tellme();
}
int main(int argc,字符**argv){
::测试::InitGoogleTest(&argc,argv);
返回运行所有测试();
}
但它仍然给出了错误:
debuging.cpp(..): error: a class or namespace qualified name is required
debuging.cpp(..): error: a class or namespace qualified name is required
debuging.cpp(..): error: nontype "gtest_TypeParam_::firstType" is not a type name
detected during:
implicit generation of "ATestExample_DefaultConstructor_Test<gtest_TypeParam_>::~ATestExample_DefaultConstructor_Test() [with gtest_TypeParam_=TypeDefs<char, char>]"
gtest/gtest.h(7209): here
instantiation of class "ATestExample_DefaultConstructor_Test<gtest_TypeParam_> [with gtest_TypeParam_=TypeDefs<char, char>]"
gtest/gtest.h(7209): here
implicit generation of "ATestExample_DefaultConstructor_Test<gtest_TypeParam_>::ATestExample_DefaultConstructor_Test() [with gtest_TypeParam_=TypeDefs<char, char>]"
gtest/gtest.h(7209): here
instantiation of class "ATestExample_DefaultConstructor_Test<gtest_TypeParam_> [with gtest_TypeParam_=TypeDefs<char, char>]"
debuging.cpp(..):错误:需要类或命名空间限定名
debuging.cpp(..):错误:需要类或命名空间限定名
debuging.cpp(..):错误:非类型“gtest_TypeParam_U3;::firstType”不是类型名称
在以下期间检测到:
隐式生成“ATestExample\u DefaultConstructor\u Test::~ATestExample\u DefaultConstructor\u Test()[使用gtest\u TypeParam\u=TypeDefs]”
gtest/gtest.h(7209):这里
实例化类“ATestExample\u DefaultConstructor\u Test[使用gtest\u TypeParam\uUx=TypeDefs]”
gtest/gtest.h(7209):这里
隐式生成“ATestExample\u DefaultConstructor\u Test::ATestExample\u DefaultConstructor\u Test()[使用gtest\u TypeParam\uuUx=TypeDefs]”
gtest/gtest.h(7209):这里
实例化类“ATestExample\u DefaultConstructor\u Test[使用gtest\u TypeParam\uUx=TypeDefs]”
编辑2:
解决方案是“typename”的位置置换:
模板
结构类型定义
{
typedef是第一种类型;
类型定义B第二类型;
};
模板
类ATestExample:公共测试::测试
{
受保护的:
ATestExample(){}
虚拟~ATestExample(){}
};
typedef::testing::Types MyTypeList;
类型化测试用例(ATestExample,MyTypeList);
类型化测试(ATestExample,DefaultConstructor)
{
whoami信息;
info.tellme();
}
您有两个错误。第一个是你已经命名了你的模板
structTypes
,与一起使用时会产生问题:testing::Types
。
假设您将其重命名为TypeDefs
。然后,在测试主体中,您在声明typedef
时出错,因为您没有指定模板参数。您必须这样做:
typedef TypeDefs<char, int>::firstType someType;
但这并不是编译器错误的答案。我更改了问题中的变量和类型命名
TypeParam
将在TestBody中包含当前类型。@wieschoo我没有注意到您编辑了这个问题。编辑改变了这个问题很多。在这个新版本的代码中生成了一个完全不同的编译器错误。我已经相应地编辑了我的答案。编译器错误仍然与我原来的问题中的错误相同,尽管您没有在我的代码中使用类型
作为名称,这是正确的。@wieschoo-Hmm,您使用的是最新版本的编译器和googletest吗。我使用的是最新的MSVC编译器和googletest的1.7.0版,这段代码编译起来没有任何问题……也许这又是MSVC的一个功能?我使用g++(ubuntu4.8.4-2ubuntu1~14.04)4.8.4
template <class A, class B>
struct TypeDefs
{
typedef A firstType;
typedef B secondType;
};
template <class T>
class ATestExample : public testing::Test
{
protected:
ATestExample() {}
virtual ~ATestExample(){ }
};
typedef ::testing::Types <TypeDefs<cpu,char>, TypeDefs<gpu,int> > MyTypeList;
TYPED_TEST_CASE(ATestExample, MyTypeList);
TYPED_TEST(ATestExample, DefaultConstructor)
{
whoami<typename TypeParam::firstType> info;
info.tellme();
}
typedef TypeDefs<char, int>::firstType someType;
whoami<TypeParam::firstType> info;
info.tellme();