no matching function for call to basic_string

Hello,

I am receiving some errors when building my code.

In a particular member function, no matching function for call to setTestStr (const std::

src/ViewTrans.cpp:249: no matching function for call to `CellInfo::setTestStr(int, const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)'
include/CellInfo.h:25: candidates are: void CellInfo::setTestStr(int, std::string&)

What is the difference.  I am passing in a reference to a string.
jeweeAsked:
Who is Participating?
 
rstaveleyConnect With a Mentor Commented:
My guess is that you are passing a const string to a function which you have declared as taking a non-const strng reference.

The wrong thing to do (which is tempting) is to use a const_cast<> on the function call to remove the constness of the string.

The right thing to do, is to re-declare and reimplement...

   void CellInfo::setTestStr(int, std::string&)

...as...

   void CellInfo::setTestStr(int, const std::string&)
0
 
Jaime OlivaresSoftware ArchitectCommented:
Could you post your class declaration, and the code portion where you call setTestStr function?
0
 
Jaime OlivaresSoftware ArchitectCommented:
Also ensure to put this into your header (.h) files:

using namespace std;
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
lisa66Commented:
Can you provide a signature for your function and some sample of your code?

try to replace current signature with
void CellInfo::setTestStr(int, std::string&)
0
 
lisa66Commented:
I would not use
using namespace std;
blindly. It may create conflict in other parts of the code (iostream and iostream.h for example). Fully qualified names should do.
0
 
AxterCommented:
Make sure you have the string header included.

#include <string>
0
 
AxterCommented:
The <string> header should be included before the header that contains CellInfo class declaration.
Infact, I recommend you add the <string> include in the CellInfo class header.

I've seen similar errors with VC++ when the <string> is not explicitly included.
0
 
AxterCommented:
Also make sure that both the class declaration and the class member function implementation match.
0
 
AxterCommented:
The error you posted can also be caused if you try to put a class forward declaration for string, which is not allowed.
0
 
pankajtiwaryCommented:
Please post the code as its difficult to say anything just seeing the error. As far as the meaning of error is concerned, it says what it wants to say.
0
 
efnCommented:
I agree with rstaveley's analysis, but not necessarily his solution.

CellInfo::setTestStr may or may not need to change the string passed to it by reference.  The name of the function suggests it may need to change the string, but we don't have enough information at this end to determine that for sure.

If the function doesn't need to change the string, you should change the parameter declaration to const std::string&, as rstaveley suggested.

If the function does need to change the string, you shouldn't change the parameter declaration, but instead fix the calling code in ViewTrans.cpp.  If it is supposed to get a string set by the setTestStr function, it should pass that function a string that is not constant.
0
 
itsmeandnobodyelseConnect With a Mentor Commented:
>>>> The name of the function suggests it may need to change the string

No, a set function normally a class member and takes a const argument. All other things are weird. So, rstaveley has it right and it isn't a guess but for sure as the error messages do not allow another interpretation:

The caller uses a const string argument, e. g.

    void CellInfo::anyFunc(const string& name)
    {
          ...
          setTestStr(name);
    }

or a const char array, like that:

     pCellInfo->setTestStr("any text");

Both examples, would give the error message posted above.

Regards, Alex


0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.