如何声明返回 class 实例的函数,该实例在同一个 class 中使用?
How to declare a function returning a class instance, that is used in the same class?
我已经尝试了几个星期并搜索了几天的答案,但没有找到。我的代码比较大而且错综复杂,但我的问题是3functions/classes,所以我只展示我的声明和相关信息。我有以下不兼容的代码:
class Word{
private:
*members*
public:
//friend declaration so i could access members and use it in class - doesn't help
friend Word search_in_file(const string& searchee);
//function that uses previous function to create a Word object using data from file:
//type int to show it succeeded or failed
int fill(const string& searchee){
Word transmission = search_in_file(searchee);
//here are member transactions for this->members=transmission.member;
}
};
//function to return Word class from file:
Word search_in_file(const string& searchee){
//code for doing that
}
我已经尝试了所有可以声明函数或 class 的可能性,但没有找到解决方案。起初我只在构造函数中使用 search_in_file() 函数(现在与函数 fill() 有同样的问题)并在 [=22= 中声明并定义了 search_in_file() 函数].然后它像上面的代码一样工作(唯一的例外是友元函数也是具有定义的实际函数)。但是我需要在不声明 Word 对象的情况下使用该函数,因此它需要在 class 之外。我怎样才能让它发挥作用?
我还应该指出,我还有另一个使用 Word 作为参数的非成员函数,并且该函数适用于上述解决方案。虽然它有重载版本,但它不使用 Word 作为在 class 之前声明的参数,我认为这就是它起作用的原因。
你想要这个:
#include <string>
using namespace std;
// declare that the class exists
class Word;
// Declare the function
Word search_in_file(const string& searchee);
class Word {
private:
public:
//friend declaration so i could access members and use it in class - doesn't help
friend Word search_in_file(const string& searchee);
//function that uses previous function to create a Word object using data from file:
//type int to show it succeeded or failed
int fill(const string& searchee) {
Word transmission = search_in_file(searchee);
//here are member transactions for this->members=transmission.member;
}
};
// Now class Word is completely defined and you can implement the function
Word search_in_file(const string& searchee)
{
//...
}
我已经尝试了几个星期并搜索了几天的答案,但没有找到。我的代码比较大而且错综复杂,但我的问题是3functions/classes,所以我只展示我的声明和相关信息。我有以下不兼容的代码:
class Word{
private:
*members*
public:
//friend declaration so i could access members and use it in class - doesn't help
friend Word search_in_file(const string& searchee);
//function that uses previous function to create a Word object using data from file:
//type int to show it succeeded or failed
int fill(const string& searchee){
Word transmission = search_in_file(searchee);
//here are member transactions for this->members=transmission.member;
}
};
//function to return Word class from file:
Word search_in_file(const string& searchee){
//code for doing that
}
我已经尝试了所有可以声明函数或 class 的可能性,但没有找到解决方案。起初我只在构造函数中使用 search_in_file() 函数(现在与函数 fill() 有同样的问题)并在 [=22= 中声明并定义了 search_in_file() 函数].然后它像上面的代码一样工作(唯一的例外是友元函数也是具有定义的实际函数)。但是我需要在不声明 Word 对象的情况下使用该函数,因此它需要在 class 之外。我怎样才能让它发挥作用?
我还应该指出,我还有另一个使用 Word 作为参数的非成员函数,并且该函数适用于上述解决方案。虽然它有重载版本,但它不使用 Word 作为在 class 之前声明的参数,我认为这就是它起作用的原因。
你想要这个:
#include <string>
using namespace std;
// declare that the class exists
class Word;
// Declare the function
Word search_in_file(const string& searchee);
class Word {
private:
public:
//friend declaration so i could access members and use it in class - doesn't help
friend Word search_in_file(const string& searchee);
//function that uses previous function to create a Word object using data from file:
//type int to show it succeeded or failed
int fill(const string& searchee) {
Word transmission = search_in_file(searchee);
//here are member transactions for this->members=transmission.member;
}
};
// Now class Word is completely defined and you can implement the function
Word search_in_file(const string& searchee)
{
//...
}