Print errors

Is possible to print error if a function isn't called after another function?
if B() always follows A() then make a wrapper C() that calls them in that order.
Then just use C()
Yeah, it can be right but it's not my case: I've have a static class called 'A':

class A
{
public:
static void init() { /*bla bla */ }
static void render() { /* bla bla */ }
};

And I before doing: A::render(), he has to call A::init().
ne555 if I do as you have recommended I have to call all the time the function init.
¿Why isn't `init()' a constructor?

rephrased: that the constructor of A() executes init() so you have a valid object from the start (perhaps init() would be no longer needed)
then the other methods would not be static, but refer to that A object.
Last edited on
Because it's a static class and I won't to create 1000 instance of A.
You can have a static boolean variable in the class that indicates whether the class has been inited. Then have render call init if it has never been called.
Ok but is possible to print errors if the variable is false and I want to call render function?

For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
static bool x = false;

void init()
{
     x = true;
}

void render()
{
     if(x)
     {
     }
     else
     {
           // print errors something like intellisense error
     }
}
> and I won't to create 1000 instance of A.
no, you would just create one
there is singleton too


> print errors something like intellisense error
I don't know at compile time, perhaps an static analyzer may do it.
At runtime you may put an assert.

But I think that this may be solved with design.


To print error messages use std::cerr << "message";
At runtime you may put an assert.

What is an assert?

To print error messages use std::cerr << "message";

And why i couldn't use a simple std::cout or printf?
http://en.cppreference.com/w/cpp/error/assert


cout and cerr refer to different streams, namely stdout and stderr.
you'll use the error stream to output error messages, that way you may separate them from the normal output of the program.
you may also redirect the standard output to another program (a pipe), and you may still see the error messages printed to the console.

Also, cerr is not buffered, if you use cout and the program terminates abnormally later, you may not see some messages because they were still in the buffer
Topic archived. No new replies allowed.