5

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; 

2 Answers 2

8

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I used something similar to what you've provided. Instead of empty list, I used new QList<int>() << ...; It seems to work. Do you see any potential issues in using this method?
Just that you are creating a list and then copying it. For such a short list and for just once in the app, I wouldn't worry about it.
1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.