Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
165 views
in Technique[技术] by (71.8m points)

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

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.
Reason: 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.
Reason: 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)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...