1

I have a C++ method that takes one variable the method signature is like this:

DLL returnObject** getObject( const std::string folder = "" );

I tried passing in:

const std::string myString = "something";

but I get the following error:

No matching function call to ... getObject( std::string&);

I have a couple questions here.

  1. How do I pass in a normal std::string without the &
  2. This looks like the value is optional folder = "" is it? And if so how do you pass an optional parameter?
6
  • 4
    Can you please show us exactly how you tried to call the getObject function with the parameter? I'm betting your issue is there. Commented Sep 29, 2010 at 0:30
  • Hi i tried this const std::string folder = ""; className->getObject( folder ); Commented Sep 29, 2010 at 0:34
  • Perhaps extern "C" would help? Commented Sep 29, 2010 at 0:42
  • 1
    -1 "DLL returnObject**"? Doesn't look like normal (iPhone) C++ to me... Commented Sep 29, 2010 at 0:43
  • 1
    This cannot be the right code. For one, I somewhat doubt anyone who doesn't know to pass strings by reference would bother about adding const (although it is possible). What's more, your error message is referring to a function taking the string by non-const reference - which is a completely different one. So post the real code. Commented Sep 29, 2010 at 8:26

2 Answers 2

5

This little example works as expected:

#include <stdio.h>
#include <string>

class foo {
public:
    void getObject( const std::string folder = "" );
};

int main ()
{
    const std::string myString = "something";

    foo* pFoo = new foo;

    pFoo->getObject( myString);
    pFoo->getObject();    // call using default parameter

    return 0;
}


void foo::getObject( const std::string folder)
{
    printf( "folder is: \"%s\"\n", folder.c_str());
}

You might want to post a similarly small example that shows your problem.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks i was being stupid the method actually had multiple variables and i was just assuming that it was the std::string that was the cause of the issue.
Oh man, the last time I used C-style IO in a C++ answer I got snapped at (or was it a question... I can't remember). +1 anyway.
1

This compiled fine for me, compare it to what you're doing:

#include <string>

void myFunc(const std::string _str)
{
}

int main()
{
    const std::string str = "hello world";
    myFunc(str);
    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.