isklenar
isklenar

Reputation: 994

"No known conversion for implicit ‘this’ parameter" error

I have a class and two methods:

public:
bool Search ( const string & oName, const string & oAddr,
                    string & cName, string & cAddr ) const
    {
        Company ctmp;
        ctmp.own = oName + "|" + oAddr;
        int place = searchDupl(ctmp,currentsize,0);

        if (place < 0)
            return false;
        else
        {
            size_t pos = c[place].cmpn.find(' ');
            cName = c[place].cmpn.substr(0,pos);
            cAddr = c[place].cmpn.substr(pos+1);
            return true;
        }
    }

and in private, I have searchDupl:

int searchDupl(Company cmp,int imax,int imin)
    {
        if (imax < imin)
            return -1;
        else
        {
            int mid = imin + ((imax - imin)/2);

            if (c[mid].own > cmp.own)
            {
                return searchDupl(cmp,mid-1,imin);   
            }
            else if (c[mid].own < cmp.own)
            {
                return searchDupl(cmp,imax,mid+1);
            }
            else 
            {
                return mid;
            }
        }
    }

c is an dynamic array, that is defined in private section and initialized in constructor. currentsize is variable of type int (also in private and initialized in constructor) that defines the amount of elements in c.

When I try to compile it using g++, following error appears:

main.cpp: In member function ‘bool CCompanyIndex::Search(const string&, const string&, std::string&, std::string&) const’:
main.cpp:69:54: error: no matching function for call to ‘CCompanyIndex::searchDupl(Company&, const int&, int) const’
main.cpp:69:54: note: candidate is:
main.cpp:106:13: note: int CCompanyIndex::searchDupl(Company, int, int) <near match>
main.cpp:106:13: note:   no known conversion for implicit ‘this’ parameter from ‘const CCompanyIndex* const’ to ‘CCompanyIndex*’

I have no idea what is wrong.

Upvotes: 1

Views: 4686

Answers (1)

Daniel Frey
Daniel Frey

Reputation: 56863

Your first method is declared as a const-method

bool Search ( const string & oName,
              const string & oAddr,
              string & cName,
              string & cAddr ) const
//                             ^^^^^ here
{
  // ...
}

which means that the type of this is const CCompanyIndex* const. When you try to call the second method from within that method, that call is equivalent to this->searchDupl(ctmp,currentsize,0). This is why the type of this is imporant in your case. You require a mutable instance of CCompanyIndex, i.e., you need this to be of type CCompanyIndex* const because the second method is declared as

int searchDupl(Company cmp,int imax,int imin)
//                                           ^^^ no const here
{
  // ...
}

and therefore your code fails to compile. To fix it, you should declare the second method as const as well as the method is obviously not changing the class' state:

int searchDupl(Company cmp,int imax,int imin) const
{
  // ...
}

Upvotes: 6

Related Questions