簡體   English   中英

將列表作為返回類型傳遞給c ++

[英]Passing a list as an return type c++

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”未定義。

與您的清單相同:

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)
{

}

讓我們把所有東西放在一個文件中,這樣我們就可以制作一個獨立的可編譯版本。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM