저는 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;
}
내가이 프로그램을 컴파일
: 정확하게 오류가 무엇인지 말씀 해주십시오 nybody 수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
:
내가 아래에 오류가?
이 해결 방법이 작동합니다. 하지만 max의 범위 분석 연산자를 유지하여 std :: max를 호출해서는 안됩니다.그럼에도 왜 오류가오고 있습니까? – Vijay
이유는 avakar가 설명하는 이유입니다. "000001.hpp"헤더에'max'의 정의 만있는 것이 사실이라면 문제는 컴파일러가 전역 이름 공간에 몇 가지 표준 기호를 주입하는 것입니다. – Manuel