简体   繁体   English

无法访问在类“ CObject”中声明的私有成员

[英]Cannot access private member declared in class 'CObject'

I am trying to create a copy constructor for a CStringArray. 我正在尝试为CStringArray创建一个副本构造函数。 once the code is compiled, visual studio gave me this error : Cannot access private member declared in class 'CObject' 一旦代码被编译,Visual Studio就给我这个错误:无法访问在类'CObject'中声明的私有成员

in example.h I have declared the variable: 在example.h中,我声明了变量:

list<CStringArray>EqptListPpiedsOptions;

in example.cpp 在example.cpp中

I use this as my copy constructor : 我将其用作复制构造函数:

example::example(const example &data) {
  list<CStringArray>::const_iterator itr = data.EqptListPpiedsOptions.begin();

  while (itr != data.EqptListPpiedsOptions.end()) {
    this->EqptListPpiedsOptions.push_back(*itr);
    itr++;
  }
}

How can I correctly use the copy constructor CStringArray List ? 如何正确使用复制构造函数CStringArray List?

Any help will be appreciated. 任何帮助将不胜感激。

There's not much to say without knowing what CStringArray or EqptListPpiedsOptions are. 在不知道什么是CStringArrayEqptListPpiedsOptions情况下没有太多要说的。 But just as an aside: 但顺便说一句:

If you're in C++11land or later, you can use auto instead of tediously spelling out the type, so 如果您使用的是C ++ 11land或更高版本,则可以使用auto而不是单调地拼写类型,因此

list<CStringArray>::const_iterator itr = data.EqptListPpiedsOptions.cbegin();

becomes 变成

auto itr = data.EqptListPpiedsOptions.cbegin();

(Note also in C++11 you use cbegin() and cend() for const_iterator s. (另请注意,C ++ 11使用cbegin()cend()const_iterator秒。

And you can simplify further using ranged- for : 您还可以进一步简化使用ranged- for

for (const auto &itr : data.EqptListPpiedsOptions) {
  this->EqptListPpiedsOptions.push_back(itr);
}

Here's a working example showing a copy constructor can access private data members of another instance of the class: 这是一个工作示例,该示例显示副本构造函数可以访问该类的另一个实例的私有数据成员:

#include <initializer_list>
#include <iostream>
#include <list>

class Example {
public:
  Example() = default;
  Example(std::initializer_list<int> li) : li_(li) {}
  Example(const Example &data);
  void print() {
    for (const auto i : li_) {
      std::cout << i << ", ";
    }
  }

private:
  std::list<int> li_;
};

Example::Example(const Example &data) {
  for (const auto &i : data.li_) {
    this->li_.push_back(i);
  }
}

int main() {
  Example foo{5, 4, 3, 2, 1};
  Example bar;
  bar = foo; // call copy constructor and move foo.li_ to bar.li_
  bar.print();
}

Although in this example, the default copy constructor works just as well, so there's no need to specify a new one. 尽管在此示例中,默认的复制构造函数也可以正常工作,所以无需指定新的构造函数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 &#39;CObject :: CObject&#39;:无法访问在类&#39;CObject&#39;中声明的私有成员 - 'CObject::CObject' : cannot access private member declared in class 'CObject' 无法访问在类“ CObject”中声明的私有成员? - Cannot access private member declared in class 'CObject'? VS2013编译器:“ CObject :: CObject”:无法访问在类“ CObject”中声明的私有成员 - VS2013 compiler: 'CObject::CObject' : cannot access private member declared in class 'CObject' 错误C2248:'CObject :: CObject':无法访问类'CObject'afxwin.h中声明的私有成员 - error C2248: 'CObject::CObject' : cannot access private member declared in class 'CObject' afxwin.h 使用CArray时出错:无法访问在类CObject中声明的私有成员 - Error using CArray: cannot access private member declared in class CObject CPtrList 无法使用:错误 C2248:“CObject::operator =”:无法访问类“CObject”中声明的私有成员 - CPtrList cannot use: error C2248: 'CObject::operator =' : cannot access private member declared in class 'CObject' 试图传递CStringArray给出错误无法访问类'CObject'中声明的私有成员 - Trying to pass a CStringArray gives error cannot access private member declared in class 'CObject' 向CArray添加数据会产生错误“无法访问在类&#39;CObject&#39;中声明的私有成员” - Adding data to CArray gives error “cannot access private member declared in class 'CObject'” &#39;CObject :: CObject&#39;:无法访问在&#39;CObject&#39;d:\\ Program Files \\ Microsoft Visual Studio 9.0 \\ vc \\ atlmfc \\ include \\ afxcoll.h中声明的私有成员 - 'CObject::CObject' : cannot access private member declared in class 'CObject'd:\program files\microsoft visual studio 9.0\vc\atlmfc\include\afxcoll.h 错误 C2248: 'CObject::CObject': 当我在 ZD7421054471AB272ZCEAC18FD97BBD237 - error C2248: 'CObject::CObject' : cannot access private member declared in class 'CObject' when I calling hDC.SelectObject function in MFC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM