Using set containing strings, How do I pass in each string into function?

I am getting one error in this code where I have commented ERROR LINE.

I have a strings containing set called possibleList in the function called wordle and I am trying to pass in each and every string from that set into the wordle function (to recurse) but it gives me an error about rvalue / lvalue and I am not sure how to fix this / make this work.



#include <iostream>

#include <algorithm> 
#include <map>
#include <set>
// #include "wordle.h"
// #include "dict-eng.h"
using namespace std;

// MOST UP TO DATE


// Add prototypes of helper functions here


// Definition of primary wordle function
set<string> wordle(string& in, string& floating, set<string>& dict){
    set<string> possibleList;
    int length = in.length();
    

    // iterate over each letter
    
    for(int i = 0; i<length;i++){
        cout <<"line 1: "<<char(in[i])<<endl;
        
        // only if -
        
        if (in[i] == '-'){
            
            for(int j = 97; j<=122; j++){
                in[i]=char(j);
                possibleList.insert(in);
            }
            set<string>::iterator itr;
            for (itr = possibleList.begin(); itr != possibleList.end(); itr++)
            { 
                wordle(string(*itr), floating, dict);  // +++ERROR LINE+++  (LINE 40)
            }
        }
    }
    // if we reach here, that means that we now have all possibilities in the set
    return possibleList;
} // end of function
    
    
int main(){
    
    string in = "--pl-";
    string floating = "ae";
    set<string> dict;
    // set with 6 strings, should only return 2 of these
    dict.insert("joshua"); // same
    dict.insert("phone"); //diff
    dict.insert("apple"); //same
    dict.insert("aepll"); //same
    dict.insert("eapll"); //same
    dict.insert("ae"); // diff
    
    wordle(in, floating, dict);
    
    
    return 0;
    
    // how this works:
    // take all possible strings of the form of size n
    // then remove all requirements not met 
    
}
    

This is the error that I am getting.

To repeat from above,

I am getting one error in this code where I have commented ERROR LINE.

I have a strings containing set called possibleList in the function called wordle and I am trying to pass in each and every string from that set into the wordle function (to recurse) but it gives me an error about rvalue / lvalue and I am not sure how to fix this / make this work.

main.cpp: In function ‘std::set<std::__cxx11::basic_string<char> > wordle(std::string&, std::string&, std::set<std::__cxx11::basic_string<char> >&)’:
main.cpp:40:24: error: cannot bind non-const lvalue reference of type ‘std::string&’ {aka ‘std::__cxx11::basic_string&’} to an rvalue of type ‘std::string’ {aka ‘std::__cxx11::basic_string’}
   40 |                 wordle(string(*itr), floating, dict);
      |                        ^~~~~~~~~~~~

>Solution :

how to fix

auto S = *itr;  //copy of *iter
wordle(S, floating, dict);  //use S

Because non-const reference can not receive temporary object (as decribed in the error message).
For example:

//this is error
std::string &r = std::string( "xxx" );

//this is ok
const std::string &cr = std::string( "xxx" );
//this is ok
std::string &&rr = std::string( "xxx" );

Leave a Reply