-1

I don't find any solutions to this error: invalid initialization of non-const reference of type ‘Controller&’ from an rvalue of type ‘ < brace-enclosed initializer list> ’

it's in this function:

QTPlay::QTPlay(Controller &c,QWidget *parent) : c{ c }, QWidget{ parent }
{
    this->initGUI();
    this->currentMovie = this->c.repo.movies;
    this->populaterepo();
}

this is the object:

class QTPlay : public QWidget
{
private:
    Q_OBJECT
public:

QTPlay(Controller &c, QWidget *parent =0);
private:
Controller &c;
std::vector<Movie> currentMovie;

QListWidget* repo;
QLineEdit* title;
QLineEdit* genre;
QLineEdit* year;
QLineEdit* likes;
QLineEdit* trailer;
QPushButton* addButton;
QPushButton* deleteButton;
QPushButton* filterButton;
QPushButton* moveOneMovieButton;
QPushButton* playmovieButton;
QPushButton* nextmovieButton;

QListWidget* playList;

void initGUI();
void populaterepo();
void populatePlaylist();
int getRepoListSelectedIndex();

int getPlayListSelectedIndex();

void connectSignalsAndSlots();

private slots:
void listItemChanged();

void addMovie();
void deleteMovie();
void filterRepoMovies();
void moveMovieToPlaylist();
void playmovie();
void nextmovie();
};

I call it here:

Controller c(repo, p);
QTPlay w{c};

I am also working with the Linux version of Qt if that is relevant.

This question is different since the type is not int but a custom made Controller and uses the framework Qt.

Paul
  • 11
  • 1
  • 5
  • 1
    Were you connected to the Internet while researching your problem? – LogicStuff May 21 '16 at 16:26
  • 1
    Possible duplicate of [error: invalid initialization of non-const reference of type ‘int&’ from an rvalue of type ‘int’](http://stackoverflow.com/questions/8293426/error-invalid-initialization-of-non-const-reference-of-type-int-from-an-rval) – LogicStuff May 21 '16 at 16:28

1 Answers1

-2

Change the name of the argument to the constructor or the name of the member variable so they are not the same (perhaps by appending _ to one of them. It may be that what you are doing is legal or it may not - but it is clearly confusing the compiler. (If I saw it in a code review, I'd have to stop and check the standard to see if it was legal or not.)

  • I didn't down vote. But having a constructor parameter with the same name as a field is perfectly legal (and common practice in some circles). If the compiler is confused by it, then the compiler is broken. The rules for name lookup are different for the members and their initialisers, and do the right thing in each case. – Alan Stokes May 21 '16 at 16:34
  • I tried it it didn't work. I cannot vote yet so IDK about the downvote. – Paul May 21 '16 at 16:36
  • @AlanStokes: I thought that was the case, but if the compiler is broken, the OP has to change his code or his compiler. I think it's easier to change the code - I should probably edit my post to make that clearer. – Martin Bonner supports Monica May 21 '16 at 16:37
  • Hmm. What compiler? Does it work if you intiailize with `c(c)` instead? And are you specifying C++11 / C++14 standard? – Martin Bonner supports Monica May 21 '16 at 16:38
  • I run it with g++ -std=c++11, tough this happens when running the program with qt's makefile(i do force it to run with std=c++11).It passes when initialized like that, tough I have other files that are initialized that way and have no problem. – Paul May 21 '16 at 16:43