views:

77

answers:

2

Can I change the code, so that the VS2010 compiler's error message points to the offending line of code?

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;
}

Note that 'NoCopy( const NoCopy& ) = delete;' does not compile in VS2010. I can not use boost.

This was added per Micheal's suggestion:

1>------ Build started: Project: Test, Configuration: Debug Win32 ------
1>  Test.cpp
1>c:\Test\Test.cpp(16): error C2248: 'NoCopy::NoCopy' : cannot access private member declared in class 'NoCopy'
1>          c:\Test\Test.cpp(8) : see declaration of 'NoCopy::NoCopy'
1>          c:\Test\Test.cpp(7) : see declaration of 'NoCopy'
1>          This diagnostic occurred in the compiler generated function 'AnotherClass::AnotherClass(const AnotherClass &)'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
A: 

This is the only error I get when trying to compile that:

Error   1   error C2248: 'NoCopy::NoCopy' : cannot access private member declared in class 'NoCopy' main.cpp    11  1

If you make the constructor public, it compiles just fine (though of course it fails to link due to the missing implementations for those member functions).

I could make a guess as to what you really mean: Why is there an access error for the constructor, but not for the = operator? The answer is that the second line is treated as a constructor and not an assignment.

Reinderien
I believe he would like to be shown that he is breaking the rules on line 16, not 11.
Default
(a) I don't even understand how he's getting errors where he's indicated in the comments, or what errors they are. (b) I don't get the purpose of moving the error around instead of addressing it so that it doesn't happen anymore.
Reinderien
(a) in visual studio you will get 3 messages. First of, the one you get as well, "cannot access private". Second message is "See declaration of NoCopy::NoCopy" (row3). Third message is "see declaration of NoCopy" (row2). And (b), I don't either... but if you do want your class to have a private copy constructor the compiler doesn't show you where you are breaking the rules. And you don't want to use Find/Search.. (Note that if he doesn't subclass, the compiler will inform him that there is an error in main function, but since he is having two instances of `AnotherClass`, it isn't shown.)
Default
I **hope** that makes sense.
Default
For instance, if he had `NoCopy c; NoCopy d = c;` // <- The compiler would give an error on this line
Default
He wants the error. He does not want the class to be capable of being copy constructed. Thus, there should be an error on the specified line. The error he does get is likely along the lines of "in function Y (line #) from class A (line #), called by function Z (line #) from class B (line #) ..."
Dennis Zickefoose
Default
+1  A: 

The error is not shown at the correct line, because Visual Studio doesn't know where it came from, which is the automatically compiled AnotherClass(const AnotherClass&). You have to explicitly define this in order for Visual Studio to continue finding where the error came from.

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;
}

You will now get:

1>\main.cpp(20) : error C2248: 'AnotherClass::AnotherClass' : cannot access private member declared in class 'AnotherClass'

which refers to the "correct" line.

Default