Solved

no matching function for call to basic_string

Posted on 2004-09-29
12
440 Views
Last Modified: 2012-08-14
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.
0
Comment
Question by:jewee
  • 4
  • 2
  • 2
  • +4
12 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12182280
Could you post your class declaration, and the code portion where you call setTestStr function?
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12182290
Also ensure to put this into your header (.h) files:

using namespace std;
0
 
LVL 1

Expert Comment

by:lisa66
ID: 12182326
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
 
LVL 1

Expert Comment

by:lisa66
ID: 12182359
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
 
LVL 30

Expert Comment

by:Axter
ID: 12182435
Make sure you have the string header included.

#include <string>
0
 
LVL 30

Expert Comment

by:Axter
ID: 12182478
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 17

Accepted Solution

by:
rstaveley earned 350 total points
ID: 12182537
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
 
LVL 30

Expert Comment

by:Axter
ID: 12182603
Also make sure that both the class declaration and the class member function implementation match.
0
 
LVL 30

Expert Comment

by:Axter
ID: 12182633
The error you posted can also be caused if you try to put a class forward declaration for string, which is not allowed.
0
 
LVL 4

Expert Comment

by:pankajtiwary
ID: 12184583
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
 
LVL 15

Expert Comment

by:efn
ID: 12186371
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
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 150 total points
ID: 12192579
>>>> 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

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now