Solved

no matching function for call to basic_string

Posted on 2004-09-29
12
441 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

863 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

20 Experts available now in Live!

Get 1:1 Help Now