Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't initialize static QList?

Tags:

c++

static

qt

I get the following error:

Cube.cpp:10: error: expected initializer before ‘<<’ token

Here's the important parts of the header file:

#ifndef CUBE_H
#define CUBE_H

#include <cstdlib>
#include <QtCore/QtCore>
#include <iostream>

#define YELLOW 0
#define RED 1
#define GREEN 2
#define ORANGE 3
#define BLUE 4
#define WHITE 5

using namespace std;

class Cube {
public:
  ...
  static QList<int> colorList;
  ...
};
#endif

Here's the line that gives the error:

QList<int> Cube::colorList << YELLOW << RED << GREEN << ORANGE << BLUE << WHITE;
like image 205
dfetter88 Avatar asked Dec 01 '10 02:12

dfetter88


2 Answers

You can't initialize an object with <<. The = that is usually there is not operator=() -- it's a special syntax that is essentially the same as calling a constructor.

Something like this might work

QList<int> Cube::colorList = EmptyList() << YELLOW << RED << GREEN << ORANGE << BLUE << WHITE;

where EmptyList() is

QList<int> EmptyList()
{
   QList<int> list;
   return list;
}

and is a copy construction of a list, and barring some optimization, a copy of the list that is created.

like image 191
Lou Franco Avatar answered Sep 18 '22 18:09

Lou Franco


That line is not a initialization/definition of QList Cube::colorList. It is invoking insertion operator on an object which is not yet defined namely (QList Cube::colorList).

I don't know QT and hence can't comment on how to really initialize this class.

like image 42
Chubsdad Avatar answered Sep 22 '22 18:09

Chubsdad