Private Constructor Error C++ - c++

I'm trying to write a class using the Singleton pattern in C++ for the first time and I get an error simply by making my constructor private, I'm using xcode 3.2 and compiling with gcc 4.2:
class GameDirector {
private:
//Singleton instance
static GameDirector* director;
//Constructor
GameDirector(); //THIS LINE GIVES ME THE ERROR
public:
//Singleton pattern
static GameDirector* sharedDirector();
};
It gives me 2 errors:
error: 'GameDirector::GameDirector()' is private
error: within this context
I can't understand why its giving me an error, I thought you were allowed to make constructors private in C++...

The line that says "within this context" should direct you to the point in the code where someone is trying to allocate a GameDirector object instead of using the sharedDirector method.

The part of the error "within this context" should have a line number in it that points you to a piece of code NOT in your class definition, but somewhere you're accidentally trying to create a GameDirector directly rather than through the sharedDirector method.

You are.
You must be trying to instantiate an object of type GameDirector somewhere. We could only guess at where.

You are trying to create instance of GameDirector somewhere else. The snippet you posted compiles with no error.

Related

Returning a const pointer throws a compile error in Unreal Engine?

I am not terribly great at C++ so I am sorry in advance if this is just a silly mistake. I was trying to expose the XMLParser to blueprints in Unreal Engine
Using VS, the code doesn't show any compile errors inline. (i.e. no red lines anywhere) However, when I try to compile, I get the following error:
Unrecognized type 'FXmlNode' - type must be a UCLASS, USTRUCT or UENUM
The line of code in question:
UFUNCTION(BlueprintCallable)
const FXmlNode* GetRoot();
I updated the Build.cs with the XMLParser module and can access the functions inside of the various XML classes. However, I find it odd that the compiler doesn't recognize it. Does anyone know what might be causing this?
Any help would be greatly appreciated.
The solution to this was rather a simple once I actually thought about it a little bit. The XmlNode class was not a UCLASS type and I can't return a non-U* type inside of Blueprints which was an issue. (Hence why the error makes sense now)
Instead I created another wrapper class for the XmlNode class itself and made it a UCLASS type. From there it was rather easy to wrap the XmlNode class since it mainly was returning Strings and other node references.

Object assignment - invalid use of member function error

I have a class which includes among its members an object of another class.
The header file looks like this:
class LinxArduinoEthernetListener : public LinxListener
{
EthernetServer ArduinoTcpServer(uint16_t);
Where EthernetServer is itself a class (defined in the Arduino EthernetServer.h library).
Because I do not know the port at compile time, but the server object should be a member of the listener class, I allow the server object to be initialized in the class constructor, and then attempt to reassign that object later, using the following code (in the corresponding .cpp file):
ArduinoTcpServer = EthernetServer(port);
Where "port" is a uint16_t. As far as I know, this is the correct way to reassign to an object variable a newly constructed instance.
And yet, the compiler gives me the following error:
LinxArduinoEthernetListener.cpp:122: error: invalid use of member
function (did you forget the '()' ?)
ArduinoTcpServer = EthernetServer((uint16_t)port);
I think this may be related to the error I get for the immediately subsequent function call:
LinxArduinoEthernetListener.cpp:123: error:
'((LinxArduinoEthernetListener*)this)->LinxArduinoEthernetListener::ArduinoTcpServer'
does not have class type
ArduinoTcpServer.begin();
But I would say it clearly does have a class type, namely the EthernetServer class, as specified in the header file.
What am I doing wrong here?
EthernetServer ArduinoTcpServer(uint16_t); declares a member function named ArduinoTcpServer. To declare a member variable, omit the parameter type and parentheses. Also add a constructor to initialize the member variable, e.g. :
class LinxArduinoEthernetListener : public LinxListener
{
public:
EthernetServer ArduinoTcpServer;
LinxArduinoEthernetListener(uint16_t port)
: ArduinoTcpServer(port)
{
}
};
So, the issue was the declaration of the server object.
Initially, I had it declared as follows:
EthernetServer ArduinoTcpServer(22);
But I would get an error about "expected identifier before numeric constant" referring to the 22. So I googled it, and found someone suggesting that (in some context I don't remember) specifying only the type was sufficient to call the constructor matching that prototype. Doing so allowed the compiler to continue, so I assumed that was valid. However, it seems not.
The real issue is that the compiler Arduino IDE uses requires braces initialization, e.g.
EthernetServer ArduinoTcpServer{22};
That seems to work.

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.

Using CMapStringToString

I have need of a simple hash table in my project and from the documentation it seems like CMapStringToString is exactly what I want. However, I've found that simply adding a new field to my class is sufficient to throw an error message, even if I'm not otherwise accessing it.
... other class members
CMapStringToString myMap;
Just doing this gives me this error:
...
error C2248: 'CObject::CObject' : cannot access private member declared in
class 'CObject'
Nowhere is anything referencing myMap as yet.
The file & line number for the error is "afxcoll.h" at line 1503, which is the closing brace of the CMapStringToString class definition.
Does anybody know what the problem is here? Is there sample code for this class anywhere?
Someone had earlier posted an answer suggesting the problem was that the copy constructor was private and suggested creating my own copy constructor for the class with the map. This does not work, and whomever posted it actually deleted their own answer (after I'd read it but before I'd responded).
I ended up creating a class derived from CMapStringToString and wrote a copy constructor for it. This wasn't quite the same thing as the earlier suggestion and it worked. Thanks to all for your suggestions. They may not have been exactly right but they at least steered me in the right direction.
Maybe your class doesn't have a copy constructor and you're trying to copy an instance somewhere? Most of the time this wouldn't yield an error, but since the underlying type of the CMapStringToString class is Object it might be needed.