簡體   English   中英

傳遞CList變量會給出錯誤C2248:'CObject :: CObject':無法訪問私有成員

[英]Passing CList variable gives error C2248: 'CObject::CObject' : cannot access private member

在我的課程中,我有一個靜態Clist variable ,它通過以下方式聲明:

#include<stdio.h>
#include<conio.h>
#include <afxtempl.h>
void otherfunc(CList<int,int> a)
{

}
class A
{
public:
CList<int,int> myvariable;
void myfunc()
{
otherfunc(myvariable);
}

};


int _tmain(int argc, _TCHAR* argv[])
{
    A a;
    a.myfunc();
    getch();
    return 0;
}

otherfunc()不屬於我的課程。

我要去哪里錯了? 我剛剛在代碼片段中粘貼了該問題。 我已經啟動了它,並且一切正常,除了im調用otherfunc()所在的行之外,其他所有文件都有效。 它不依賴於靜態關鍵字。 即使我刪除靜態,我也會遇到相同的錯誤。

編輯:這是我得到的錯誤:

C:\program files (x86)\microsoft visual studio 9.0\vc\atlmfc\include\afxtempl.h(776) : error C2248: 'CObject::CObject' : cannot access private member declared in class 'CObject'
1>        c:\program files (x86)\microsoft visual studio 9.0\vc\atlmfc\include\afx.h(561) : see declaration of 'CObject::CObject'
1>        c:\program files (x86)\microsoft visual studio 9.0\vc\atlmfc\include\afx.h(532) : see declaration of 'CObject'
1>        This diagnostic occurred in the compiler generated function 'CList<TYPE,ARG_TYPE>::CList(const CList<TYPE,ARG_TYPE> &)'
1>        with
1>        [
1>            TYPE=int,
1>            ARG_TYPE=int
1>        ]

您的代碼無法編譯( Class應該是classPublic應該是public等)。 錯誤消息是什么? 另外,您必須發布一個簡單的可編譯示例來重現您的錯誤。 我的猜測是您沒有在類聲明之外實例化靜態變量,請參見

http://www.learncpp.com/cpp-tutorial/811-static-member-variables/

由於“ Public:”,您可能不會收到錯誤。 因為“ Public:”不是關鍵字,所以它是一個標簽。 這就是默認情況下“ myvariable”是私有的原因。 代替“ Public:”使用“ public:”,並用靜態替換“ Static”。

看一下-

void otherfunc(CList<int,int> a)

輸入參數CList<int,int> a通過值傳遞,這意味着調用此函數時,它將使用CList<int,int>復制構造函數復制輸入參數。
但是CList<int,int>沒有實現Copy構造函數,並且它的基類CObject將其Copy構造函數定義為私有。

您應該將定義更改為-

void otherfunc(CList<int,int>& a)

暫無
暫無

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

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