C++ 对于无复制类,我是否可以更改代码,以便VS2010编译器在出现问题的行上标记错误?

C++ 对于无复制类,我是否可以更改代码,以便VS2010编译器在出现问题的行上标记错误?,c++,visual-studio-2010,C++,Visual Studio 2010,我是否可以更改代码,使VS2010编译器的错误消息指向有问题的代码行 class NoCopy { //<-- error shows up here NoCopy( const NoCopy& ); //<-- and error shows up here NoCopy& operator=( const NoCopy& ); public: NoCopy(){}; }; struct AnotherClass :NoCopy { }

我是否可以更改代码,使VS2010编译器的错误消息指向有问题的代码行

class NoCopy
{ //<-- error shows up here
   NoCopy( const NoCopy& ); //<-- and error shows up here
   NoCopy& operator=( const NoCopy& );
 public:
   NoCopy(){};
};

struct AnotherClass :NoCopy
{
}; //<-- and error shows up here

int _tmain(int argc, _TCHAR* argv[])
{
  AnotherClass c;
  AnotherClass d = c; //<-- but the error does not show up here
  return 0;
}

这是我在尝试编译时遇到的唯一错误:

Error   1   error C2248: 'NoCopy::NoCopy' : cannot access private member declared in class 'NoCopy' main.cpp    11  1
如果将构造函数公开,那么它可以正常编译(当然,由于缺少这些成员函数的实现,它无法链接)


我可以猜测一下你的真正意思:为什么构造函数有访问错误,而
=
操作符没有?答案是第二行被视为构造函数而不是赋值。

错误没有显示在正确的行,因为Visual Studio不知道它来自何处,这是自动编译的另一个类(const-AnotherClass&)。为了让Visual Studio继续查找错误的来源,您必须显式定义它

class NoCopy {
   NoCopy( const NoCopy& );
   NoCopy& operator=( const NoCopy& );
 public:
   NoCopy(){};
};

struct AnotherClass :NoCopy
{
    AnotherClass();  // Since there is another constructor that _could_ fit,
                     // this also has to be defined
private:
    AnotherClass(const AnotherClass&);  // Define this one
};

int _tmain(int argc, _TCHAR* argv[])
{
  AnotherClass c;
  AnotherClass d = c; //<-- error is now shown here
  return 0;
}
class NoCopy{
NoCopy(const NoCopy&);
NoCopy&运算符=(const NoCopy&);
公众:
NoCopy(){};
};
结构另一个类:NoCopy
{
AnotherClass();//因为有另一个_可以u适合的构造函数,
//这也必须加以界定
私人:
AnotherClass(const AnotherClass&);//定义这个
};
int _tmain(int argc,_TCHAR*argv[]
{
另一个c类;
AnotherClass d=c;//\main.cpp(20):错误C2248:“AnotherClass::AnotherClass”:无法访问在类“AnotherClass”中声明的私有成员


这是指“正确”的一行。

我相信他希望在第16行,而不是第11行被证明他违反了规则。(a)我甚至不明白他在评论中指出的地方是如何出错的,或者错误是什么。(b)我不明白移动错误的目的,而不是解决它,这样它就不会再发生了。(a)在visual studio中,您将收到3条消息。首先,您也会收到一条消息,“无法访问私有”。第二条消息是“请参阅NoCopy声明::NoCopy”(第3行)。第三条消息是“请参阅NoCopy声明”(第2行)。和(b),我也不知道…但是如果你真的想让你的类有一个私有的副本构造函数,编译器不会告诉你哪里违反了规则。你也不想使用Find/Search。。(请注意,如果他没有子类,编译器将通知他主函数中有错误,但由于他有另一个类的两个实例,因此不会显示。)例如,如果他有
NoCopy c;NoCopy d=c;
//他想要这个错误。他不希望这个类能够被复制构造。因此,在指定的行上应该有一个错误。他确实得到的错误很可能是在类A(行#)的函数Y(行#)中,由类B的函数Z(行#)调用(第#)行)…“我认为您应该发布您收到的实际编译器错误,并解释它表明,给出错误的不是另一个类d=c;
行,而是另一个类
};
class NoCopy {
   NoCopy( const NoCopy& );
   NoCopy& operator=( const NoCopy& );
 public:
   NoCopy(){};
};

struct AnotherClass :NoCopy
{
    AnotherClass();  // Since there is another constructor that _could_ fit,
                     // this also has to be defined
private:
    AnotherClass(const AnotherClass&);  // Define this one
};

int _tmain(int argc, _TCHAR* argv[])
{
  AnotherClass c;
  AnotherClass d = c; //<-- error is now shown here
  return 0;
}