0

Clist variableMy クラスでは、次のようにstatic を宣言しました。

#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()は私のクラスの一部ではありません。

どこが間違っていますか?問題のあるコード スニペットを貼り付けました。私はそれを開始しましたが、otherfunc()を呼び出している行を除いて、すべてが機能します。static キーワードに依存しません。静的を削除しても、同じエラーが発生します。

編集済み:ここに私が得るエラーがあります:

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>        ]
4

3 に答える 3

0

の定義を見てみましょう -

void otherfunc(CList<int,int> a)

入力パラメーターCList<int,int> a は値で渡されます。つまり、この関数を呼び出すと、CList<int,int>コピー コンストラクターを使用して入力パラメーターがコピーされます。
ただしCList<int,int>、コピー コンストラクターは実装せず、その基本クラスCObjectはそのコピー コンストラクターをプライベートとして定義します。

定義を次のように変更する必要があります -

void otherfunc(CList<int,int>& a)
于 2016-07-25T13:56:32.907 に答える
0

あなたのコードはそのままではコンパイルされません(すべきClassclassPublicべきpublicなど)。エラーメッセージは何ですか? また、エラーを再現する簡単なコンパイル可能な例を投稿する必要があります。私の推測では、クラス宣言の外で静的変数をインスタンス化していないということです。

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

于 2014-11-28T04:19:17.753 に答える