Dec 22 2009

Making sense of primary-expression error

Category: .codeAmit Bahree @ 11:38 am

If you are new to C++ then some of the compiler errors can be a bit confusing and daunting at first. For example if the compiler given you an expected primary-expression error (e.g. expected primary-expression before ‘.’ token), at face value it does not make sense.

void RGBViewer::accept() {
	QMessageBox::information(this, "Button Clicked", "You clicked OK", QMessageBox::Ok);

	QColor color = QColor(Ui_RGBViewerClass.spinBoxRed->value(),
			Ui_RGBViewerClass.spinBoxGreen->value(),
			Ui_RGBViewerClass.spinBoxBlue->value(),
			255);

	qDebug() << color;

	QBrush brush(color);
	brush.setStyle(Qt::SolidPattern);
	ui.graphicsView->setBackgroundBrush(brush);
	ui.graphicsView->backgroundBrush();
}

When I compile the above code snippet, I get the following error from the compiler:

rgbviewer.cpp: In member function ‘virtual void RGBViewer::accept()’:
rgbviewer.cpp:19: error: expected primary-expression before ‘(’ token
rgbviewer.cpp:19: error: expected primary-expression before ‘.’ token
rgbviewer.cpp:20: error: expected primary-expression before ‘.’ token
rgbviewer.cpp:21: error: expected primary-expression before ‘.’ token

All this means is that I need to initialize the type ‘Ui_RGBViewerClass’ as the compiler does not understand it. In other words, I need to use a new.

Here is the ‘fixed’ version below of the same code snippet.

Ui::RGBViewerClass ui; //Defined in the header; shown here for completness

void RGBViewer::accept() {
	QMessageBox::information(this, "Button Clicked", "You clicked OK", QMessageBox::Ok);

	QColor color = QColor(ui.spinBoxRed->value(),
			ui.spinBoxGreen->value(),
			ui.spinBoxBlue->value(),
			255);

	qDebug() << color;

	QBrush brush(color);
	brush.setStyle(Qt::SolidPattern);
	ui.graphicsView->setBackgroundBrush(brush);
	ui.graphicsView->backgroundBrush();
}
Other similar posts to check out:
  • March 1, 2010 -- Printing code and making it look pretty (0)
    If you are on Linux and want to print some code and also make it look pretty then check out a2ps (Any to postscript filter). Of course if you can avoid printing in the first place and saving paper and trees and make it greener that is ideal - however there are times that is not possible. I tried printing from CDT, but the printing options from CDT just looks plain ugly and big fonts and can spread over 10 pages for a simple code file (spanning 293 lines). Sure I can tweak the font in CDT, but th...
  • February 17, 2010 -- Ten commandments of Programming (0)
    I came across the Ten commandments of Programming while looking at a question on StackOverflow and I can't believe I have not seen these before. I think every developer, lead, architect, dba, pm, whoever should print this out! 8-) Understand and accept that you will make mistakes. The point is to find them early, before they make it into production. Fortunately, except for the few of us developing rocket guidance software at JPL, mistakes are rarely fatal in our industry, so we can, and sho...
  • January 23, 2010 -- Copying strings in C++ (0)
    Here is a good example on why either you love C++ or hate it with such terse expression oriented code; I think its pretty cool. If you want to copy one string to another, one option can be something like this. [sourcecode lang="cpp" toolbar="false"] void mycopy(char *p, char *q) { int len = strlen(q); for(int i=0; i&lt;=len; i++) p[i] = q[i]; } [/sourcecode] However this achieves the same thing as above and is more efficient: [sourcecode lang="cpp" toolbar="false"] v...
  • January 10, 2010 -- ‘QPainter painter’ has initialiser but incomplete type (0)
    If you ever got an error something like [some-class] has initialiser but incomplete type, it basically means the compiler cannot understand the type and you need to add the include for it. [sourcecode lang="cpp" toolbar="false"] QPixmap pixmap(20,10); pixmap.fill(Qt::white); QPainter painter(&pixmap); QPen pen(Qt::blue); [/sourcecode] Take the code snipped above when you compile it you might get an error something along the lines of the following for line 4. ‘QPainter painter’ ...

Tags:

Leave a Reply

Get Adobe Flash playerPlugin by wpburn.com wordpress themes