i want build 1 const with std::endl but by some reason isn't accepted:(

#define NewLine std::endl

i understand the '#define' isn't adviced to be used, but in these case i belive that i can't use the 'const':(
what isn't right with that line?

error message:
"C:\Users\Joaquim\Documents\CodeBlocks\My Class\console.h|170|note: void Console::write(A, B ...) [with A = const char*; B = {}]|"

the write function:

//write
    void write()
    {
        cout <<"";
        print();
    }

    template <typename A, typename ...B>
    void write(A argHead, B... argTail)
    {
        cout << argHead;
        write(argTail...);
    }

so why the error?

Why do you want to do this? I don't get the point??

Where and how are you using NewLine? If you replace your use of NewLine with std::endl, does the error disappear? If so, that would kind of surprise me to be honest. If not, your problem has nothing to do with the fact that you defined NewLine as a macro.

Also what you posted isn't an error message. It's a note that gives you additional information about the preceding error message. Please post the whole error message and any notes attached to it.

error messages:
"||=== My Class, Debug ===|
C:\Users\Joaquim\Documents\CodeBlocks\My Class\main.cpp||In function 'int main()':|
C:\Users\Joaquim\Documents\CodeBlocks\My Class\main.cpp|29|error: no matching function for call to 'console::write(const char [3], <unresolved overloaded function type>)'|
C:\Users\Joaquim\Documents\CodeBlocks\My Class\main.cpp|29|note: candidates are:|
C:\Users\Joaquim\Documents\CodeBlocks\My Class\console.h|163|note: void Console::write()|
C:\Users\Joaquim\Documents\CodeBlocks\My Class\console.h|163|note: candidate expects 0 arguments, 2 provided|
C:\Users\Joaquim\Documents\CodeBlocks\My Class\console.h|170|note: void Console::write(A, B ...) [with A = const char*; B = {}]|
C:\Users\Joaquim\Documents\CodeBlocks\My Class\console.h|170|note: candidate expects 1 argument, 2 provided|
||=== Build finished: 1 errors, 0 warnings (0 minutes, 2 seconds) ===|"

Okay and what about my other questions? What does line 29 in main.cpp look like? If you replace NewLine with std::endl, does that make a difference?

From the error message it looks like the problem might be because endl is an overloaded function, so the compiler does not know which overload (and thus which type) to pick. This would be unrelated to the fact that you defined a NewLine macro.

To fix this you could define NewLine as a function that explicitly takes an ostream as its argument. Or you could just use cout normally and not bother with any of this, which is really the preferable option if you ask me.

but in these case i belive that i can't use the 'const':(

That's right, you can't use a constant because std::endl is actually a free-function which gets implicitely converted to a function pointer, and then, within the stream's << operator, it is called on the stream. Anyways, long story short, std::endl is not a constant variable, so, to rename it, the easiest is probably a #define. The other option would be to use a wrapper function:

std::ostream& NewLine(std::ostream& out) {
  return std::endl(out);
};

which will allow you to write things like:

std::cout << "Hello World!" << NewLine;

But, if you ask me, this is all just a waste of time, just use std::endl and forget about it. Renaming standard things is generally just a bad idea, every single C++ programmer in the world knows exactly what std::endl is, you don't need to find a "more obvious" name for it, it's already clear enough to everyone concerned.

what isn't right with that line?

That line seems correct, as far as compilation goes, the define should work for your purposes. The two problems with that line is (1) that it is useless, as I already explained, and (2) that it breaks the MACRO convention that they should always be entirely written in upper-case letters to make it clear to all that it is a MACRO and not something else. That's all that is wrong with it, beyond the fact that MACROs are not to be abused, and this qualifies as abuse, in my opinion.

so why the error?

From the error messages you have posted, I imagine that you have tried to call the function with something like this:

write("Hi", NewLine);

And the error is telling you that it can't find a suitable overload for the write function. The reason why I know this is that the error message says <unresolved overloaded function type> as the second argument. This is because std::endl exists in two forms:

ostream& endl (ostream& os);

template <class charT, class traits>
basic_ostream<charT,traits>& endl (basic_ostream<charT,traits>& os);

In other words, when you provide std::endl to the function write, the compile has to figure out which one of those you actually mean (the normal one or the template one). But because your "write" function is a template, any of these two options would be fine, and thus the confusion leading to the compilation error.

Ironically, the way to fix this problem is to wrap the std::endl into something else (as I just told you not to). If you want to use a MACRO, you could do this:

#define NEWLINE ((std::ostream& (*)(std::ostream&))std::endl)

or, using the function I put above. Either way, it's gonna fix the problem by making the "unresolved overload" resolvable (in the MACRO case, it is an explicit cast, and in the function wrapper case, there is no alternative overload).

let me ask you 1 thing:
why the 'PRO's' want avoid the "\n"?
(seems, like you said, the std::endl, is only for '<<')

why the 'PRO's' want avoid the "\n"?

It's the opposite. You should avoid endl unless you know that the stream needs to be flushed afterward. Otherwise you should prefer '\n'. endl is implemented like this:

inline ostream& endl(ostream& out)
{
    out.put(out.widen('\n'));
    out.flush();
    return out;
}

There's always an implicit flush involved even if it's not needed.

thanks for that great information.
thanks
i have 2 questions out off topic:
1 - why they want const be uppercase?
2 - why some people adive me use '{' after function\class name and not in next line?

1 - why they want const be uppercase?

It's a naming convention that makes such constants stand out.

2 - why some people adive me use '{' after function\class name and not in next line?

It's just a programming style and makes no difference as long as you're consistent with it. Pick the one you like if you aren't already required to match company or project style guidelines.

  1. If by "const" you mean macros (as opposed to a variable that has been declared const), it's convention in C and C++ for macro names to be in ALL_CAPS to easily distinguish between macros and functions/variables.

  2. That's a matter of style and really up to you (or your team's style guidelines if you're a part of a larger team).

understood. thanks to all. thanks

2 - why some people adive me use '{' after function\class name and not in next line?

I used to put { on the same line as function/class name, but was often difficulty to match { and } braces, so I started putting { on the next line all by itself. Now with that and indentation matching braces are usually easy to spot.

thanks for all to all

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.