C++模板问题
我是 C++ 模板的新手。 我正在尝试一些小程序。
CPP [80]> cat 000001.cpp 000001.hpp
#include <iostream>
#include <string>
#include "000001.hpp"
int main()
{
int i = 42;
std::cout << "max(7,i): " << ::max(7,i) << std::endl;
double f1 = 3.4;
double f2 = -6.7;
std::cout << "max(f1,f2): " << ::max(f1,f2) << std::endl;
std::string s1 = "mathematics";
std::string s2 = "math";
std::cout << "max(s1,s2): " << ::max(s1,s2) << std::endl;
}
template <typename T>
inline T const& max (T const& a, T const& b)
{
return a < b ? b : a;
}
当我编译这个程序时:
我收到以下错误:
CPP [78]> /opt/aCC/bin/aCC -AA 000001.cpp
Error (future) 229: "/opt/aCC/include_std/string.cc", line 164 # "Ambiguous overloaded function call; a
function match was not found that was strictly best for ALL arguments. Two functions that matched
best for some arguments (but not all) were "const unsigned long &max<unsigned long>(const unsigned
long &,const unsigned long &)" ["000001.hpp", line 2] and "const unsigned long &std::max<unsigned
long>(const unsigned long &,const unsigned long &)" ["/opt/aCC/include_std/algorithm", line 1762]."
Choosing "const unsigned long &max<unsigned long>(const unsigned long &,const unsigned long &)"
["000001.hpp", line 2] for resolving ambiguity.
_C_data = _C_getRep (max (_RW::__rw_new_capacity (0, this),
^^^
Warning: 1 future errors were detected and ignored. Add a '+p' option to detect and fix them before they become fatal errors in a future release. Behavior of this ill-formed program is not guaranteed to match that of a well-formed program
有人可以告诉我错误到底是什么吗?
I am new to templates in c++.
i was trying some small programs.
CPP [80]> cat 000001.cpp 000001.hpp
#include <iostream>
#include <string>
#include "000001.hpp"
int main()
{
int i = 42;
std::cout << "max(7,i): " << ::max(7,i) << std::endl;
double f1 = 3.4;
double f2 = -6.7;
std::cout << "max(f1,f2): " << ::max(f1,f2) << std::endl;
std::string s1 = "mathematics";
std::string s2 = "math";
std::cout << "max(s1,s2): " << ::max(s1,s2) << std::endl;
}
template <typename T>
inline T const& max (T const& a, T const& b)
{
return a < b ? b : a;
}
when i compile this program:
i get an error below:
CPP [78]> /opt/aCC/bin/aCC -AA 000001.cpp
Error (future) 229: "/opt/aCC/include_std/string.cc", line 164 # "Ambiguous overloaded function call; a
function match was not found that was strictly best for ALL arguments. Two functions that matched
best for some arguments (but not all) were "const unsigned long &max<unsigned long>(const unsigned
long &,const unsigned long &)" ["000001.hpp", line 2] and "const unsigned long &std::max<unsigned
long>(const unsigned long &,const unsigned long &)" ["/opt/aCC/include_std/algorithm", line 1762]."
Choosing "const unsigned long &max<unsigned long>(const unsigned long &,const unsigned long &)"
["000001.hpp", line 2] for resolving ambiguity.
_C_data = _C_getRep (max (_RW::__rw_new_capacity (0, this),
^^^
Warning: 1 future errors were detected and ignored. Add a '+p' option to detect and fix them before they become fatal errors in a future release. Behavior of this ill-formed program is not guaranteed to match that of a well-formed program
Could nybody please tell me what exactly the error is?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可能在某处包含
而不是
。前者已经不存在一段时间了,但出于兼容性原因,编译器仍然接受包含并将其替换为这会导致
std::max
被引入全局命名空间,从而导致含糊不清。将
替换为
或重命名max
函数,问题就会消失。编辑:你显然已经修复了包含,但我敢打赌你仍然在某处
using namespace std;
。你需要摆脱它。事实上,您永远不应该在全局范围内使用using namespace
。编辑:您可能还在某处
使用 std::max
。你也需要摆脱它。You are probably including
<iostream.h>
instead of<iostream>
somewhere. The former hasn't existed for some time now, but for compatibility reasons, you compiler still accepts the include and replaces it withThis causes
std::max
to be brought to the global namespace, thus resulting in an ambiguity. Replace<iostream.h>
with<iostream>
or rename yourmax
function and the problem should disappear.Edit: You've apparently fixed the include, but I bet you still have
using namespace std;
somewhere. You need to get rid of that. In fact you should never useusing namespace
in the global scope.Edit: You might also have
using std::max
somewhere. You need to get rid of it too.您发布的代码编译得很好,“000001.hpp”内部肯定还有其他问题。您也可以发布该文件的内容吗?
编辑:如果您按照 avakar 的说明进行操作,但问题仍然存在,则一定是由于您的编译器存在问题。我能想到两种明显的解决方法:将您的 max 函数重命名为其他名称,或者将其放入命名空间中:
The code you've posted compiles just fine, there must be something else that is wrong inside "000001.hpp". Can you post the contents of that file too?
Edit: If you do as avakar says but the problem persists, that must be due to some problem with your compiler. There are two obvious workarounds I can think of: rename your
max
function to something else, or put it in a namespace:我不知道您使用的是哪个编译器,但第二个错误告诉您以下两个函数发生冲突:
这似乎很奇怪,您可能在某处有一个
using namespace std;
或更糟的是,那个如第一个错误中所述,您的包含使用iostream.h
。您能否提供有关您的编译器/工具链以及 .hpp 文件内容的更多信息?I don't know which compiler you are using but the second error tells you that the two following functions are clashing :
It seems really weird, you may have a
using namespace std;
somewhere or worse, that one of your include useiostream.h
as noted in the first error. Could you give more information about your compiler/toolchain and the content of your .hpp file?它说
找到了两个定义。一个定义位于 000001.hpp 中,另一个定义位于 /opt/aCC/include_std/algorithm 中。编译器暂时选择了000001.hpp中的那个,所以现在不存在错误。但它说这两个定义将来可能会导致错误。
It says that two definitions for
were found. One definition is in 000001.hpp and the other is in /opt/aCC/include_std/algorithm. The compiler chose the one in 000001.hpp for now, so no error is present now. But it says that these two definitions may cause errors in the future.
我不知道这是否会导致问题,但无论如何;您不应该将名称 max 用于全局(或局部)函数,因为它是 STL 的一部分。
I don't know if this causes the problem, but anyhow; you shouldnt use the name max for your global (or local) function as it is a part of STL.