Cuda 为什么NVCC无法将函数定义与现有声明匹配?

Cuda 为什么NVCC无法将函数定义与现有声明匹配?,cuda,Cuda,我有这些文件,在使用nvcc编译时会产生以下错误 error C2244: 'TemplateClass<N>::print': unable to match function definition to an existing declaration note: see declaration of 'TemplateClass<N>::print' note: definition note: 'void TemplateClass<N>::prin

我有这些文件,在使用nvcc编译时会产生以下错误

error C2244: 'TemplateClass<N>::print': unable to match function definition to an existing declaration 
note: see declaration of 'TemplateClass<N>::print' 
note: definition note: 'void TemplateClass<N>::print(const std::string [])' 
note: existing declarations note: 'void TemplateClass<N>::print(const std::string [N])'

出于好奇,尝试使用std::string namesf={“un”、“deux”、“trois”};这似乎是编译器的问题。尝试不同的格式可能有助于编译器更好地理解。否则代码似乎就可以了。
也许你错过了一些与CMake的联系。还可以通过创建CUDA项目直接从VS2017编译而不使用CMake。

发生的情况是数组正在衰减为指针,并且在编译过程中数组的大小丢失。那么这个

template <unsigned int N>
void TemplateClass<N>::print(const std::string familyName[N]);
模板
void TemplateClass::print(const std::string familyName[N]);
实际上会变成这样

template <unsigned int N>
void TemplateClass<N>::print(const std::string* familyName);
模板
void TemplateClass::print(const std::string*familyName);
正如我们所看到的,编译器无法知道它必须根据数组的大小(即模板参数N)生成不同的函数

为了解决这个问题,我们可以使用一个老技巧来避免像这样的阵列衰减

cmake_minimum_required(VERSION 3.10)

project(template_inl_file LANGUAGES CXX CUDA)

set (lib_files template.h consume_template.cu)
add_library(template_inl_file_lib ${lib_files})
template <unsigned int N>
void TemplateClass<N>::print(const std::string (&familyName)[N]);
模板
void TemplateClass::print(const std::string(&familyName)[N]);
现在大小N在编译过程中出现,编译器知道要生成不同的函数。我猜,正如在问题的评论中指出的那样,NVCC生成的代码不是VS自己生成的,然后它不知道如何处理它

有关该主题的更多信息,请访问以下链接


您缺少文件
consume\u template.cpp
。这与问题无关,在.cu文件中包含
template.h
时会发生这种情况。请允许我编辑我的帖子以避免混淆。它使用
nvcc
gcc
编译。在我的例子中,
gcc
抱怨
for
循环中
auto
来自
template.inl
,但将其更改为
int
是我编译文件所要做的一切。在linux上的CUDA 9.1上,它在
cu
文件中为我工作。这里没有足够的信息来确定问题所在,我也不打算通过CMake来查看是否存在错误。如果你没有使用CUDA9.1,你可能想试试。或者可以在developer.nvidia.com上提交一个bug。最明显的一点是,错误不是由nvcc发出的,而是由。所以,充其量只是nvcc正在生成VS无法编译的代码。这应该很容易检查
template <unsigned int N>
void TemplateClass<N>::print(const std::string familyName[N]);
template <unsigned int N>
void TemplateClass<N>::print(const std::string* familyName);
template <unsigned int N>
void TemplateClass<N>::print(const std::string (&familyName)[N]);