2

I got following issue: That's work:

#include <QtCore/QCoreApplication>
#include <QColor>
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QColor *c = new QColor();
    c->setRgb(12,123,13);
    return a.exec();
}

but this don't:

#include <QtCore/QCoreApplication>
#include <QColor>
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QColor c();
    c.setRgb(123,213,2);
    return a.exec();
}

Qtcreator get me:

request for member 'setRgb' in 'c' which is of non-class type 'Qcolor()'

What is going on?

EDIT OK solution was use Qcolor c without '()', but what if it is member of class? Then direct access still doesn't work... ie:

class X{

QColor c;

  void func(){
     c.setRgb(1,2,3);
  }
}
John X
  • 499
  • 2
  • 7
  • 15
  • 1
    possible duplicate of [Why is it an error to use an empty set of brackets to call a constructor with no arguments?](http://stackoverflow.com/questions/180172/why-is-it-an-error-to-use-an-empty-set-of-brackets-to-call-a-constructor-with-no) – Yakov Galka Dec 05 '10 at 15:29
  • Now it's legal C++. Your tools may be bugged. What error do you get? – Yakov Galka Dec 05 '10 at 15:45
  • My app crashed... (it must be a reason because if i change on c->setRgb(1,2,3) and Qcolor *c; app works... – John X Dec 05 '10 at 15:47
  • Fine, then it doesn't fall in the scope of this question anymore. It depends on how you use `c`, which you doesn't show in the last piece of code. – Yakov Galka Dec 05 '10 at 15:51

2 Answers2

3

This

QColor c();

is a function declaration. Change it to

QColor c;

That's a possible duplicate of hundreds of similar questions....

Yakov Galka
  • 70,775
  • 16
  • 139
  • 220
  • could you explain it a little more? Even Qtcreator tipping '()' – John X Dec 05 '10 at 15:27
  • @John: I have no idea what's Qtcreator, neither I know Qt. Probably because your code completion utility expects you to type something between the parentheses or delete them otherwise. See the dupe I posted above. – Yakov Galka Dec 05 '10 at 15:31
  • Thx... last question: what is about if Qcolor is member of class? Then it doesn't work still – John X Dec 05 '10 at 15:32
  • What exactly is your code? If you write `QColor c();` as a member of a class then it's still a function declaration, so it won't work. When you remove the `()` it will work. – Yakov Galka Dec 05 '10 at 15:38
1

Declare like

QColor c;

Öö Tiib
  • 10,809
  • 25
  • 44