【问题标题】:Passing a list as a return type in c++?在 C++ 中将列表作为返回类型传递?
【发布时间】:2013-12-03 10:58:58
【问题描述】:

User.h

#pragma once
#include <iostream>
#include <string>
#include <fstream>
#include <list>

class User
{
private:

char line1[50];
char line2[50];
char line3[50];
char line4[50];

public:

void getUser(std::string);
};

用户.cpp

#include "User.h"

void getUser(std::string username)
{
std::ifstream fin(username + ".txt");
fin.getline(line1, 50);
fin.getline(line2, 50);
fin.getline(line3, 50);
fin.getline(line4, 50);
fin.close();

std::list<std::string> UserInfo;

UserInfo.push_back(line1);
UserInfo.push_back(line2);
UserInfo.push_back(line3);
UserInfo.push_back(line4);

return UserInfo;
}

ma​​in.cpp

#include "User.h"

std::string username;

User display;

std::cout << std::endl << "Please enter your username: ";
std::getline (std::cin, username);
display.getUser(username);

我想在 main 中访问列表 UserInfo - 我假设它必须被返回,但是,我不知道返回类型是什么? (在我知道返回类型之前,void 只是暂时的)。

我遇到的另一个问题是在访问User.cppline1line2 等中的 char 变量时,出现错误:

标识符“line1”未定义。

【问题讨论】:

    标签: c++ class scope return-type


    【解决方案1】:

    与您的列表相同:

    std::list<std::string> getUser(std::string username)
    {
    
      return UserInfo ;
    }
    

    或将其作为参考传递:

    void getUser(std::string username, std::list<std::string>& UserInfo)
    {
    
    
    }
    

    char数组line1等是私有成员,只能在类的成员函数内部或通过friend函数访问。

    定义你的getUser 课外

    void User::getUser(std::string username)
    {
    
    }
    

    【讨论】:

    • 谢谢 - 就返回列表而言,这似乎有效 - 你能否提出一些关于为什么我无法从 User.h 访问行变量的建议?
    • 我以为 getUser() 是类的成员函数?
    • 很抱歉很痛苦,但我现在坚持实际访问(输出)main.cpp 中的 UserInfo 列表
    • 你也可以通过给这种类型的列表一个类型名称然后使用它来让你的生活更简单。例如:typedef std::list&lt;std::string&gt; UserInfo;。这不仅可以更容易输入,而且还可以兼作自我文档。 UserInfo User::getUser(const std::string&amp; userName);
    • @user3001499 那应该是另一个问题,因为你可以得到各种各样的答案。 (重载&lt;&lt;或者干脆使用成员函数)
    【解决方案2】:

    让我们将所有内容放在一个文件中,以便我们可以制作它的独立可编译版本。

    #include <string>
    #include <list>
    #include <iostream>
    
    // make an alias for this type of list and call it 'UserInfo'.
    typedef std::list<std::string> UserInfo;
    
    class User
    {
        // private is the default for class, it's the only way
        // it differs from using 'struct'.
        char m_line1[50];
        char m_line2[50];
        char m_line3[50];
        char m_line4[50];
    
    public:
        UserInfo getUser(const std::string&);
    };
    
    UserInfo User::getUser(const std::string& username)
    {
        std::ifstream fin(username + ".txt");
        fin.getline(m_line1, sizeof(m_line1));
        fin.getline(m_line2, sizeof(m_line1));
        fin.getline(m_line3, sizeof(m_line1));
        fin.getline(m_line4, sizeof(m_line1));
        fin.close();
    
        UserInfo info;
    
        info.push_back(m_line1);
        info.push_back(m_line2);
        info.push_back(m_line3);
        info.push_back(m_line4);
    
        return info;
    }
    
    int main()
    {
        std::string username;
        User display;
    
        std::cout << std::endl << "Please enter your username: ";
        std::getline(std::cin, username);
    
        UserInfo info = display.getUser(username);
        for (auto it = info.begin(); it != info.end(); ++it) {
            std::cout << *it << "\n";
        }
    
        return 0;
    }
    

    对您的代码的一些评论:

    与您使用的大小写保持一致。您使用所谓的“UpperCamelCase”来表示类型(用户),使用“lowerCamelCase”表示变量并坚持使用它 - 否则您将遇到变量名称和类型之间的冲突。

    其次,您选择了一个 std::list,其中 std::vector 看起来会好得多。

    接下来,您将返回数据的方式与存储数据的方式解耦。为什么不将这些行存储为 std::strings?

    您的“用户”对象有点含糊不清。它有一些字符串字段,在创建对象时您不会对其进行初始化,并且它有一个函数可以获取有关特定用户的数据,将其存储在对象中,然后以不同的格式返回。

    这就是我将如何实现该类:

    #include <string>
    #include <vector>
    #include <iostream>
    
    typedef std::vector<std::string> UserInfo;
    
    class User
    {
        std::string        m_username;
        UserInfo        m_info;
    
    public:
        User(const std::string& name);
    
        enum { NumInfoLines = 4 };    // how many lines of info we use.
    
        // Accessor function to retrieve the userinfo
        // Instead of passing a copy, provide read-only access
        // to our internal copy - read-only so you have to go thru
        // the class to modify it.
        // Mark the function call as 'const' because it has no
        // side effects. This may allow the compiler to do some
        // optimizations for us.
        const UserInfo& getInfo() const { return m_info; }
    };
    
    User::User(const std::string& username)
        : m_username(username), m_info()
    {
        std::ifstream fin(m_username + ".txt");
        std::string inputLine;
        m_info.reserve(NumInfoLines);
        for (size_t i = 0; i < NumInfoLines; ++i) {
            fin.getline(inputLine);
            m_info.push_back(inputLine);
        }
    }
    
    int main()
    {
        std::string username;
        std::cout << std::endl << "Please enter your username: ";
        std::getline(std::cin, username);
    
        User user(username);
        const UserInfo& info = user.getInfo();
        for (auto line : info) {
            std::cout << line << "\n";
        }
    
    /* or you could use
        const size_t numInfoLines = info.size();
        for (size_t i = 0; i < numInfoLines; ++i) {
            std::cout << i << ": " << info[i] << "\n";
        }
    */
    
        return 0;
    }
    

    【讨论】:

    • 非常感谢,将尝试一下你的代码,看看我是怎么做的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-27
    • 2012-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多