Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

no matching function for call to basic_string

Posted on 2004-09-29
12
Medium Priority
?
449 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
LVL 17

Accepted Solution

by:
rstaveley earned 1400 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 600 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

926 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