关于const的一个问题
正在看C++程序设计这本书,其中写到const用法的时候有个例子树上写可以执行,但是我执行的时候出现了错误{:7_122:} ,请大神们看下代码:#include<iostream>
using namespace std;
int negate(const int &val);
int main()
{
int v = 15;
cout<< v << " negate is " <<negate(v)<< endl;
return 0;
}
int negate(const int &val)
{
return -val;
}
编译日志是这样的:note: template<class _Tp> struct std::negate
struct negate : public unary_function<_Tp, _Tp>
这是为什么呢{:7_138:} vs2017没问题
同上{:5_94:} http://www.cplusplus.com/reference/functional/negate/
std空间已经有了negate这个名字了
C:\VisualStudioProjects\C++\C++>cat main.cpp
#include<iostream>
using namespace std;
int negate(const int &val);
int main()
{
int v = 15;
cout << v << " negate is " << negate(v) << endl;
return 0;
}
int negate(const int &val)
{
return -val;
}
C:\VisualStudioProjects\C++\C++>g++ main.cpp
main.cpp: In function 'int main()':
main.cpp:9:32: error: reference to 'negate' is ambiguous
cout << v << " negate is " << negate(v) << endl;
^~~~~~
main.cpp:4:5: note: candidates are: int negate(const int&)
int negate(const int &val);
^~~~~~
In file included from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/string:48:0,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/locale_classes.h:40,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/ios_base.h:41,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ios:42,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/iostream:39,
from main.cpp:1:
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/stl_function.h:162:12: note: template<class _Tp> struct std::negate
struct negate;
^~~~~~
C:\VisualStudioProjects\C++\C++>vim main.cpp
C:\VisualStudioProjects\C++\C++>cat main.cpp
#include<iostream>
//using namespace std;
int negate(const int &val);
int main()
{
int v = 15;
std::cout << v << " negate is " << negate(v) << std::endl;
return 0;
}
int negate(const int &val)
{
return -val;
}
C:\VisualStudioProjects\C++\C++>g++ main.cpp
C:\VisualStudioProjects\C++\C++>
人造人 发表于 2018-7-28 17:39
vs2017没问题
好吧,应该是编译器的问题,我用的是Dev-C++,有可能是函数名字和编译器内置的函数名重复了,换了个函数名就好了。谢啦{:7_129:}
页:
[1]