将列表作为返回类型传递给c ++

时间:2013-11-19 18:26:54

标签: c++ class scope return-type

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

User.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;
}

的main.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.cpp,line1,line2等中的char变量时,我收到错误:标识符“line1”未定义。

2 个答案:

答案 0 :(得分: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)
{

}

答案 1 :(得分:1)

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

#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”来表示类型(User),对变量使用“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;
}