鱼C论坛

 找回密码
 立即注册
查看: 1928|回复: 4

[已解决]关于const的一个问题

[复制链接]
发表于 2018-7-28 17:29:04 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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>
这是为什么呢
最佳答案
2018-7-28 17:48:00
http://www.cplusplus.com/reference/functional/negate/
std空间已经有了negate这个名字了

  1. C:\VisualStudioProjects\C++\C++>cat main.cpp
  2. #include<iostream>
  3. using namespace std;

  4. int negate(const int &val);

  5. int main()
  6. {
  7.         int v = 15;
  8.         cout << v << " negate is " << negate(v) << endl;
  9.         return 0;
  10. }

  11. int negate(const int &val)
  12. {
  13.         return -val;
  14. }

  15. C:\VisualStudioProjects\C++\C++>g++ main.cpp
  16. main.cpp: In function 'int main()':
  17. main.cpp:9:32: error: reference to 'negate' is ambiguous
  18.   cout << v << " negate is " << negate(v) << endl;
  19.                                 ^~~~~~
  20. main.cpp:4:5: note: candidates are: int negate(const int&)
  21. int negate(const int &val);
  22.      ^~~~~~
  23. In file included from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/string:48:0,
  24.                  from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/locale_classes.h:40,
  25.                  from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/ios_base.h:41,
  26.                  from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ios:42,
  27.                  from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ostream:38,
  28.                  from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/iostream:39,
  29.                  from main.cpp:1:
  30. /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
  31.      struct negate;
  32.             ^~~~~~

  33. C:\VisualStudioProjects\C++\C++>vim main.cpp

  34. C:\VisualStudioProjects\C++\C++>cat main.cpp
  35. #include<iostream>
  36. //using namespace std;

  37. int negate(const int &val);

  38. int main()
  39. {
  40.         int v = 15;
  41.         std::cout << v << " negate is " << negate(v) << std::endl;
  42.         return 0;
  43. }

  44. int negate(const int &val)
  45. {
  46.         return -val;
  47. }

  48. C:\VisualStudioProjects\C++\C++>g++ main.cpp

  49. C:\VisualStudioProjects\C++\C++>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-7-28 17:39:57 | 显示全部楼层
vs2017没问题

4.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-7-28 17:44:17 | 显示全部楼层
同上
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-7-28 17:48:00 | 显示全部楼层    本楼为最佳答案   
http://www.cplusplus.com/reference/functional/negate/
std空间已经有了negate这个名字了

  1. C:\VisualStudioProjects\C++\C++>cat main.cpp
  2. #include<iostream>
  3. using namespace std;

  4. int negate(const int &val);

  5. int main()
  6. {
  7.         int v = 15;
  8.         cout << v << " negate is " << negate(v) << endl;
  9.         return 0;
  10. }

  11. int negate(const int &val)
  12. {
  13.         return -val;
  14. }

  15. C:\VisualStudioProjects\C++\C++>g++ main.cpp
  16. main.cpp: In function 'int main()':
  17. main.cpp:9:32: error: reference to 'negate' is ambiguous
  18.   cout << v << " negate is " << negate(v) << endl;
  19.                                 ^~~~~~
  20. main.cpp:4:5: note: candidates are: int negate(const int&)
  21. int negate(const int &val);
  22.      ^~~~~~
  23. In file included from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/string:48:0,
  24.                  from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/locale_classes.h:40,
  25.                  from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/ios_base.h:41,
  26.                  from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ios:42,
  27.                  from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ostream:38,
  28.                  from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/iostream:39,
  29.                  from main.cpp:1:
  30. /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
  31.      struct negate;
  32.             ^~~~~~

  33. C:\VisualStudioProjects\C++\C++>vim main.cpp

  34. C:\VisualStudioProjects\C++\C++>cat main.cpp
  35. #include<iostream>
  36. //using namespace std;

  37. int negate(const int &val);

  38. int main()
  39. {
  40.         int v = 15;
  41.         std::cout << v << " negate is " << negate(v) << std::endl;
  42.         return 0;
  43. }

  44. int negate(const int &val)
  45. {
  46.         return -val;
  47. }

  48. C:\VisualStudioProjects\C++\C++>g++ main.cpp

  49. C:\VisualStudioProjects\C++\C++>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-7-28 17:50:18 | 显示全部楼层

好吧,应该是编译器的问题,我用的是Dev-C++,有可能是函数名字和编译器内置的函数名重复了,换了个函数名就好了。谢啦
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-10 00:08

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表