0

在我的班级中,我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()不属于我的课程。

我哪里错了?我刚刚粘贴了有问题的代码片段。我已经启动了它,除了我调用 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>        ]
4

3 回答 3

0

您的代码无法编译(Class应该是classPublic应该是public等)。错误信息是什么?此外,您必须发布一个简单的可编译示例来重现您的错误。我的猜测是您没有在其类声明之外实例化您的静态变量,请参阅

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

于 2014-11-28T04:19:17.753 回答
0

由于“公共:”,您可能不会收到错误消息。因为“Public:”不是关键词,它是一个标签。这就是为什么“myvariable”默认是私有的。代替“公共:”使用“公共:”并将“静态”替换为静态。

于 2014-11-28T04:26:34.060 回答
0

看一下定义——

void otherfunc(CList<int,int> a)

输入参数CList<int,int> a 是按值传递的,这意味着当您调用此函数时,它将使用CList<int,int>复制构造函数复制输入参数。
CList<int,int>没有实现 Copy Constructor,其基类CObject将其 Copy Constructor 定义为私有。

您应该将定义更改为 -

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