Oto mój kod:Nie powinien char * niejawnie przekonwertować na std :: string?
#include <iostream>
using namespace std;
struct ST {};
bool operator==(const struct ST *s1, const string &s2) {
return true;
}
int main() {
struct ST *st = new ST();
const char *p = "abc";
if (st == p) {
return 0;
}
return 1;
}
otrzymuję błąd kompilacji:
prog.cpp:14:12: error: comparison between distinct pointer types ‘ST*’ and ‘const char*’ lacks a cast [-fpermissive]
if (st == p) {
^
Zastanawiam się dlaczego niejawna konwersja z char * na ciąg nie działa tutaj?
UPDATE odpowiedź Antona sens, I uaktualniony kod:
#include <string>
using namespace std;
struct ST {};
bool operator==(const struct ST s1, const string &s2) {
return true;
}
int main() {
struct ST st;
const char *p = "abc";
if (st == p) {
return 0;
}
return 1;
}
teraz kompiluje.
Tutaj można porównać różne typy wskaźników. Twój przeciążony operator nie jest wywoływany. – 101010
Nie dotyczy Twojego problemu, ale ''include' nie jest tutaj wymagane. '#include ' jest. –