string variables as arguments to constructors

I'm trying to accomplish something of this fashion:

1
2
3
4
5
6
7
8
9
10
// Constructor for class A

A::A(string filename)
{
    ifstream file(filename);

    // Do stuff

    file.close();
}


But I get a compiler error saying no matching call to ifstream(std::string&).

So, to me, it looks like passing string variables as string arguments does not work, and I'm unsure why. So, I would like to know why this is happening and what needs done to make passing string variables as arguments work.

Upon more digging, I discovered that ifstream takes const char * versus a string. Is there any way to convert a string, or should I change my constructor's argument type to const char*?
Last edited on
Try this:

1
2
3
4
5
6
7
8
9
10
// Constructor for class A

A::A(string filename)
{
    ifstream file(filename.c_str());

    // Do stuff

    file.close();
}
That worked wonderfully.

Thank you very much for the help =)
Topic archived. No new replies allowed.