C++ 错误:C2228:'' 的左边必须有类/结构/联合
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20871225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error: C2228: left of '' must have class/struct/union
提问by Lighthat
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
我是使用 Qt 学习 C++ 的 Java 用户,但在理解方法的工作原理时遇到了很多麻烦。现在,我试图找出数据库,并试图用标题简化我的代码。通常在 Java 中,我只有一个名为 DatabaseControl 的类,它带有一个可以执行我想要的任何内容的 void 方法。例如,像我现在所做的那样,将员工添加到数据库中。我会通过做类似的事情来实例化这个类
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
所以,现在转到 C++。我有我的标题
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
我的构造函数中没有任何参数,因为我想要做的就是在我的 main 中调用“addEmployee”方法,如上所示。在同一个头文件中,我的类声明下面有这个
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
我希望只运行我在头文件中编写的 addEmployee 方法。但是,当我编译时,出现错误 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)
我已经查看了此错误的其他实例,但并没有真正理解到底出了什么问题,我觉得这是由于我对 C++ 中的方法的误解,因为我知道在 Java 中这样的事情可以毫无问题地工作(假设代码在标题是正确的,但很可能不是)
回答by Lightness Races in Orbit
You made an error here:
你在这里犯了一个错误:
DatabaseControl myDBControl();
You declared a function called myDBControl
taking no arguments and returning a DatabaseControl
.
您声明了一个myDBControl
不带参数的函数,并返回一个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.
这与(但不完全是)“最令人烦恼的解析”有关,因为它是由相同的语言规则引起的,即语句是函数声明,如果它们可以如此解析。
回答by Digital_Reality
DatabaseControl myDBControl();
should be
应该
DatabaseControl myDBControl;
回答by Kerrek SB
You need to say this:
你需要这样说:
DatabaseControl myDBControl;
myDBControl.addEmployee();
回答by Joseph D.
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?]
[?注:由于
()
初始化程序的语法不允许,X a();
不是 的对象
class X
的声明,而是不带参数并返回 X 的函数的 声明。该形式()
在某些其他初始化上下文中是允许的([expr.new]、[expr.type.conv]、[class.base.init])。—?尾注?]