what is wrong with this cerr useage?

S

sandwich_eater

I get compiler error
"cerr undeclared first use this function."


#define err_ret(e) cerr << e; return -1

....

err_ret("who ate my muffy?");
 
A

Alf P. Steinbach

* (e-mail address removed):
#define err_ret(e) cerr << e; return -1

It's a macro, that's mainly what's wrong.

You have not shown your incorrect usage of the abomination.

But that hardly matters.
 
S

sandwich_eater

I did show it...
err_ret("who ate my muffy?");

macros are not always bad.
what alternative do you suggest?
 
M

Maxim Yegorushkin

I get compiler error
"cerr undeclared first use this function."


#define err_ret(e) cerr << e; return -1

...

err_ret("who ate my muffy?");

Wild guess: did you forget std:: before cerr?
 
S

sandwich_eater

I removed the macro, but I still get the same error with this code

cerr << "who ate my muffy?";

So the context of the macro or not is irrelevant, you completely missed
the point of my post.
 
S

sandwich_eater

yes, I just spotted this myself, it is a common error, so I detect a
hint of sarcasm in your post.
 
M

Mike Smith

I get compiler error
"cerr undeclared first use this function."


#define err_ret(e) cerr << e; return -1

...

err_ret("who ate my muffy?");

You need to have:

#include <iostream>
using std::cerr;

somewhere before the definition of your macro. However, I think the
real problem here is that you're using a macro when a function would do
just as well?

#define <iostream>
inline int err_ret(std::string e) /* or maybe char const *e */
{
std::cerr << e << std::endl;
return -1;
}
 
A

Alf P. Steinbach

* (e-mail address removed):
I removed the macro
Good.


, but I still get the same error with this code

cerr << "who ate my muffy?";

The compiler states that 'cerr' is undeclared.

It may be that you've forgotten to qualify the name, 'std::cerr', which is
most likely.

It may be that you've forgotten to include the relevant headers.

It may be that this is a spurious message due to earlier errors.

No way to tell when you don't post the code.

So the context of the macro or not is irrelevant,

Both the context of the macro definition and of the macro usage are
relevant.

And the problem with your output statement is somewhere in the context.

Which you haven't shown.

you completely missed the point of my post.

Nope.

Perhaps you've learned that macros are evil and context is important.
 

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

Forum statistics

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

Latest Threads

Top