編譯器警告(等級 1)C4930 錯誤
阿新 • • 發佈:2019-02-09
環境:
win7 32bits
Visual Studio 2013
參考:https://msdn.microsoft.com/zh-cn/library/4ddd21xh.aspx
錯誤說明“prototype”: 未呼叫原型函式(是否是有意用變數定義的?
下列示例將產生C4930錯誤
// C4930.cpp // compile with: /W1 class Lock { public: int i; }; void f() { Lock theLock(); // C4930 // try the following line instead // Lock theLock; } int main() { }
當編譯器無法分清函式原型宣告與函式呼叫時,也會出現 C4930。
下面的示例生成 C4930:
// C4930b.cpp // compile with: /EHsc /W1 class BooleanException { bool _result; public: BooleanException(bool result) : _result(result) { } bool GetResult() const { return _result; } }; template<class T = BooleanException> class IfFailedThrow { public: IfFailedThrow(bool result) { if (!result) { throw T(result); } } }; class MyClass { public: bool MyFunc() { try { IfFailedThrow<>(MyMethod()); // C4930 // try one of the following lines instead // IfFailedThrow<> ift(MyMethod()); // IfFailedThrow<>(this->MyMethod()); // IfFailedThrow<>((*this).MyMethod()); return true; } catch (BooleanException e) { return e.GetResult(); } } private: bool MyMethod() { return true; } }; int main() { MyClass myClass; myClass.MyFunc(); }
在上面的示例中,不含引數的方法的結果作為引數傳遞給未命名本地類變數的建構函式。該呼叫會產生歧義:既可以是命名本地變數,也可以是使用物件例項以及相應的指向成員的指標運算子給方法呼叫加字首。
----------------------------------------------------------------------------------------------------------------------
在我自己的程式碼中如下情況會出現C4930錯誤:
在主函式中宣告優先順序佇列:class mycomparison { bool reverse; public: mycomparison() { //cout << "construct" << endl; } mycomparison(bool revparam) { reverse = revparam; } bool operator() (const int& lhs, const int&rhs) const { return lhs > rhs; // if (reverse) return (lhs>rhs); // else return (lhs<rhs); } };
int main()
{
priority_queue<int, vector<int>, mycomparison>pq(mycomparison());//C4930
system("pause");
return 0;
}
我以為我建立了一個臨時變數,而編譯器卻可以理解為一個函式宣告,返回值是priority_queue,函式名pq,引數是一個省去特定返回型別的函式指標。
可以做如下兩種修改:
第一種方式:
priority_queue<int, vector<int>, mycomparison>pq(( mycomparison() ));//補充括號
第二種方式:
priority_queue<int, vector<int>, mycomparison>pq(mycomparison::mycomparison());//補充::訪問符號
參考:http://stackoverflow.com/questions/12041509/possible-compiler-bug-for-c4930