简体   繁体   English

在C ++中返回Struct

[英]Returning a Struct in C++

I know this question has been asked a million times but I've looked at so many answers and not a single one was helpful. 我知道这个问题已被问了一百万次,但我看了很多答案,而没有一个答案是有帮助的。

Basically I'm creating a login form and I need to return a struct containing the email, password, and a boolean to tell whether to remember the login info specified. 基本上我正在创建一个登录表单,我需要返回一个包含电子邮件,密码和布尔值的结构,以告知是否记住指定的登录信息。

I have a class called Database which is made up of "database.cpp" and "database.h". 我有一个名为Database的类,它由“database.cpp”和“database.h”组成。 in the header file, I have: 在头文件中,我有:

public:
    typedef struct{
        QString email;
        QString password;
        bool remember;
    }LoginInfo;

    LoginInfo getLoginInfo();

Then in the source file, I have: 然后在源文件中,我有:

LoginInfo Database::getLoginInfo()
{
    LoginInfo data;
    data.email = QString("email@example.com");
    data.password = QString("test12345");
    data.remember = true;

    return data;
}

The error I seem to be getting is that I need a semicolon before "Database::getLoginInfo()" and that I have a redefinition of Database::getLoginInfo() when I only have one. 我似乎得到的错误是我在“Database :: getLoginInfo()”之前需要一个分号,并且当我只有一个时,我有一个重新定义的Database :: getLoginInfo()。

I don't know a whole lot in c++ and this is actually my first time using structs, but I have allot of programming experience from Java and Python. 我在c ++中并不了解很多,这实际上是我第一次使用结构体,但是我有很多来自Java和Python的编程经验。 So being that I could still be considered a "noob" at c++, I'm sure I have left some silly mistakes in the code. 因此,我仍然可以被认为是c ++的“noob”,我确信我在代码中留下了一些愚蠢的错误。

So yeah if you could help me that'd be great. 所以,如果你能帮助我,那就太好了。 But like I said, I've been trying to get this to work for about 2 hours now with no luck and I've givin' up searching. 但就像我说的那样,我一直试图让这个工作大约2个小时,现在没有运气,而且我正在寻找。

结构被定义为类的成员,因此您需要使用:

Database::LoginInfo Database::getLoginInfo()

You need to properly scope your struct in the function definition. 您需要在函数定义中正确定义结构的范围。

Database::LoginInfo Database::getLoginInfo() {
    .
    .
    .
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM