Je suis en train de compiler une erreur dans VS2010 avec du code qui compile proprement dans VS2008.Erreur de compilation dans VC++ 2010 (clean in 2008!), "Appel ambigu à une fonction surchargée"
Voici mon erreur, de la fenêtre de sortie, avec toutes ses verbosité:
...\elementimpl.h(49): error C2668: 'std::basic_string<_Elem,_Traits,_Ax>::basic_string' : ambiguous call to overloaded function
with
[
_Elem=char,
_Traits=std::char_traits<char>,
_Ax=std::allocator<char>
]
c:\program files (x86)\microsoft visual studio 10.0\vc\include\xstring(700): could be 'std::basic_string<_Elem,_Traits,_Ax>::basic_string(std::basic_string<_Elem,_Traits,_Ax> &&)'
with
[
_Elem=char,
_Traits=std::char_traits<char>,
_Ax=std::allocator<char>
]
c:\program files (x86)\microsoft visual studio 10.0\vc\include\xstring(590): or 'std::basic_string<_Elem,_Traits,_Ax>::basic_string(const _Elem *)'
with
[
_Elem=char,
_Traits=std::char_traits<char>,
_Ax=std::allocator<char>
]
while trying to match the argument list '(CXStr)'
et est ici le code d'erreur, elementimpl.h (49):
std::string getAttributeValue(const char* attr) { return getAttribute(CXStr(attr)); }
getAttribute() renvoie un CXStr (un ADT), ce qui n'est évidemment pas accepté par le constructeur std :: string, il doit donc convertir le CXStr en quelque chose de convenable.
CXStr peut être converti en soit un std :: string ou un char *:
class CXStr
{
public:
...
CXStr(std::string);
CXStr(const char* ch);
const CXStr& operator=(const char*);
const CXStr& operator=(std::string&);
operator char*();
operator std::string();
...
}
Quand je lance à travers le débogueur dans VS2008, je peux voir qu'il passe par « CXStr opérateur :: std :: string() "après getAttribute(), puis il passe par le constructeur de la copie std :: string pour créer l'objet à renvoyer par getAttributeValue(). Je veux que mon build 2010 se comporte de la même manière.
Mes questions:
- Qu'est-ce que VS2010 fait différemment, et pourquoi?
- Quelle modification de code puis-je apporter à corriger cette erreur? Certaines choses qui travail, mais ne sont pas satisfaisants:
- commentant "CXStr :: opérateur char *()"; insatisfaisant car certains autre code utilise cette conversion.
- appelez explicitement "getAttribute (...). Opérateur std :: string()" at elementimpl.h (49); désagréable parce que cette même erreur se produit dans 27 endroits différents et il semble comme il doit y avoir une manière plus centralisée pour le réparer.
Quelle est la déclaration de 'getAttribute'? – GManNickG
CXStr getAttribute (const XMLCh * pAttr); – feuGene