簡體   English   中英

錯誤C2248:無法訪問類中聲明的私有成員

[英]error C2248: cannot access private member declared in class

我在c ++應用程序中收到此錯誤:

error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' :
              cannot access private member declared in class '

我在stackoverflow中看到了類似的問題,但我無法弄清楚我的代碼有什么問題。 有人能幫我嗎?

    //header file
class batchformat {
    public:
        batchformat();
        ~batchformat();
        std::vector<long> cases;        
        void readBatchformat();
    private:
        string readLinee(ifstream batchFile);
        void clear();
};


    //cpp file
void batchformat::readBatchformat() 
{
    ifstream batchFile; 
    //CODE HERE
    line = batchformat::readLinee(batchFile);

}


string batchformat::readLinee(ifstream batchFile)
{
    string thisLine;
    //CODE HERE
    return thisLine;
}

問題是:

string readLinee(ifstream batchFile);

這會嘗試按值傳遞流的副本; 但流不可復制。 您希望通過引用傳遞:

string readLinee(ifstream & batchFile);
//                        ^
string batchformat::readLinee(ifstream batchFile)

正試圖復制ifstream取而代之的是ref

string batchformat::readLinee(ifstream& batchFile)

您的錯誤是您無法按值傳遞ifstream:

string readLinee(ifstream batchFile);

通過ref傳遞它:

string readLinee(ifstream& batchFile);

我建議你在readBatchformat方法中更改一個lign:

void batchformat::readBatchformat() 
{
    ifstream batchFile; 
    //CODE HERE
    line = this->readLinee(batchFile);
    //      ^^^^^^
}

我認為它更具可讀性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM