Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: C2228: left of '' must have class/struct/union

Tags:

c++

sqlite

qt

I'm a long time Java user learning C++ with Qt and I'm having a lot of trouble understanding how methods work. Right now, I'm trying to figure out databases, and tried to simplify my code with a header. Normally in Java I would just have a class called DatabaseControl with a void method that would execute whatever I wanted. For example, adding an employee to a database, as I'm doing now. I'd instantiate the class, by doing something like

DatabaseControl myDBControl = new DatabaseControl();

and then execute the method with

myDBControl.addEmploye();

which would bring up the series of input boxes for the user to input the information on the employee - name, department, etc.

So, now over to C++. I have my header

class DatabaseControl
{
public:
    DatabaseControl();
    ~DatabaseControl();

    //Methods
    void addEmployee();
};

I don't have any parameters in my constructors because all I want to do is call the "addEmployee" method in my main as I've shown above. In the same header file I have this below my class declaration

void DatabaseControl::addEmployee(){
QSqlQuery qry;
bool ok;
QString firstName = QInputDialog::getText(NULL, "QInputDialog::getText()",
                                     "Employee first name:", QLineEdit::Normal,
                                     NULL, &ok);
if (ok && !firstName.isEmpty()){}
else{
    QMessageBox msgBox;
    msgBox.setWindowTitle("Error");
    msgBox.setText("Failed to add employee.\nReason: No employee name given.");
    msgBox.exec();
}
QString lastName = QInputDialog::getText(NULL, "QInputDialog::getText()",
                                     "Employee last name:", QLineEdit::Normal,
                                     NULL, &ok);
     if (ok && !lastName.isEmpty()){
         qry.prepare("INSERT INTO employees (firstname, lastname)" "VALUES (:f1, :f2)");
         qry.bindValue(":f1", firstName);
         qry.bindValue(":f2", lastName);
         qry.exec();
     }
     else{
         QMessageBox msgBox;
         msgBox.setWindowTitle("Error");
         msgBox.setText("Failed to add employee.\nReason: No employee name given.");
         msgBox.exec();
     }

}

and then in my main I have this:

void MainWindow::on_addEmployee_clicked()
{
    DatabaseControl myDBControl();
    myDBControl.addEmployee();
}

which I expected to just run the addEmployee method I wrote in the header file. However, when I compile, I get the error Error: C2228: left of '.addEmployee' must have class/struct/union

I've looked at other instances of this error and don't really understand exactly what's wrong, and I feel it comes from my misunderstanding of methods in C++, because I know in Java something like this would work without issue (assuming the code in the header is correct which it very well may not be)

like image 368
ozzymado Avatar asked Jan 01 '14 17:01

ozzymado


4 Answers

You made an error here:

DatabaseControl myDBControl();

You declared a function called myDBControl taking no arguments and returning a DatabaseControl.

Object declarations without any constructor arguments must omit the ():

DatabaseControl myDBControl;

This is related to (but is not precisely) the "most vexing parse", in that it's caused by the same language rule that statements are function declarations if they can be so parsed.

like image 83
Lightness Races in Orbit Avatar answered Nov 14 '22 17:11

Lightness Races in Orbit


DatabaseControl myDBControl();

should be

DatabaseControl myDBControl;
like image 41
Digital_Reality Avatar answered Nov 14 '22 19:11

Digital_Reality


You need to say this:

 DatabaseControl myDBControl;
 myDBControl.addEmployee();
like image 1
Kerrek SB Avatar answered Nov 14 '22 19:11

Kerrek SB


In support to the accepted answer.

From dcl.init#11:

An object whose initializer is an empty set of parentheses, i.e., (), shall be value-initialized.

[ Note: Since () is not permitted by the syntax for initializer,

X a();

is not the declaration of an object of class X, but the declaration of a function taking no argument and returning an X. The form () is permitted in certain other initialization contexts ([expr.new], [expr.type.conv], [class.base.init]). — end note ]

like image 1
Joseph D. Avatar answered Nov 14 '22 18:11

Joseph D.