鱼C论坛

 找回密码
 立即注册
查看: 1597|回复: 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这个名字了
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++>
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

4.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-7-28 17:44:17 | 显示全部楼层
同上
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-7-28 17:48:00 | 显示全部楼层    本楼为最佳答案   
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++>
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

好吧,应该是编译器的问题,我用的是Dev-C++,有可能是函数名字和编译器内置的函数名重复了,换了个函数名就好了。谢啦
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-2 06:27

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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