page 293 of C++ primer,4th edition

A

asdf

how to use c_str() ?

if I compile the following program, there are errors.

#include<iostream>
#include<fstream>

void main()
{
ofstream outfiloe(ofile.c_str());
}
 
T

Thomas Tutone

asdf said:
how to use c_str() ?

if I compile the following program, there are errors.

#include<iostream>
#include<fstream>

void main()

main returns int. Always.
{
ofstream outfiloe(ofile.c_str());

What is ofile? Where did you define it? Without the definition, this
is an error.

ofstream resides in the std namespace, so you need to qualify it with
"std::".

Best regards,

Tom
 
H

Howard

"Moonlit" <news moonlit xs4all nl> wrote in message

Please don't top-post. Place responses after (or interspersed with) what
you're responding to. [re-arragned]


It's a member of std::string. Look it up.

int main()

What's "ofile"?
add

using namespace std;

Ron, you didn't say where to add that. But better, in such a simple case,
would be to add the std:: specifiers where needed, or to add using
statements for the specific std:: items that will be used.

But, there's more here wrong than just that.

What's "ofile"? It's not defined. I'm guessing it's supposed to be a
std::string, given the use of c_str(), but I don't see it declared as such
anywhere. And the <string> header needs to be included if using strings.
Also, the string needs to get its content filled somehow, such as by using
std::cin.

If this was an attempt at reducing what you're posting, you cut out too
much. Read the FAQ, at http://www.parashift.com/c++-faq-lite/ (especially
section 5, reagarding how to post here for best results).

And if you're getting errors you don't understand, post the error message
(the text; error numbers mean nothing for people using other compilers), and
tell us which line of code generates the error.

Lastly, most of us probably don't have the book referred to in your subject
line. Make your subject lines more specific to the problem (such as "error
using c_str()" or "how do I use c_str()?"). In the body of your message,
you can refer to the book you're getting an example from (but don't expect
we'll all have that book).

-Howard
 
D

Default User

asdf said:
how to use c_str() ?

if I compile the following program, there are errors.

#include<iostream>
#include<fstream>

void main()
{
ofstream outfiloe(ofile.c_str());
}

In the future, post complete programs. Do that by copying the actual
code, not retyping. Also include the exact error messages.




Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top