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.

4
  • 4
    please post a minimal reproducible example and the compiler error message Commented Jun 8, 2021 at 9: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. Commented Jun 8, 2021 at 9:30
  • What does a Word represent, and why does it care about searching for strings in files? Commented Jun 8, 2021 at 9: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. Commented Jun 9, 2021 at 11:15

1 Answer 1

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)
{
  //...
}
Sign up to request clarification or add additional context in comments.

4 Comments

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 yes, it is: godbolt.org/z/9Weexq6W3
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 - 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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.