Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何返回对象C++;_C++ - Fatal编程技术网

C++ 如何返回对象C++;

C++ 如何返回对象C++;,c++,C++,所以,我有这个问题。 基本上,我创建了一个包含字符串的类,然后创建了一个包含该字符串的类数组的函数,我想将其返回到main(),但只返回wordBank[];不起作用。 有人能给我解释一下为什么我的代码不起作用,以及我需要做些什么才能使它起作用吗?对不起,我是C++初学者。谢谢,下面是代码: #include <iostream> #include <fstream> using namespace std; // wordlist object class word

所以,我有这个问题。 基本上,我创建了一个包含字符串的类,然后创建了一个包含该字符串的类数组的函数,我想将其返回到main(),但只返回wordBank[];不起作用。 有人能给我解释一下为什么我的代码不起作用,以及我需要做些什么才能使它起作用吗?对不起,我是C++初学者。谢谢,下面是代码:

#include <iostream>
#include <fstream>

using namespace std;

// wordlist object
class wordList {
public:
    string word;
};

// function that is supposed to fill my wordList class object with words
wordList* readWordList() {
    wordList wordBank[3];
    string wlist = "wordlist.txt";
    ifstream data(wlist);
    while (!data.eof()) {
        for (int i = 0;i < 3;i++) {
            data >> wordBank[i].word;
        }
    }
    data.close();
    return wordBank;
}

//main function
int main()
{
    wordList wordBank[2];
    wordBank = *readWordList() ; // ?
    std::cout << wordBank[2].word;
}
#包括
#包括
使用名称空间std;
//单词表对象
类词表{
公众:
字符串字;
};
//函数,该函数应该用单词填充我的wordList类对象
单词列表*readWordList(){
字库[3];
字符串wlist=“wordlist.txt”;
ifstream数据(wlist);
而(!data.eof()){
对于(int i=0;i<3;i++){
数据>>字库[i].word;
}
}
data.close();
返回字库;
}
//主要功能
int main()
{
字库[2];
wordBank=*readWordList();/?

std::cout
wordBank
readWordList()
函数的本地函数。因此该函数结束后,
wordBank
和它拥有的所有内存都不再属于您。换句话说,您通过尝试查看返回的内容来调用未定义的行为

相反,我建议您只需使用向量:

std::vector<wordList> readWordList() {
    std::vector<wordList> wordBank(3);
    string wlist = "wordlist.txt";
    ifstream data(wlist);
    while (!data.eof()) {
        for (int i = 0;i < 3;i++) {
            data >> wordBank[i].word;
        }
    }
    data.close();
    return wordBank;
}
std::vector readWordList(){
std::向量字库(3);
字符串wlist=“wordlist.txt”;
ifstream数据(wlist);
而(!data.eof()){
对于(int i=0;i<3;i++){
数据>>字库[i].word;
}
}
data.close();
返回字库;
}

最简单的解决方案是使用
std::vector
。数组是一个构建块-简单但不太方便。

您不能返回数组。您可以返回一个指针(指向数组的开头),但随后会遇到问题,因为数组的生存期只是函数范围

正确的方法是使用
std::vector

std::vector<wordList> wordBank(3);
std::vector词库(3);

它的行为类似于可以通过值返回的数组。

您可以使用
std::vector
吗?使用std::vector。打开警告(编译器可能会告诉您为什么不能这样做)不要使用
C
,而是使用
C++
。因此,当函数结束时,使用
std::array
std::vector
wordBank
,就像函数的所有其他局部变量一样。返回的指针不再指向任何特定的对象。我讨厌没有解释的否决投票,even当我不是受害者时。不是向下投票者,但要小心。我不知道为什么我们要永久性地回答重复的问题…似乎有人对所有答案进行了向下投票者投票,因为这是一个经常重复的问题。@Klaus选择的重复答案建议返回数组作为指针。它严重过时,大多数人更倾向于建议t中的std::vector他的日子age@Jeffrey:这个问题有更多的重复项,也有更好的重复项。我同意有必要对这些重复项进行排序,但我不同意再次回答是更好的解决方案。@Jeff公平地说,StackOverflow格式要求最好将其发布到重复目标上,以保持它是最新的,而不是这个会永远消失的傻瓜。这里的问题是在我回答的时候这个问题没有被标记为傻瓜,所以我不知道。