2017-10-04 62 views
-1

給錯誤screenshot.save('screenshot.png', 'PNG');QT5 - 無法從 '詮釋' 轉換參數2「爲const char *

..\screen\main.cpp(10): error C2015: too many characters in constant 
..\screen\main.cpp(10): error C2664: 'bool QPixmap::save(QIODevice *,const char *,int) const': cannot convert argument 2 from 'int' to 'const char *' 

代碼:

#include "mainwindow.h" 
#include <QApplication> 
#include <QScreen> 
#include <QPixmap> 

int main(int argc, char *argv[]) { 
    QApplication a(argc, argv); 
    QScreen *screen = a.primaryScreen(); 
    QPixmap screenshot = screen->grabWindow(0); 
    screenshot.save('screenshot.png', 'PNG'); 

    MainWindow w; 
    w.show(); 
    return a.exec(); 
} 
+3

你必須使用'「'的字符串,而不是''' 。 – nwp

+3

C++不是Python,不要假設,[學習它](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Drop

+0

[這解釋](http: //en.cppreference.com/w/cpp/language/character_literal)int是從哪裏來的。 – nwp

回答

3

變化

screenshot.save('screenshot.png', 'PNG'); 

screenshot.save("screenshot.png", "PNG"); 

您可以在單個字符中使用撇號,如'a'。 如果函數需要的const char *參數類型,你必須使用雙引號(「),即使你寫有單個字符,如‘一’

‘一’ - 類型爲const char的

‘一’ - 類型爲const char *相關

如果你寫裏面撇多個字符,編譯器無法將其轉換爲char,將轉換爲int。

+1

更確切地說'a'是char,'screenshot.png'不能表示爲char,因此它被表示爲int。 – deW1

+0

我已經更新了我的答案 –

相關問題