Making sense of primary-expression error

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();
}

Published by

Amit Bahree

This blog is my personal blog and while it does reflect my experiences in my professional life, this is just my thoughts. Most of the entries are technical though sometimes they can vary from the wacky to even political – however that is quite rare. Quite often, I have been asked what’s up with the “gibberish” and the funny title of the blog? Some people even going the extra step to say that, this is a virus that infected their system (ahem) well. [:D] It actually is quite simple, and if you have still not figured out then check out this link – whats in a name?

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.