C++ CUDA错误:名称后面跟着“:”&引用;必须是类或命名空间
我正在编写我的第一个CUDA程序,使用C++ CUDA错误:名称后面跟着“:”&引用;必须是类或命名空间,c++,compiler-errors,cuda,nvcc,C++,Compiler Errors,Cuda,Nvcc,我正在编写我的第一个CUDA程序,使用nvcc编译器时遇到了错误,如果我使用g++编译,就不会遇到这种情况 我的代码: #include <iostream> #include <cmath> using namespace std; double distance(double first, double second); int main(){ double dis; dis = distance(7.0, 1.0); cout <<
nvcc
编译器时遇到了错误,如果我使用g++
编译,就不会遇到这种情况
我的代码:
#include <iostream>
#include <cmath>
using namespace std;
double distance(double first, double second);
int main(){
double dis;
dis = distance(7.0, 1.0);
cout << "distance = " << dis << endl;
return 0;
}
double distance(double first, double second){
double diff;
diff = abs(first-second);
return diff;
}
当我将文件扩展名更改为.cpp并按如下方式编译时,g++test.cpp-o test
,代码符合要求。如果然后执行/test
,我会得到我想要的结果:
distance = 6
查看帖子启发我考虑从主机/设备划分错误的地方调用一些东西的可能性,但是,我现在还没有做任何GPU调用。p>
不确定发生了什么,但到目前为止,CUDA编译器似乎非常挑剔。您需要将
-std=c++11
选项添加到nvcc以编译此代码。通过使用std名称空间,您会与std::distance
发生冲突,这需要c++11或更高版本使用nvcc进行编译
这项工作:
$ cat bugaboo.cu
#include <iostream>
#include <cmath>
using namespace std;
double distance(double first, double second);
int main(){
double dis;
dis = distance(7.0, 1.0);
cout << "distance = " << dis << endl;
return 0;
}
double distance(double first, double second){
double diff;
diff = abs(first-second);
return diff;
}
$ nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2018 NVIDIA Corporation
Built on Tue_Jun_12_23:07:04_CDT_2018
Cuda compilation tools, release 9.2, V9.2.148
$ nvcc --std=c++11 -o bugaboo bugaboo.cu
$ ./bugaboo
distance = 6
$cat bugaboo.cu
#包括
#包括
使用名称空间std;
双倍距离(双倍第一、双倍第二);
int main(){
双dis;
dis=距离(7.0,1.0);
cout Off-topic:About…Hm,如果再次阅读错误消息,我之前的评论似乎没有最初出现的那么离题-已经有了,并且您自己的distance
函数似乎与…@Aconcagua发生了冲突,您解决了!将我的函数名更改为dist(…)
解决了这个问题。更好的方法是:不要使用命名空间std;
如果编写std::cout
对你来说确实太多了,那么就改用使用std::cout;
吧。你可以在你自己的命名空间中另外放置你自己的函数。@Acocagua这里有一个建议:如果你非常愿意帮助学习如果你想对某人的问题发表评论,试着用一种不会让人觉得是屈尊俯就的方式来做。提示:请记住,有些人对软件开发和/或特定编程语言是新手。
$ cat bugaboo.cu
#include <iostream>
#include <cmath>
using namespace std;
double distance(double first, double second);
int main(){
double dis;
dis = distance(7.0, 1.0);
cout << "distance = " << dis << endl;
return 0;
}
double distance(double first, double second){
double diff;
diff = abs(first-second);
return diff;
}
$ nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2018 NVIDIA Corporation
Built on Tue_Jun_12_23:07:04_CDT_2018
Cuda compilation tools, release 9.2, V9.2.148
$ nvcc --std=c++11 -o bugaboo bugaboo.cu
$ ./bugaboo
distance = 6
$ nvcc -o bugaboo bugaboo.cu
/usr/include/c++/4.8/bits/stl_iterator_base_types.h(165): error: a class or namespace qualified name is required
detected during instantiation of class "std::iterator_traits<_Iterator> [with _Iterator=double]"
bugaboo.cu(10): here
/usr/include/c++/4.8/bits/stl_iterator_base_types.h(165): error: global-scope qualifier (leading "::") is not allowed
detected during instantiation of class "std::iterator_traits<_Iterator> [with _Iterator=double]"
bugaboo.cu(10): here
/usr/include/c++/4.8/bits/stl_iterator_base_types.h(165): error: expected a ";"
detected during instantiation of class "std::iterator_traits<_Iterator> [with _Iterator=double]"
bugaboo.cu(10): here
/usr/include/c++/4.8/bits/stl_iterator_base_types.h(166): error: a class or namespace qualified name is required
detected during instantiation of class "std::iterator_traits<_Iterator> [with _Iterator=double]"
bugaboo.cu(10): here
/usr/include/c++/4.8/bits/stl_iterator_base_types.h(166): error: global-scope qualifier (leading "::") is not allowed
detected during instantiation of class "std::iterator_traits<_Iterator> [with _Iterator=double]"
bugaboo.cu(10): here
/usr/include/c++/4.8/bits/stl_iterator_base_types.h(166): error: expected a ";"
detected during instantiation of class "std::iterator_traits<_Iterator> [with _Iterator=double]"
bugaboo.cu(10): here
/usr/include/c++/4.8/bits/stl_iterator_base_types.h(167): error: a class or namespace qualified name is required
detected during instantiation of class "std::iterator_traits<_Iterator> [with _Iterator=double]"
bugaboo.cu(10): here
/usr/include/c++/4.8/bits/stl_iterator_base_types.h(167): error: global-scope qualifier (leading "::") is not allowed
detected during instantiation of class "std::iterator_traits<_Iterator> [with _Iterator=double]"
bugaboo.cu(10): here
/usr/include/c++/4.8/bits/stl_iterator_base_types.h(167): error: expected a ";"
detected during instantiation of class "std::iterator_traits<_Iterator> [with _Iterator=double]"
bugaboo.cu(10): here
/usr/include/c++/4.8/bits/stl_iterator_base_types.h(168): error: a class or namespace qualified name is required
detected during instantiation of class "std::iterator_traits<_Iterator> [with _Iterator=double]"
bugaboo.cu(10): here
/usr/include/c++/4.8/bits/stl_iterator_base_types.h(168): error: global-scope qualifier (leading "::") is not allowed
detected during instantiation of class "std::iterator_traits<_Iterator> [with _Iterator=double]"
bugaboo.cu(10): here
/usr/include/c++/4.8/bits/stl_iterator_base_types.h(168): error: expected a ";"
detected during instantiation of class "std::iterator_traits<_Iterator> [with _Iterator=double]"
bugaboo.cu(10): here
/usr/include/c++/4.8/bits/stl_iterator_base_types.h(169): error: a class or namespace qualified name is required
detected during instantiation of class "std::iterator_traits<_Iterator> [with _Iterator=double]"
bugaboo.cu(10): here
/usr/include/c++/4.8/bits/stl_iterator_base_types.h(169): error: global-scope qualifier (leading "::") is not allowed
detected during instantiation of class "std::iterator_traits<_Iterator> [with _Iterator=double]"
bugaboo.cu(10): here
/usr/include/c++/4.8/bits/stl_iterator_base_types.h(169): error: expected a ";"
detected during instantiation of class "std::iterator_traits<_Iterator> [with _Iterator=double]"
bugaboo.cu(10): here
15 errors detected in the compilation of "/tmp/tmpxft_00000acd_00000000-8_bugaboo.cpp1.ii".