vector pushing back problem



no matching function for call to 'PlaylistsManager::PlaylistsManager()'
new (from++) T();
^

why do i keep getting this error?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
///playlistmanager.h
class PlaylistsManager
{
public:
    PlaylistsManager(QVector<MyMusicPlayer*>);
    PlaylistsManager(QString name);
private:
    QVector<MyMusicPlayer*> playlist; // same as std::vector;
    QString name; // same as std::string
};

///////////////////////on other cpp:
class MainWindow
{
public:

void createNewPlaylist(QString);

private:

QVector<PlaylistsManager> playlists; //same as std::vector;
};


void MainWindow::createNewPlaylist(QString playlist_name)
{
    playlists.push_back(playlist_name); // this is where error comes from
}


Last edited on
QVector<PlaylistsManager> playlists;

You're calling the PlaylistManager default constructor here which doesnt exist. Add this to the class -

PlaylistsManager(){}
thats my problem im not calling the default constructor, im using the PlaylistManager::PlaylistsManager(QString name); constructor

EDIT: i see, im calling it when i list intialize it in the constructor :) thanks
but when i removed line 27 i dont get any error why?
Last edited on
I might have been wrong with my first post, Im not sure if it calls the default constructor, someone will have to correct me on that. The problem on line 27 is this.

playlist is a Vector of PlaylistManager . in that function, you're pushing back a string, when your vector doesnt take strings, it takes PlayListManager, like this one -

1
2
PlaylistManager playlist;
playlists.pushback(playlist); 
Last edited on
Please post the constructor implementations. And post the complete error message, all of them, exactly as they appear in your development environment. There should probably be several messages related to the message you have posted.
Assuming that your QVector is the one described here: http://doc.qt.io/qt-4.8/qvector.html#push_back the line should be playlists.push_back(PlaylistsManager(playlist_name));

It doesn't appear to have an equivalent to emplace_back.
Last edited on
Please post the constructor implementations.


here is the screenshot of complete error messages:
http://imgur.com/H1JmOib


here is the implementation of the constructors:
1
2
3
4
5
6
7
8
9
PlaylistsManager::PlaylistsManager(QString name)
{
    setName(name);
}

void PlaylistsManager::setName(QString name)
{
    this->name = name;
}
the line should be playlists.push_back(PlaylistsManager(playlist_name));


i already tried that, but still the same.
when your vector doesnt take strings, it takes PlayListManager, like this one - 


it doesnt take a pointer or a reference to PlaylistsManager object or QString so it should be okay if i call the constructor directly, not sure so correct me if im wrong
Last edited on
here is the screenshot of complete error messages:

Well, that makes it pretty clear. QVector requires a type with a default constructor, and your type doesn't have one. It's an odd design decision for QVector::reallocData to default construct objects, but... there it is.
i think std::vector doesnt require it
but thanks guys :)
I found this in:

http://doc.qt.io/qt-5/containers.html


QtDoc wrote:
The values stored in the various containers can be of any assignable data type. To qualify, a type must provide a default constructor, a copy constructor, and an assignment operator. This covers most data types you are likely to want to store in a container, including basic types such as int and double, pointer types, and Qt data types such as QString, QDate, and QTime, but it doesn't cover QObject or any QObject subclass (QWidget, QDialog, QTimer, etc.). If you attempt to instantiate a QList<QWidget>, the compiler will complain that QWidget's copy constructor and assignment operators are disabled. If you want to store these kinds of objects in a container, store them as pointers, for example as QList<QWidget *>.
@TheIdeasMan
can you tell me my disadvantage here if i make a default constructor of PlaylistsManager?

in my program, i want to disable pushbacking a PlaylistsManager using default constructor.
Last edited on
Hi,

in my program, i want to disable pushbacking a PlaylistsManager using default constructor.

I really don't know, maybe try asking on the Qt forum?

I would be tempted to try storing them as pointers. The lack of an emplace_back seems awkward, maybe there is some other way of doing this?

Good Luck, and happy seasonal greetings :+)
Topic archived. No new replies allowed.