0

I've tried couple of weeks and searched for days for an answer, but haven't found it. My code is rather large and intertwined, but my problem is with 3 functions/classes, therefore I will only show my declarations and relevant information. I have the following non-compliable code:

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
}

I've tried every possibility where I could declare the functions or class and haven't found a solution. At first I only used the search_in_file() function in the constructor(which now has the same problem as the function fill() ) and declared and defined the search_in_file() function in the class. Then it worked as above code (with the only exception being the friend function was the actual function with definition as well). But I need to use the function without having a Word object declared and therefore it needs to be outside of the class. How can I make it work?

I should also point out that I have another non-member function that uses Word as a parameter, and that function works with the above solution. Although it has on overloaded version, that doesn't use Word as parameter declared before the class and I think that is why it works.

Tony Stark
  • 434
  • 4
  • 13
  • 4
    please post a [mcve] and the compiler error message – 463035818_is_not_an_ai Jun 08 '21 at 09:16
  • 1
    With the information available, it seems `search_in_file` should just be a member function. filling information in "`this`" object and perhaps return a status. – nielsen Jun 08 '21 at 09:30
  • What does a `Word` represent, and why does it care about searching for strings in files? – molbdnilo Jun 08 '21 at 09:45
  • @nielsen At first it was a member function, but i needed it to declare Words in other non-member functions and while the idea you had would work, i think, it would take two rows each time i want to declare a word using it: 1) Declare empty word 2) fill it as you suggested. But i want it to work with one:` Word any = search_in_file(); ` Anyway i got the exactly the awnser i was looking for. – Mihkel Rüütli Jun 09 '21 at 11:15

1 Answers1

1

You want this:

#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)
{
  //...
}
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • It is not necessary to declare a function before declaring it as friend. I cannot see what this answer contributes with. OP needs to provide more info. – nielsen Jun 08 '21 at 09:31
  • @nielsen yes, it is: https://www.godbolt.org/z/9Weexq6W3 – Jabberwocky Jun 08 '21 at 09:38
  • For the friend declaration it is not necessary, but of course it is necessary to declare it before using it in the implementation of `fill()`. It may be better to take that implementation out of the class declaration to avoid the forward declaration of `Word`, but that is a matter of taste. In any case, OP still needs to provide more info. – nielsen Jun 08 '21 at 15:03
  • @nielsen - your idea might work as well, but I also have a constructor with the same problem and i'd prefer not taking that outside of the initial class definition, also i got it working the way i wanted! – Mihkel Rüütli Jun 09 '21 at 11:15