undefined refrence to WinMain@16 error?

Hello all,

Im new to c++, going through teaching myself, and Im encountering many problems
building functions. However, this problem in particular is stumping me.

my program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;

void fuction (void)
{

    int return_value = 0;

    cout << "ENTER A NUMBER: ";
    cin >> return_value ;
    cout << endl << return_value;

    cin.ignore(10, '\n');
    cin.get();
    return;

}


This is just an arbitrary program I made, just to see if I can successfully build a function. When I go to build it I get this error:
undefined refrence to WinMain@16
could someone explain to me why I am getting this error?
Thanks for your time!
(im using the Code::Block compiler if that helps)
http://www.cplusplus.com/doc/tutorial/program_structure/
The function named main is a special function in all C++ programs; it is the function called when the program is run. The execution of all C++ programs begins with the main function

you don't have a main() function.

I don't know about WinMain@16, perhaps you didn't chose `console project' at the beginning.
ne555 wrote:
I don't know about WinMain@16, perhaps you didn't chose `console project' at the beginning.

That's exactly the issue. Make sure you start every new C::B project as a "Console application" from the project wizard.
Duoas, even when I choose a Console application it returns the same erro. I don't know if i might have accidentally changed some setting in the compiler, or did something wrong when i first set it up, but when i run my other programs from the book im using they work fine.
C++ has 2 types of functions
 A value-returning function is designed to compute and return EXACTLY ONE value
using a return statement.
 A void function is a series of statements designed to perform a task. This type of
function does not have a specific data type.
What is needed to add a function to a C++ program?
function definition – consists of a heading and a body, placed after the main function
function prototype – placed after using statement and const declarations, before heading
for main function – provides information to compiler about the function
function call – placed inside any function, used when you want the statements in thefunction to be executed

1
2
3
4
5
6
7
8
9
10
11
//preprocessor directives
using namespace std;
//const declarations
//function prototypes
int main()
{
. . .
//function call(s)
return 0;
}
//function definitions (can include function calls) 



So for your code.
You need to include the function prototype.
The function call in the main function.
And the function definition ( the actual computations)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;

void function(int); // create a function prototype before your main function.
                           // the parameters are whatever data type you will send
                           // I changed it to an int
int main()
{
int return_value = 0; // create whatever variables you want to pass in main
function(return_value); // send "return _value" to your function

return 0;
}

void function(int n) // this is the function definition. include parameters & names
{
    cout << "ENTER A NUMBER: ";
    cin >> n ;
    cout << n << endl;

    cin.ignore(10, '\n');
    cin.get();

}


// Your variable names can change in the function. Just to demonstrate that is why I chose "n".
//

thanks so much for your reponse, this helps a lot. To get one thing straight, would "int n" in function represent the "return_value" you passed it from main?
Holy crap! Shows how much attention my brain was giving you.

Yes, every program must have a function named int main().

Since you are struggling with the very basics (you are greener than I thought, sorry!), it would definitely be worth your time to look through the tutorial, particularly:

http://www.cplusplus.com/doc/tutorial/program_structure/
http://www.cplusplus.com/doc/tutorial/functions/

would "int n" in function represent the "return_value" you passed it from main?
That depends entirely on how you mean that.

"function" does not return a value. Just naming a variable "return_value" does not make it so. (Just as you can't say "verb" is a verb -- it is still a noun.*)

Some commentary:

1
2
3
4
5
6
7
8
9
int main()
{
  int return_value = 0;
  // integer variable named "return_value" is assigned the value: zero

  function(return_value);
  // invoke function named "function", passing as argument the value of the variable "return_value", which is zero.
  // That is, this is the same as saying: function(0);
So we jump over to function():
14
15
16
17
18
19
20
21
22
23
24
25
26
27
void function(int n)  
// When invoked from main(), "n" is assigned the value zero (as explained above)
{
  cout << "ENTER A NUMBER: ";
  cin >> n;
  // Get integer input from user and assign its value to "n".
  // Note that this completely overwrites any previous value that "n" had (zero) with the new value.

  cout << n << endl;

  cin.ignore(10, '\n');
  cin.get();
}
//all done. returns nothing (void) to main(). 
Back to main():
10
11
12
  return 0;
  // All done. Program over.
}

It would be much simpler to just use a local variable in main():

1
2
3
4
5
6
7
8
9
10
11
int main()
{
  int n;
  cout << "ENTER A NUMBER: ";
  cin >> n;
  cout << n << endl;

  cout << "(press enter to quit)\n";
  cin.ignore(1000, '\n');
  cin.get();
}

Hope this helps.

* Unless you verb it, of course.
Topic archived. No new replies allowed.