-7

Consider:

void func_print(int value) {
    std::cout << “int” << std::endl;
}
void func_print(void* value) {
    std::cout << “void” << std::endl;
}

int main() {
    func_print(0);
    func_print(NULL);
}

I can’t seem to find the reason. My compiler may be broken, because it gives me stray errors. I can’t find out what is wrong.

g++: error: -E or -x required when input is from standard input
c.cpp:3:5: error: stray ‘\342’ in program
     std::cout << “int” << std::endl;
     ^
c.cpp:3:5: error: stray ‘\200’ in program
c.cpp:3:5: error: stray ‘\234’ in program
c.cpp:3:5: error: stray ‘\342’ in program
c.cpp:3:5: error: stray ‘\200’ in program
c.cpp:3:5: error: stray ‘\235’ in program
c.cpp:6:5: error: stray ‘\342’ in program
     std::cout << “void” << std::endl;
     ^
c.cpp:6:5: error: stray ‘\200’ in program
c.cpp:6:5: error: stray ‘\234’ in program
c.cpp:6:5: error: stray ‘\342’ in program
c.cpp:6:5: error: stray ‘\200’ in program
c.cpp:6:5: error: stray ‘\235’ in program
c.cpp: In function ‘void func_print(int)’:
c.cpp:3:21: error: expected primary-expression before ‘int’
     std::cout << “int” << std::endl;
                     ^
c.cpp: In function ‘void func_print(void*)’:
c.cpp:6:21: error: expected primary-expression before ‘void’
     std::cout << “void” << std::endl;
                     ^
c.cpp: In function ‘int main()’:
c.cpp:11:20: error: call of overloaded ‘func_print(NULL)’ is ambiguous
     func_print(NULL);
                    ^
c.cpp:2:6: note: candidate: void func_print(int)
 void func_print(int value) {
      ^
c.cpp:5:6: note: candidate: void func_print(void*)
 void func_print(void* value) {

All errors here are explained with messages, though I don't get it exactly what is wrong.

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • 4
    Perhaps you should create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) and ask us about the errors instead? – Some programmer dude Aug 25 '18 at 15:06
  • Also note that `NULL` is commonly defined as the integer literal `0`, which means you make the same call twice. – Some programmer dude Aug 25 '18 at 15:06
  • 1
    When posting code, please include all the headers needed to make the code compilable. –  Aug 25 '18 at 15:06
  • This is the code i was given. It doesnt have any include or headers it should compile by its own (supposedly) . Also its literally all code so dont get minimal thing you posted – itsfreeandtakesminute Aug 25 '18 at 15:07
  • 1
    The code will *not* build without including needed header files. Perhaps that's the problem you have with your "broken" compiler? – Some programmer dude Aug 25 '18 at 15:08
  • 1
    "it should compile by its own " - no, it won't. I take it you haven't even bothered to try compiling it? –  Aug 25 '18 at 15:09
  • This is given from interview , so it is supposedly tested and compiled before. On standart GCC – itsfreeandtakesminute Aug 25 '18 at 15:09
  • @NeilButterworth I did it gives me stray errors. Didnt you read? – itsfreeandtakesminute Aug 25 '18 at 15:09
  • 1
    What are "stray errors"? What is the *exact* error message you get? – UnholySheep Aug 25 '18 at 15:10
  • c.cpp:6:5: error: stray ‘\342’ in program std::cout << “void” << std::endl; ^ c.cpp:6:5: error: stray ‘\200’ in program c.cpp:6:5: error: stray ‘\234’ in program it goes on – itsfreeandtakesminute Aug 25 '18 at 15:10
  • 8
    So if you look at the symbol it points to you see that it is a unicode `“` character, which is different from the ASCII double quotes `"` – UnholySheep Aug 25 '18 at 15:12
  • Still gives me error "g++: error: -E or -x required when input is from standard input c.cpp: In function ‘int main()’: c.cpp:11:20: error: call of overloaded ‘func_print(NULL)’ is ambiguous func_print(NULL); – itsfreeandtakesminute Aug 25 '18 at 15:16
  • 1
    Please [edit] the question with the latest state of your code. – E_net4 Aug 25 '18 at 15:17
  • Use one of the online compiler explorers for this type of question. Cleaned-up question live: https://godbolt.org/z/R9nJ7W – Richard Critten Aug 25 '18 at 15:26
  • 1
    This is one reason why `nullptr` and `nullptr_t` exist these days. – Jesper Juhl Aug 25 '18 at 15:38
  • 2
    Your compiler isn't broken. Your source file is. As @UnholySheep points out, it has a bunch of characters that the compiler doesn't recognize. For something as small as this, just retype it, using a programmer's editor. And, no, this snippet won't compile on its own. It needs a `#include` directive to see the declaration of `std::cout`. – Pete Becker Aug 25 '18 at 15:56
  • It is the wrong close reason. It is ***perfectly*** clear what the problem for those in the know. It should ***be closed as a duplicate***. This is a ***very*** common error when copying code from web pages, [PDF](https://en.wikipedia.org/wiki/Portable_Document_Format) documents, through chat (e.g. [Skype Chat](https://en.wikipedia.org/wiki/Features_of_Skype#Skype_chat) or [Facebook Messenger](https://en.wikipedia.org/wiki/Facebook_Messenger)), etc. The canonical question is *[Compilation error: stray ‘\302’ in program, etc.](https://stackoverflow.com/questions/19198332)*. – Peter Mortensen Apr 26 '23 at 16:17
  • It is a triplet of errors (one weird character gives rise to three error messages because it is UTF-8 byte sequence): ‘\342’ ‘\200’ ‘\234’. 342 200 234 (octal) → 0xE2 0x80 0x9C (hexadecimal) → UTF-8 sequence for Unicode code point U+201C ([LEFT DOUBLE QUOTATION MARK](https://www.charset.org/utf-8/9)). – Peter Mortensen Apr 26 '23 at 16:20
  • Of the common ones, there are only [LEFT DOUBLE QUOTATION MARK](https://www.charset.org/utf-8/9) (U+201C) and [RIGHT DOUBLE QUOTATION MARK](https://www.charset.org/utf-8/9) (U+201D) in this source file. They can be searched for (and replaced) with any modern text editor by the regular expression `\x{201C}|\x{201D}` (there are a total of 12 characters to be replaced in the source). – Peter Mortensen Apr 26 '23 at 16:30
  • Or there are really ***two completely different questions in one*** here: 1) What is the fix for the first compilation error? 2) What is the fix for the 'stray' character compilation error? There ought to be only one question. – Peter Mortensen Apr 26 '23 at 16:39
  • Note: The notation is different in Visual Studio Code (and probably others): `\u201C|\u201D` – Peter Mortensen Apr 27 '23 at 13:15

1 Answers1

1

Which functions get called when you send 0 as argument

The call is ambiguous. Neither function is preferred by the overload resolution, since 0 is both an int literal, as well as a pointer literal. An ambiguous call makes the program ill-formed, so a compiler is not required to accept it. This is what the compiler told you:

error: call of overloaded ‘func_print(NULL)’ is ambiguous

std::cout << “void” << std::endl;

This is wrong, because (Left Double Quotation Mark) is not a valid character there. You've most likely attempted to write a string literal. A string literal uses the " (Quotation Mark) character, which is similar. This is what the compiler told you:

error: expected primary-expression before ‘void’

It doesnt have any include or headers it should compile by its own (supposedly)

Your supposition is wrong. Besides the problems mentioned earlier, std::cout (or anything else from the std namespace) cannot be used without including standard headers.

eerorika
  • 232,697
  • 12
  • 197
  • 326