Instantiating a new instance of a variable - c++

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.

Related

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.

C++ Visual Studio cannot assign value of type to entity of same type

I am writing a class called BleakField, and it uses an enumeration called DrawStyle.
DrawStyle is forward declared in the area marked protected (it needs public access but the class has protected members of type DrawStyle) as such:
class BleakField {
protected:
enum DrawStyle;
DrawStyle type;
It is fully defined right after public:
public:
enum DrawStyle : bool {DRAW_SOLID = true, DRAW_TRANSPARENT = false};
Inside the constructor for the class, I set "type" to DRAW_SOLID.
BleakField::BleakField() {
type = DRAW_SOLID;
These are the only areas that deal with this type in the class so far (and the rest of the class is properly defined I just didn't include it because it isn't necessary but I obviously closed the brackets and everything), and I am getting
IntelliSense error: a value of type "BleakField::DrawStyle" cannot
be assigned to an entity of type "BleakField::DrawStyle"
I am highly confused as to why I cannot assign values of a type to entities of the same type. I figured it could just be a glitch and I tried deleting and retyping it, but it is still doing this. Does anyone know why this is happening and how I can avoid it?
EDIT: It gives me a warning about the change from protected to public, indicating that the protected status gets overwritten by the public one. Also I have done this exact thing before and not gotten that error, which I find odd. And I can definitely forward declare enums, but even moving sections around so that I don't need to didn't solve it. Apparently I can compile the program with half the code unfinished and it worked, but the error is still there. So I guess the compiler thinks it is fine, just not Visual Studio.

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.

Calling QPluginLoader::staticInstances from the constructor of a class instantiated as a static local crashes

I have a singleton class for the purpose of loading Qt plugins instantiated as a static local:
LibraryManager* LibraryManager::instance()
{
static LibraryManager manager;
return &manager;
}
I'm getting "__cxa_guard_acquire(): initializer for function local static variable called enclosing function" in the console whenever QPluginLoader::staticInstances() is hit in the constructor of LibraryManager. What does this mean exactly, and how can I fix this?
According to this source, you have somehow managed to recurse back to the same function local in the same thread. Isn't your library manager a plugin itself, by chance? :)
I don't know what it means, but my gut feeling is that an oldschool singleton could fix it. That is having a pointer to instance as class member initialized to null and do a lazy check in instance() call. I understand that it requires implementing a static release method and finding a proper place to call it. But it would bypass the function local, which is what your error message complains about.
This was just a brain error.
My plugin class was inheriting from the class actually used by library clients (which calls into manager, which instantiates plugins, whose constructors call manager... you can see where that leads), when it should have inherited from a different class that only calls manager from member functions (not its constructor).
tl;dr I typed the wrong class name, but didn't think I did so I dismissed that part of the code as possibly containing the issue.

Private Constructor Error 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.