C++ Initialising std::vector<std::unique_ptr<AbstractClass>> [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 8 years ago.
Improve this question
I've derived std::vector and I'm trying to initialise it. I don't understand why the following is happening:
namespace Bpm {
typedef std::unique_ptr<PolingNormalised> PolingNormalisedPtr;
class PolingNormalisedArray : public std::vector<std::unique_ptr<PolingNormalised>>
{
using std::vector<std::unique_ptr<PolingNormalised>>::vector;
};
}
// Works perfectly
Bpm::PolingNormalisedArray qpmNormArray;
qpmNormArray.push_back(std::move(Bpm::PolingNormalisedPtr(new Bpm::QpmNormalised(2.))));
// Doesn't compile
Bpm::PolingNormalisedArray qpmNormArray(std::move(Bpm::PolingNormalisedPtr(new Bpm::QpmNormalised(2.))));
// Doesn't compile
Bpm::PolingNormalisedArray qpmNormArray(Bpm::PolingNormalisedPtr(new Bpm::QpmNormalised(2.));
// Doesn't compile
Bpm::PolingNormalisedArray qpmNormArray(new Bpm::QpmNormalised(2.));
I don't understand why many of the initialisation methods do not compile. What's going on here? What's the proper way to initialise the std::vector?
Thank you.

If you get rid of all the unique_ptrs and aliases running around in your example, what you're trying to do in the cases that don't compile boils down to this:
#include <vector>
struct foo{};
int main()
{
std::vector<foo> vec(foo{});
}
This obviously won't compile because std::vector has no constructor that takes an instance of the value_type as its argument.
Either use vector::push_back as you've shown in your example, or use vector::emplace_back to construct the unique_ptr in-place within the vector without having to move it.
qpmNormArray.emplace_back(new Bpm::QpmNormalised(2.));

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());
}

My program is stopping [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 6 years ago.
Improve this question
This is my class,and my problem is when I call the constructor
Eveniment e1(1,m1);
with parameters in main method, my program is stopping and I don't know why. M1 is an object of IntrareCAlendar.
class Eveniment{
private:
const int id;
IntrareCalendar data;
char* detalii;
int static nrIntrari;
public:
Eveniment(int nr,IntrareCalendar ic) :id(nr){
this->data = ic;
nrIntrari++;
}
~Eveniment(){
if (this->detalii != NULL)
delete[]this->detalii;
}
};
What should I do? thanks a lot!
You never set detalii to anything valid. It remains uninitialised - it is not initialised to a particular value automatically. You could set it to nullptr in your constructor. (Don't use NULL in C++.)
Your destructor calls delete[] on that member, but no new[] has been called prior to that. As such the behaviour of your program is undefined.
Also, consider using static std::atomic<int> as the type for nrIntarari in case multiple threads instantiate an Eveniment.

C++: accessing private members of the class [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
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()

C++ Reference Variable [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 8 years ago.
Improve this question
How to use 'reference variables' in C++ classes?
I have the following code that I want to put into a class: (note KinectCV&)
KinectCV& kinect = freenect.createDevice(0);
kinect.some_init_functions();
while(condition) {
// getting frames from kinect and processing
kinect.some_processing_functions();
}
kinect.some_stopping_functions();
I'm trying to make a class and separate init, process and stop functions:
class MyKinect {
public:
KinectCV kinect;
void init(){
/* I cannot use the '& kinect = freenect.createDevice(0);' syntax, help me in this */
}
void process(){
kinect.some_processing_functions();
}
void stop(){
kinect.some_stopping_functions();
}
}
I cannot use the '& kinect = freenect.createDevice(0)
That is right, you cannot assign references; once initialized, they refer to the same object forever. What looks like an assignment in your code that works
KinectCV& kinect = freenect.createDevice(0);
is not an assignment, it's initialization. It can be rewritten using the initialization syntax instead of the assignment syntax, like this:
KinectCV& kinect(freenect.createDevice(0));
The reason behind it is that in C++ all initialization must happen in the constructor, not in a "designated initialization function". C++ has no idea that init is your initialization function; all it knows is that once the constructor is over, the object must be in a consistent state, included with all the references that it might hold.
Moving the initialization code into MyKinect's constructor will fix the problem:
class MyKinect {
public:
KinectCV kinect;
MyKinect() : kinect(freenect.createDevice(0)) {
}
}