Error C2059 : 'constant' when trying to create a Qt container in a header file with known size

StackOverflow https://stackoverflow.com/questions/21159520

  •  28-09-2022
  •  | 
  •  

Question

I am using Qt 4.8.5 with MSVC 2010 compiler and debugger from windows 7.0 SDK, and Qt creator as my IDE.

The following syntax works fine:

class Device
{
 public:
    Device();
    ....
    QVector<double> MyContainer;
    ....
 protected:
    ....
}

, where QContainer can be QVector, QList...etc, and T can be any type.

But the following syntax is denied by Qt creator and shows "Error C2059" while attempting to compile:

class Device
{
 public:
    Device();
    ....
    QVector<double> MyContainer(100);
    ....
 protected:
    ....
}

I am so confused since the document says the syntax "QContainer = MyContainer(szie)"is legitimate, but my Qt creator just can't read and it tells me there is an "unexpected token '('".

Am I doing worng?

Was it helpful?

Solution

It's because you are trying to assign to a type. QVector<double> is a type, and not a variable declaration or anything else you can assign to. That means that both examples are actually wrong.

I think you mean to declare the member variable MyContainer, for which you should use

QVector<double> MyContainer;

To initialize the container to a specific size, you have to use the constructors initializer list:

Device()
    : MyContainer(100)
{
    ...
}

For more information about initializer lists, see e.g. this tutorial, or this reference.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top