lundi 25 février 2019

No viable overloaded '=' data.end() -1 = '\0'

I'm trying to create a program that filters through speech text, removes any unwanted characters (",", "?", etc., etc." and then produces a new speech where the words are jumbled based on what words follow or precede them. So for example, if you had the Gettysburg Address: "Four score and seven years ago our fathers brought forth on this continent, a new nation conceived in Liberty, and dedicated to the proposition that all men are created equal." My program would take that text, put it into a set of strings. i.e. ["Four","score","and","seven",...."continent,"..."Liberty,"..."equal."] Then it would remove any unwanted characters from each string using c++ .erase and c++ .remove, like "," or "." and capitals. After, you'd have a filtered string like ["four","score","and","seven",...."continent"..."liberty"..."equal."]

After that then the words would be rearranged into a new coherent, funnier speech, like: "Seven years ago our fathers conceived on men...", etc., etc.

That was just so you know the scope of this project. My trouble at the moment has to do with either using my iterator properly or null terminators

   #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <string>
    #include <set>
    #include <iterator>//iterates through sets
    #include <algorithm>

    using namespace std;

    int main(){
    set<string> speechSet;
    set <string>::iterator itr;//forgot what :: means. Declares iterator as    set
    int sum = 0;
    int x;
    string data;
    ofstream out;
    string setString;

    ifstream speechFile;//declare output file stream object. Unknown type name

    speechFile.open("./MySpeech");

    if(!speechFile){
        cerr<< "Unable to open file " << endl;
        exit(1);
    }

    char unwantedCharacters[] = ".";

    while(!speechFile.eof()){
        speechFile >> data;//speechFile input into data
        for (unsigned int i = 0; i < strlen(unwantedCharacters); ++i) {
            data.erase((remove(data.begin(), data.end(), 
    unwantedCharacters[i]), data.end()));//remove doesn't delete.    
            data.end()-1 = '\0';//Reorganizes
            cout << data << endl;
        }
        speechSet.insert(string(data));
    }


    //go through each string (word) one at a time and remove "",?, etc.
        /*for(itr = speechSet.begin(); itr != speechSet.end(); ++itr){
            if(*itr == ".")//if value pointed to by *itr is equal to '.'
                itr = speechSet.erase(itr);//erase the value in the set and  leave blank
                cout << " " << *itr;//print out the blank
            else{
                cout << " " << *itr;
            }
        }*/

        speechFile.close();
        return(0);

    }//Last line of program. All code must be written above.

I keep getting an error that says error: no viable overloaded '='. At first I thought it might be due to .end() not being a command for a c++ string, but I checked the documentation and it shouldn't be an issue of mismatched data typed. Then I thought it might have to set the iterator itr equal to the end of the data.

iterator itr = data.end() - 1;

and then dereference that pointer and set it equal to the null terminator

itr* = '\0';

That removed the overload error, but I still had another error "use of class template 'iterator' requires template arguments." Any help would be appreciated. Let me know if any more clarification is needed.

Aucun commentaire:

Enregistrer un commentaire