在C++中可以在函式定義時可以使用:來為該函式賦初值。
有一種寫法是將建構式寫在:后面,來為其他函式賦初值。
但是我這么用的時候編輯器卻報錯,代碼大致如下:
BMPImage::BMPImage():m_width(0),m_height(0),m_depth(8),m_row_size(0),
m_data(nullptr),m_color_palette_size(0),m_color_palette(nullptr) {}
此乃建構式,類名為BMPImage,后面跟的都是賦初值的變數。
然后我又定義了其他函式,使用建構式來為其賦初值:
BMPImage::BMPImage(const char* bmp_file_path):BMPImage() {}
此乃另一個函式,就是用:來呼叫建構式來為其賦初值,然后編輯器就報了如下錯:
C:\Users\User\Desktop\MKD\extra\BMPImage.cpp|36|error: no matching function for call to 'BMPImage::BMPImage()'
這個建構式我在.h檔案中也宣告了:
explicit BMPImage();
求問有人知道這是怎么回事么,是我寫錯了,還是就是沒有這種寫法呢?
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/135722.html
標籤:C++ 語言
上一篇:浮點數變數判等問題
下一篇:編程新手求助!
