tags:

views:

99

answers:

2

Hi 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?

Any help on this is greatly appreciated. Thanks

+3  A: 

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.

Michael Burr
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.
Anthony McCormick
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.
dreamlax
A: 

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;
}
Alexander Rafferty