| 
 | 
 
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册  
 
x
 
正在看C++程序设计这本书,其中写到const用法的时候有个例子树上写可以执行,但是我执行的时候出现了错误  ,请大神们看下代码: 
#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> 
这是为什么呢  
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++>
 
 
  复制代码 
 
 
 |   
 
 
 
 |