#include<iostream>
using namespace std;
int main(){
cout<"Yes";
}
它可以編譯,但是當它運行時,它什么也不做。這是其他地方的編譯錯誤。編譯器是 gcc 4.9.2
和....相比
#include<iostream>
using namespace std;
int main(){
cout<<"Yes";
}
它缺少一個“<”,但它仍然可以編譯。
我預計它是一個編譯錯誤,就像變數一樣,如下所示:
> 6 6 C:\Users\Denny\Desktop\Codes\compileerror.cpp [Error] no match for
> 'operator<' (operand types are 'std::ostream {aka
> std::basic_ostream<char>}' and 'int')
這也發生在下面的代碼中。
#include<iostream>
using namespace std;
int main(){
cin>"Yes";
}
編輯:同樣的事情發生在
#include<iostream>
int main(){
std::cout<"Yes";
}
另外,我啟用了編譯器警告,但沒有。
uj5u.com熱心網友回復:
預設的C 中GCC <6.1標準(其中包括您4.9.2)是gnu 98,而對于GCC≥6.1它的gnu 14(如記錄例如此處)。因此,后者的編譯器默認不會接受此代碼,因為explicit operator bool()自 C 11 以來出現在 iostreams 中而不是operator void*()在 C 98 中(參見例如cppreference)。
如果您打開警告,您可能會收到警告:
$ g -4.8 test.cpp -o test -Wall
test.cpp: In function ‘int main()’:
test.cpp:5:15: warning: value computed is not used [-Wunused-value]
std::cout < "Yes";
^
其中test.cpp包含示例代碼:
#include <iostream>
int main()
{
std::cout < "Yes";
}
uj5u.com熱心網友回復:
在 C 11 之前,if (std::cin >> var)支持的方式是將流物件隱式轉換為void *.
因此,在應用轉換->和-> ->之后,std::cout<"Yes"評估為呼叫內置的。bool operator<(void*, const void*)std::basic_iosvoid *const char[4]const char*const void*
由于C 11,現在有,一個規則explicit,以轉換bool可被用if,while等。隨著在于,所述operator void*變更為explicit operator bool,和多載決議正確地沒有找到匹配為std::cout<"Yes"
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/346451.html
