Can't initialize a static QList?

I get the following error:

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

Here are 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 is the line that gives the error:

QList<int> Cube::colorList << YELLOW << RED << GREEN << ORANGE << BLUE << WHITE;
+5
source share
2 answers

You cannot initialize an object with <<. =, which is usually absent, operator=()is a special syntax that essentially matches a constructor call.

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 of the list structure and the prohibition of some optimization - a copy of the created list.

+7
source

/ QList Cube:: colorList. , (QList Cube:: colorList).

QT , .

+1

All Articles