VS2013 compiler: 'CObject::CObject' : cannot access private member declared in class 'CObject' - c++

I'm trying to migrate a project from to c++11 in Visual Studio. I fixed a number of issues, but there's one remaining which I can't seem to crack with MFC:
error C2248: 'CObject::CObject' : cannot access private member declared in class 'CObject' (file.cpp)
: see declaration of 'CObject::CObject'
: see declaration of 'CObject'
This diagnostic occurred in the compiler generated function 'CList<ParameterValue,ParameterValue &>::CList(const CList<ParameterValue,ParameterValue &> &)'
This is code that hasn't changed on our end and has been compiling fine when targeting the Visual Studio 2010 toolset. From what I could gather it doesn't seem like the definition of CObject has changed either which makes it all the stranger.
There's other similar questions reported here, but I couldn't find a solution to my problem there. In most other cases it would appear the issue comes from a lack of public default constructors, Copy constructors, or assignment operators.
Our class that extends CList however provides public versions of all these, and ParameterValue does not inherit from CObject.
class __declspec(dllexport) GParameterValueList : public CList<ParameterValue, ParameterValue&>
{
// ParameterValue is a struct that DOES NOT inherit from CObject (or anything)
public:
GParameterValueList();
GParameterValueList(const GParameterValueList& SrcList);
GParameterValueList& operator=(const GParameterValueList& SrcList);
}
Any help would be appreciated.
P.S.Our implementation is exported into a DLL, I'm wondering if that might be causing some conflicts?
Edit: Not a duplicate of error using CArray or error using CArray -> in these the CObject derived classes were missing public default and copy constructors. As described above, this is not the case with our code.

I think the problem is in your ParameterValue class. Try to declare public constructors in there, including the default constructor.
It seems to me that the CList tries to call the ParameterValue constructor somewhere, but the former can't because the latter is private.
Alternatively, try to use a List of Pointers, like CList<ParameterValue*, ParameterValue*&>.
Another alternative is to use a std container instead of CList.

Related

Strange happenings in the barren wastes of visual studio

So I was just happily going along defining a new class when this happend:
class Thing : public OtherThing
{
public:
Thing : public OtherThing();//here
};
I really have no idea why visual assist/studio did this and i've never seen it before so really my question is what does it mean?
Just for the sake of clarity I would normally define the same class like this:
class Thing : public OtherThing
{
public:
Thing();
};
The public keyword is defined in 2 contexts:
Deferring access to members of a base class to the access level they were declared with:
When a class uses public member access specifier to derive from a base, all public members of the base class are accessible as public members of the derived class and all protected members of the base class are accessible as protected members of the derived class (private members of the base are never accessible unless friended)[source]
Specifying the access level for all members declared following this specifier
Public members form a part of the public interface of the class (other parts of the public interface are the non-member functions found by Argument-Dependent Lookup).
A public member of a class is accessible everywhere.[source]
Since the code you have demonstrated is not declaring inheritance (1) or specifying member access (2) it is an invalid use of the keyword and should not compile.
I see your statement that this compiles for you, but indeed this cannot compile in gcc: http://ideone.com/Z33viJ or in Visual Studio 2015, which you can validate by going here: http://webcompiler.cloudapp.net/ The only plausible explanation that I can come up with is there is a malfunction in which the code your editor is showing you is not what has been written to the file and thus is not what's being compiled. If this is the case perhaps a restart of Visual Studio would solve the problem.

Error c2248 when exporting a class that has a private member of type std::stack<std::unique_ptr<T>>

Why do I get this error:
Error 2 error C2248: 'std::unique_ptr<_Ty>::unique_ptr' : cannot access private member declared in class 'std::unique_ptr<_Ty>' c:\program files (x86)\microsoft visual studio 11.0\vc\include\xmemory0 606 1 UNIQUE_PTR
when trying to Export this class with __declspec(dllexport)?
class __declspec(dllexport) A
{
private:
std::stack<std::unique_ptr<int>> ints;
};
Im using vs2012
I don't know Visual Studio so I can't be sure of the meaning of the error message, but I can see that your class is unusable: the only member variable is private, and you wrote no methods to access it.
As far as I understand std::stack assumes that the elements can be copied, which is not true for the std::unique_ptr, which has to be moved.
So the problem is that std::stack cannot access the private copy constructor of the std::unique_ptr, not that std::stack is private itself.
According to https://support.microsoft.com/en-us/kb/168958 you might have some difficulties exporting stack and unique_ptr. (Since stack uses deque)
Some STL classes contain nested classes. These classes can not be
exported. For instance, deque contains a nested class deque::iterator.
If you export deque, you will get a warning that you must export
deque::iterator. If you export deque::iterator, you get a warning that
you must export deque. This is caused by a designed limitation that
once a template class is instantiated, it can not be re-instantiated
and exported. The only STL container that can currently be exported
is vector. The other containers (that is, map, set, queue, list,
deque) all contain nested classes and cannot be exported.

Instantiating a new instance of a variable

I have two classes. In the main class I have a process that is step by step and can be stepped though, I have implemented this as a state machine. To help keep the machine easy/friendly I created another class that holds information about the current state of the machine. In the main class I then added a reference to it as such
CAnimateMachine *m_AniMach;
After some actions happen I call a function in the main class to instantiate variables within my state machine object. Each call to this function AniInit() should basically "reset" the state machine by instantiating the variables to the "initial state".
My issue is I am not sure how to instantiate my m_AniMach properly. I am used to C# where I can just do
m_AniMach = new CAnimateMachine();
to "erase" the old object and instantiate a new one. Though, from what I have read, I can not be so cavalier in C++. What is the proper way to "re-instantiate" this variable in my init method?
Could I use the new operator m_AniMach = new CAnimateMachine() and then in the main class' deconstrutor do delete &m_AniMach?
EDIT: juanchopanza's answer makes sense to me. Though I keep getting an error when trying to compile. I am not exactly sure what the error is trying to tell me, I think it is telling me that my class isn't public when it is? I reviewed the C2248 MSDN article but I do not see how it relates to my situation.
error C2248: 'CObject::operator =' : cannot access private member declared in class 'CObject'
include\afx.h(562) : see declaration of 'CObject::operator ='
include\afx.h(532) : see declaration of 'CObject'
occurred in the compiler generated function 'CAnimateMachine &CAnimateMachine::operator =(const CAnimateMachine &)'
Here is my CAnimateMachine class
class CAnimateMachine
{
public:
CAnimateMachine();
int startX,startY;
};
And this is how I instantiate it
m_AniMach = CAnimateMachine();
And how it is defined
CAnimateMachine m_AniMach;
There seems to be no reason to use a pointer. You can use an object instead:
CAnimateMachine m_AniMach;
in which case it will get default initialized when an object of the type that holds it gets instantiated. To "re-initialize" it, you can say
m_AniMach = CAnimateMachine();
If you do this, you will not have to worry about things like following the rule of three or other dynamic allocation pitfalls.

Find root cause of "cannot access private member declared in class 'QObject'"

I understand why I get a C2248: 'QObject::QObject' : cannot access private member declared in class 'QObject' . Qt objects are not copyable, as explained here:
https://stackoverflow.com/a/3513395/356726
No copy constructor or assignment operator
The problem is, that the compiler message always indicates the last line (closing }) of the class:
class MyQObject : public QObject {
Q_OBJECT
....
}; <-- error line
Root cause is somewhere else, ie. where the class is copied (other file, some different line in code). This is sometimes hard to spot!
Question: Is there a way to locate the line of the real reason for the error
Remark: Please note, before you mark this Duplicate. Question is about finding the root cause, not how to solve it as in the other questions.
--- Edit 1 ---
Good hint Kuba et.al. It's VS2010, compiling in Qt Creator 2.8.0
'QObject::QObject'
C:\Qt\5.1.0-32\qtbase\include\QtCore/qobject.h(115) : see declaration of 'QObject'
This diagnostic occurred in the compiler generated function 'MyQObject ::MyQObject (const MyQObject &)'
I wonder why a copy constructor is generated. One idea crossed my mind, I am using the DBus enabled version of Qt, might this be the reason?
The easiest solution to detect the root cause is by making your copy ctor also private. (Or deleted, but that's not possible in VS2010 yet). This will suppress the automatically-generated copy ctor, which was the source of the error.
If you are not explicitly copying your MyObject but you keep getting this error message then something you're using in conjunction with your MyObject is doing the copying on your behalf.
The most likely culprit would be one of the container classes, e.g. QList, QVector, etc.
Read the Container class documentation for more information as well as the specific class' documentation of any container you might be using. All containers have requirements of their elements, e.g. Must have default constructor, must be assignable, etc. This is where I think your problem lies.

Way To Prevent To Redefine Constructor Definitions

I am writing a custom Exception class, with some additional features over the std::exception one. I added a custom constructor Exception(std::string details) : details(details) {} to my Exception class.
Later, when I extend the Exception class, with the class FileNotAccessibleException, and throw it, I get a compile error saying no matching function for call to ‘FileNotAccessibleException::FileNotAccessibleException(std::basic_string<char>)’ When I then add a method FileNotAccessibleException(std::string details) : Exception(details) {} to my class, it does work fine.
Is there no way to make the constructor work, without having to redefine the constructor for all my classes?
You can inherit the constructors of the base class with a using declaration:
class FileNotAccessibleException : public Exception
{
public:
using Exception::Exception;
...
};
See more on inherited constructors on Bjarne Stroustrup's C++11 FAQ.
Note: this does not apply to pre-C++11 implementations, and was one of the last C++11 language features to be added to popular compilers such as G++ and CLANG.