Why does my C++ function, only when it's placed after main(), not work?

Giorgos Tsolakidis

I recently picked up C++ and decided to try out making a function. However, I've run into a problem with my function func() where, even if declared beforehand, it only works if it's placed before the main() function.

If I place it after the main() function, the system tells me there is "no matching function for call to func".

Note: the functionfunc2 on the other hand works even if placed before or after the main() function.

So here's the code :

#include <stdio.h>
#include <iostream>

void func2();

int func();

int main()
{
  int y=2;

  std :: cout << "Hello World\n" << func(y) << "\n";
  func2();
  return 0;
}

int func(int x)
{
 x *= 2;
 return x;
}

void func2()
{
 std :: cout << "Hello there";
}
prapin

In C language, the declaration int func(); means a function with an unspecified number of arguments of any type, returning a int.

In C++ language, the same declaration int func(); means a function with an no arguments, returning a int.

And therefore, in C++, the definition of func with an argument of type int is an overload. For the compiler, it is a different function, which in the original code is not declared before use, so an error is emitted.

But in C, it would be perfectly legal.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

My DLL doesn't work properly when it's placed on System's directory

분류에서Dev

Why does my icon font not work in IE10 on Windows 8 only?

분류에서Dev

Why does saveInBackgroundWithBlock only work *once* in my Parse-enabled class?

분류에서Dev

Why does the following code only work with GridLayout?

분류에서Dev

Why does my bubble sort method not work?

분류에서Dev

Why my cron Job does not work?

분류에서Dev

Why does my xdotool key command not work?

분류에서Dev

Why does my 1440p HDMI monitor only display correctly when running from the bootable USB?

분류에서Dev

Why does my system show only 3.2 GiB of RAM when I definitely have 4.0 GiB

분류에서Dev

Why does my function print '0'?

분류에서Dev

Why does the wildcard * not work when changing directories?

분류에서Dev

Why does laptop's battery drain only when I am playing Minecraft?

분류에서Dev

Why for/while/do-while were placed in c/c++ when they perform same task

분류에서Dev

Why does this C code not work in JavaScript?

분류에서Dev

Why does this C code not work in JavaScript?

분류에서Dev

Why does strcat not work with empty string in C?

분류에서Dev

Why does sed only work on direct output from echo?

분류에서Dev

Why Does `'ln -s /directory 'D:'` Work Like It Does?

분류에서Dev

Why does my MFMailComposeViewController instance only dismiss one time?

분류에서Dev

Why doesn't my homemade isset function in javascript completely work?

분류에서Dev

Why does Tk().after only execute once and how to fix it?

분류에서Dev

Why does didUpdateLocations: only have one location after locationManagerDidResumeLocationUpdates:

분류에서Dev

Why is my js only working correctly when the page is refreshed?

분류에서Dev

Why does this python function not work if I wrap it in a def() call?

분류에서Dev

Why will this only work with break;?

분류에서Dev

Why does the printf function affect my speller program?

분류에서Dev

Why does my definition for function read a number as if it is negative?

분류에서Dev

Why does "and" work when specifying specific file extensions and not "or"

분류에서Dev

Why does powershell freeze for a bit when running my python scripts

Related 관련 기사

  1. 1

    My DLL doesn't work properly when it's placed on System's directory

  2. 2

    Why does my icon font not work in IE10 on Windows 8 only?

  3. 3

    Why does saveInBackgroundWithBlock only work *once* in my Parse-enabled class?

  4. 4

    Why does the following code only work with GridLayout?

  5. 5

    Why does my bubble sort method not work?

  6. 6

    Why my cron Job does not work?

  7. 7

    Why does my xdotool key command not work?

  8. 8

    Why does my 1440p HDMI monitor only display correctly when running from the bootable USB?

  9. 9

    Why does my system show only 3.2 GiB of RAM when I definitely have 4.0 GiB

  10. 10

    Why does my function print '0'?

  11. 11

    Why does the wildcard * not work when changing directories?

  12. 12

    Why does laptop's battery drain only when I am playing Minecraft?

  13. 13

    Why for/while/do-while were placed in c/c++ when they perform same task

  14. 14

    Why does this C code not work in JavaScript?

  15. 15

    Why does this C code not work in JavaScript?

  16. 16

    Why does strcat not work with empty string in C?

  17. 17

    Why does sed only work on direct output from echo?

  18. 18

    Why Does `'ln -s /directory 'D:'` Work Like It Does?

  19. 19

    Why does my MFMailComposeViewController instance only dismiss one time?

  20. 20

    Why doesn't my homemade isset function in javascript completely work?

  21. 21

    Why does Tk().after only execute once and how to fix it?

  22. 22

    Why does didUpdateLocations: only have one location after locationManagerDidResumeLocationUpdates:

  23. 23

    Why is my js only working correctly when the page is refreshed?

  24. 24

    Why does this python function not work if I wrap it in a def() call?

  25. 25

    Why will this only work with break;?

  26. 26

    Why does the printf function affect my speller program?

  27. 27

    Why does my definition for function read a number as if it is negative?

  28. 28

    Why does "and" work when specifying specific file extensions and not "or"

  29. 29

    Why does powershell freeze for a bit when running my python scripts

뜨겁다태그

보관