C++: accessing private members of the class [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Still getting my head around C++ as I'm new to it, but I'm trying to extend some existing code I've got that is expecting me to make use of the std::vector.
The following is declared in the header (shortened for simplicity):
class WindowManager
{
private:
std::vector<Item*> m_itemlist;
}
My problem is how I'm meant to access this from the .cpp? I'd like to use it to have an array of Item type but I don't understand how to actually get to the point where I can add a newly instantiated Item, let's say button, to the array?
A bit of a rudimentary question but I've not had much luck with tutorials that cover std::vector.

If possible avoid using vector of pointers to Item. Use vector of Item directly.
class WindowManager
{
void addItem(Item const& item) { m_itemlist.push_back(item); }
private:
std::vector<Item> m_itemlist;
};
int main()
{
WindowManager wm;
Item i;
wm.addItem(i);
}

To add an item you could use a member function like this:
class WindowManager
{
private:
std::vector<Item *> m_itemlist;
public:
void addItem(Item *newItem);
}
in window_manager.cpp:
void WindowManager::addItem(Item *newItem)
{
m_itemlist.push_back(newItem);
}
see std::vector::push_back()

Related

return std::map with a getter method [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
As the title indicates I'm trying to grab on to a std::map container and return it.
I get the following error: invalid use of template-name 'std::map' without an argument list
Now I pretty sure the reason has to do with templates, but simply haven't found an example that describes my specific situation.
My program i rather simple since I'm a newbie on parantal leave. It consists of these files:
main.cpp
Movie_Archive.hpp
Movie_Archive.cpp
Movie.hpp
Movie.cpp
Helper.hpp
Helper.cpp
I don't think anyone wants me to paste all the code so I've pasted the parts that I belive to be vital to my question. Code below:
Movie_Archive.hpp
class MovieArchive {
private:
std::map <std::string, Movie> movie_archive;
public:
std::map getMovieArchive();
};
Movie_Archive.cpp
std::map MovieArchive::getMovieArchive() {
return movie_archive;
}
main.cpp
TheMovieArchive.controlArchiveStatus(TheMovieArchive.getMovieArchive(), TheMovieArchive.getTitle());
// Checks if the movie title already has been entered
Thank soo much for taking a look. I hope someone can find a solution.
Kind regards//Alle
You probably mean
class MovieArchive {
private:
std::map <std::string, Movie> movie_archive;
public:
std::map<std::string, Movie> getMovieArchive();
};
i.e., you have to provide the template parameters in the return type of the getter as well.
BTW: you probably want to write
class MovieArchive {
private:
std::map <std::string, Movie> movie_archive;
public:
const std::map<std::string, Movie> & getMovieArchive();
};
i.e. returning a const reference instead of a copy of the internal data structure.

confused about class structure in code [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am reading a sample code that uses C++ and classes, I am new on C++ classes I can work with basics similar to this http://www.cplusplus.com/doc/tutorial/classes/, but I cant understand what the code below does mean or the color it is using visual studio c++
thanks
I am sorry if it is a fool question
It creates an object named some by instantiating the class some.
Then it calls the member function ToVector() on the object some and pass the result of the call to the function named function.
class is blue because it is a keyword of the C++ language.
The first some is green because it is the name of a class.
The second some is black because it is a variable.
And function and ToVector are red because the are functions.
Now this is ugly code because you "hide" the class some by reusing the same name for your variable. Also you do not need to put the word class here.
Here is a more complete and nicer version:
#include <vector>
class Some
{
public:
std::vector<int> ToVector()
{
return std::vector<int>(); //return an empty vector
}
};
int f(std::vector<int> v)
{
return 0;
}
int main(int, char**)
{
Some some; // Was "class some some"
return f(some.ToVector());
}

memory access violation using std::map as local member [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have a memory access violation error on the line m_Lights=tmp; during the call of the method v_init() in Algo::Init(). Shouldn't map m_Lights be created at the instantiation of m_LightsManager? Why do I have this error?
class LightManager
{
private:
std::map<sint32,Light> m_Lights;
public:
LightManager (void);
~LightManager (void);
void v_init();
};
LightManager ::LightManager (void)
{
}
LightManager ::~LightManager (void)
{
}
void LightManager ::v_init()
{
Light tL;
std::memset(&tL,0,sizeof(Light));
std::map<sint32,Light> tmp;
tmp.insert(std::pair<sint32,Light> (-1,tL));
m_Lights=tmp;
}
class Algo
{
private:
LightsManager m_LightsManager;
....
public:
Algo();
void Init();
};
Algo::Algo()
{
Init();
}
void Algo::Init()
{
m_LightsManager.v_init();
}
If Light is not trivially-copyable then
std::memset(&tL,0,sizeof(Light));
is undefined behavior. This is likely the cause of your error.
In addition to Vittorios answer: Do all the initialization in Lights constructor, instead of relying on an external memset call. And don't use std::memset in C++ to initialize all member variables of an object in one statement, do it explicitely for every variable (usually using a simple assignment; in C++11 you can do that even in the declaration/header, where it IMHO belongs).
Reason: Light may be derived, and the base class(es) define their own data, which you overwrite by recklessly nulling the whole object.
I solved the error, the problem came from a wrong declared pointer in other part of the soft that overlaped with the memory space where the object m_LightsManager where initiated ,

C++ Calling a function with a vector as a parameter in main [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have a myVector class:
class myVector {
public:
void populateVector();
void showMenu(vector <myVector> const &vec_first);
private:
vector <myVector> &vec_first;
}
I haven't filled my vector yet but I want to essentially use the vector in the showMenu() function, however, a problem arises in my main when I attempt to call the showMenu() function.
int main() {
myVector obj;
obj.showMenu(vector <myVector> const &vec_first);
}
Codeblocks keeps saying:
main.cpp|33|error: expected primary-expression before 'const'
Your are confusing the function declaration with calling it. You need
int main() {
myVector obj;
vector<myVector> vec;
obj.showMenu(vec);
}
or something like that
I haven't filled my vector yet but I want to essentially use the vector in the showMenu() function, however, a problem arises in my main when I attempt to call the showMenu() function.
Don't pass the vector (or anything) in through showMenu; it already has access to the vector, which is a member of the same class.
If you did want to pass a function argument, repeating the argument's original declaration would not be the way to do it. Only its name should be specified. Here that would be:
obj.showMenu(obj.vec_first);
… if vec_first weren't private.
It looks like you need to go back to basics and read the initial chapters of your C++ book.

Returning an empty smart pointer [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a class that looks like this,
class A
{
std::shared_ptr<Type> ret;
public:
A()
{
ret=std::shared_ptr<Type>(new Type);
}
std::shared_ptr<Type> GetTypeA(){return ret;}
A (const A&a)
{
....
ret=a.ret;
}
};
class Type
{
A aa;
public:
Type(A*a):aa(*a){}
};
Somewhere in the client code, I call the method GetTypeA like this
void func(A*pA)
{
...
std::shared_ptr<Type> spT=pA->GetTypeA();
...
}
Debugging shows me that spT=empty after the call. But inside pA, ret value is NOT empty.
I notice some mistakes in your code :
A()
{
ret=std::shared_ptr<Type>(new Type);
}
"new Type" means you call default constructor for Type (Type::Type()), and you didn't write it in your sample. Try "new Type(*this)" to use your own constructor.
But to do this you need to change your Type class to:
class Type
{
A* aa; // Use a pointer
public:
Type::Type(A&a) :aa(&a){} // Use references
};
The problem is it's not resolving the "recursive aspect", depend your needs, I would use a static reference to A in the Type class...