Using CMapStringToString - c++

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.

Related

No matching function for call to...?

I'm getting an error for what I assume is declaring the incorrect type, but I'm not sure where the issue is or what I need to fix. I'm using two files.
Please tell me if I haven't given enough info, I'm still a newbie. I'd really appreciate the help.
I appreciate you can't change the input for the test, but whoever wrote the test needs to be told it's a bad test.
Data should be initialized through a constructor.
Data members (address and price) should be declared as private not public.
Whoever wrote the test is requiring you to write a bad C++ class.
That said, you can conform with this bad test by adding to the .h file:
HousePrice();
and to the .cpp file
HousePrice::HousePrice():address(""),price(0){}
Thereby giving it the expected default constructor.
Change it to
#include "zillow.h"
HousePrice hp ("1600 Pen. Ave",1561.53);
ASSERT_EQ(hp.price, 1561.53);
Previously you were trying to create your HousePrice object with no constructor parameters, but the only constructor you've written takes address and price.
The error is here:
HousePrice hp;
When you create an object of a class like that, the default constructor of the class is invoked implicitly. But your class definition does not have a default constructor.
Solution:
Since you are not allowed to change the code at the caller's part, you should modify your class definition such that it contains a default constructor. Then it is upto you to decide the initial values or simply leave it empty.
For example:
Add HousePrice() inside the class definition in zillow.h
Add the following into zillow.cpp
HousePrice::HousePrice()
{
address = "";
price = 0.0;
}
The answer is in the error message. You are trying to default-construct an object of HousePrice type, but HousePrice does not have a default constructor.
Add a default constructor (a constructor with no parameters)
Side note: there is no point in having getter or setter functions in this example. Your data members are public and you don't have any constraints on them

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.

Boost noncopyable error when creating STXXL maps

I have been doing some work with STXXL, and I've kind of encountered a problem with the maps inheriting from boost::noncopyable... For this project, I create several maps with the statement:
stxxl::map<int, mapData, CmpIntGreater, 4096, 4096> node_map((stxxl::unsigned_type)(4096 * 4), (stxxl::unsigned_type)(4096 * 3));
Needless to say the hard coded values in the constructor will be replaced once I solve this problem, but in any case, I get the error:
C2248: 'boost::noncopyable_::noncopyable::noncopyable' : cannot access private member declared in class 'boost::noncopyable_::noncopyable'
Has anyone else encountered this problem with STXXL maps? Or does anyone have some general advice or best practices when working with noncopyable objects?
Thanks for all your help guys :)
Just to add an official answer here to accept, my problem was that I had some functions returning maps by value and some function arguments being passed by value. Once this was fixed, it worked like a charm! So, moral of the story, when using a version of STXXL that inherits from noncopyable, make sure that all instances of your STXXL object are used with your functions by reference only, not by value.

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.

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.