0

I am trying to write a program that compresses text. For example, it would change aaaa to a^4.

For whatever reason, my compiler thinks that I am trying to overload a function when I am not. Here is my code (I apologize if it's long):

Code:

#include <iostream>
#include <string>

using namespace std;

string compress(string str);

int main()
{
    string input;
    string str = "gggxxx";
    cout << str;
    cout << compress(str);
    cin >> input;
}

string compress(string str)
{
    //Adds space to the end and front of a string
    str = " " + str + " ";
    string final = str;
    int repeatingcharnum = 0;
    char relavantchar = ' ';
    string replacestr = "";
    string placeholder = "";
    bool flag = false;
    for (int i = 1; i < str.length(); i++)
    {
        char lastchar = str[i - 1];
        char currentchar = str[i];
        if (lastchar == currentchar)
        {
            relavantchar = currentchar;
            repeatingcharnum++;
            flag = true;
        } else if (lastchar != currentchar && flag)
        {
            flag = false;
            //cout << repeatingcharnum << endl;
            //cout << "relavant:" <<relavantchar << endl;
            string replacestr = "";
            for (int x = 0; x < repeatingcharnum; x++)
            {
                replacestr += relavantchar;
            }
            placeholder = relavantchar + "^" + repeatingcharnum;
            final.replace(replacestr, placeholder);
            replacestr = "";
            placeholder = "";
            repeatingcharnum = 0;
            relavantchar = ' ';
        }
    }
    return final;
}

Output:

1>------ Build started: Project: Compression, Configuration: Debug Win32 ------
1>Compression.cpp
1>C:\Users\Admin\source\repos\Compression\Compression\Compression.cpp(27,23): warning C4018: '<': signed/unsigned mismatch
1>C:\Users\Admin\source\repos\Compression\Compression\Compression.cpp(47,50): error C2661: 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>::replace': no overloaded function takes 2 arguments
1>Done building project "Compression.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

I am programming in C++ using Visual Studio 2019 Community Edition.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 1
    The error is not saying you are trying to overload a function. It is saying that you are trying to *call* an overload that doesn't exist. Meaning you are calling a function that doesn't match the parameters you are passing in. Look at the available overloads of [`std::string::replace()`](https://en.cppreference.com/w/cpp/string/basic_string/replace), none of them match your code – Remy Lebeau Feb 28 '20 at 02:29
  • 2
    I count 15 overloaded versions of `std::string::replace`. None of them take two parameters. That's what your compiler is telling you. – Sam Varshavchik Feb 28 '20 at 02:29
  • https://en.cppreference.com/w/cpp/string/basic_string/replace All the `replace` functions here take 3 or 4 arguments. Pick one! – JohnFilleau Feb 28 '20 at 02:30

0 Answers0