错误:C2228:'' 的左边必须有类/结构/联合

2021-12-08 00:00:00 qt sqlite c++

我是使用 Qt 学习 C++ 的很长时间 Java 用户,但我在理解方法的工作原理方面遇到了很多麻烦.现在,我正在尝试找出数据库,并尝试使用标题简化我的代码.通常在 Java 中,我只会有一个名为 DatabaseControl 的类,它带有一个 void 方法,可以执行我想要的任何操作.例如,像我现在所做的那样,将员工添加到数据库中.我会通过做类似的事情来实例化这个类

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

然后用

myDBControl.addEmploye();

会弹出一系列输入框供用户输入员工信息——姓名、部门等

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

那么,现在转到 C++.我有我的标题

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

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

    //Methods
    void addEmployee();
};

我的构造函数中没有任何参数,因为我想做的就是在我的主程序中调用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();
}

我希望只运行我在头文件中编写的 addEmployee 方法.但是,当我编译时,出现错误 Error: C2228: left of '.addEmployee' must have class/struct/union

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

我已经查看了此错误的其他实例,但并没有真正理解到底出了什么问题,我觉得这是由于我对 C++ 中的方法的误解,因为我知道在 Java 中这样的事情可以毫无问题地工作(假设标题中的代码是正确的,但很可能不是)

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)

推荐答案

您在这里犯了一个错误:

You made an error here:

DatabaseControl myDBControl();

您声明了一个名为 myDBControl 的函数,不带任何参数并返回一个 DatabaseControl.

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.

相关文章