1

A function has 2 arguments. One type is a vector of class(has a string private variable). The other is the string it looks for. I tried == both string but it won't work. I expected it but was hoping I could use a friend on it but it appears to work only for 2 classes.

I tried searching up using a friend function on class Term, but couldn't find results that used one class and one function. Besides friend, I can't think of another way.

class Term
{
    string str;
    unsigned long long int weight;
    Term(string s, long int w) : str(s), weight(w) {}
};
//my teacher provided this code so I can't change anything above

int FindFirstMatch(vector<Term> & records, string prefix)
//prefix is the word it looks for and returns the earliest time it appears.
{
    for (int i=0; i<records.size(); i++)
    {
        if (records[i].str==prefix)
        {
//I just need to get this part working
           return i;
        }
    }
}`

It says str is a private member of Term. Which is why I was hoping to simply use a friend on it.

JeJo
  • 30,635
  • 6
  • 49
  • 88
  • You can declare `FindFirstMatch` as a friend function, but you have to do it inside the class. If you can't modify the class, there's no way to do what you want. Are you sure you've posted the exact code as provided by your teacher, and that you can't modify it? – Brennan Vincent May 23 '19 at 03:52
  • 2
    `Term` is completely unusable. Nothing is public, so nothing can be accessed. You can't even create an object of the type. Friends have to be declared within the class. The declaration is either missing a `public:` as the first line, or needs to be a `struct` instead of a `class`. – 1201ProgramAlarm May 23 '19 at 03:53

1 Answers1

1

All the members of the Term class is under private custody and hence you can not even make an instance out of it. Your teacher definitely missed/ or want you to figure it out this.

Other than friending the member, you could provide a getter function by which you will able to access it.

class Term
{
private:
    std::string _str;
    unsigned long long int weight;

public:
    // constructor needs to be public in order to make an instance of the class
    Term(const std::string &s, long int w) : _str(s), weight(w) {}

    // provide a getter for member string
    const std::string& getString() const /* noexcept */ { return _str; }
};

int FindFirstMatch(const std::vector<Term>& records, const std::string &prefix)
{
    for (std::size_t i = 0; i < records.size(); i++)
    {
        if (records[i].getString() == prefix) // now you could access via getString()
        {
            return i;
        }
    }   
    return -1; // default return
}

Or if you are allowed to use standard algorithms, for instance using std::find_if and std::distance.

(See Live)

#include <iterator>
#include <algorithm>

int FindFirstMatch(const std::vector<Term>& records, const std::string &prefix)
{
    const auto iter = std::find_if(std::cbegin(records), std::cend(records), [&](const Term & term) { return term.getString() == prefix; });
    return iter != std::cend(records) ? std::distance(std::cbegin(records) , iter) : -1;
}
JeJo
  • 30,635
  • 6
  • 49
  • 88
  • 1
    Oh, thanks so much. Do you have any tips for getting into the right mindset? It only took you 10 minutes, and that includes writing, to figure it out while it took me a few hours and I still couldn't figure it out – Abbas Zaidi May 23 '19 at 04:19
  • @AbbasZaidi Definitely you could if you follow any of [good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) while learning. – JeJo May 23 '19 at 04:21
  • thanks for the link and for explaining how to accept it. I never used this site before, and turns out we can add stuff to the class so your example worked well – Abbas Zaidi May 24 '19 at 07:55