Error: Undefined Reference to WinMain@16

Mar 8, 2012 at 10:51am
Hi!

I'm writing a program in C++ to ask the user for a list of ten names and cities of residence in the format <firstname> <Surname> <city>.
I am using isspace to sort out the number of spaces, but then i have to sort the list by city and then in each city group alphabetically by name.
I have written numerous codes that have all failed me! This is the latest code I have written. There is one error message "undefined reference to 'WinMain@16'"
I have read in a number of places that including int main() solves this issue, however each time I do this, that error is replaced by about 20 others!! :/
If anyone has any ideas on how I might actually get this code working I would be most thankful!!:)
This is the code:

#include <cstring>
#include <algorithm>

const int STRING_BUFFER_SIZE=80;//increase this to allow for really long names
struct Individual {
char firstName[STRING_BUFFER_SIZE];
char lastName[STRING_BUFFER_SIZE];
char cityName[STRING_BUFFER_SIZE];
};
bool isGreater(const Individual& a, const Individual& b) {
int result;
return ((result=std::strcmp(a.cityName, b.cityName))?(result>0):((result=std::strcmp(a.lastName,b.lastName))?(result>0):(std::strcmp(a.firstName, b.firstName)>0)));
};
void bubbleSort(Individual* a, int aLength) {
for(bool swapped=true; (swapped && (!(swapped=false))); --aLength)
for(int i=1;i<aLength;++i)
if((isGreater(a[i-1],a[i])) && (swapped=true)) std::swap(a[i-1],a[i]);
};
Mar 8, 2012 at 10:55am
where's your
int main()
function?
Mar 8, 2012 at 10:58am
I left out the int main () function, because when I put that in, I loads more errors!!! :( Do you still think I should put it in??
Mar 8, 2012 at 11:03am
Yes, perhaps that is what is generating the error.
Try putting int main() and if that doesn't work, int main(int argc, char* argv[]) and repost the errors
Mar 8, 2012 at 11:15am
Ok, thank u so much for this help! :)

When I include int main () my code looks like:


#include <cstring>
#include <algorithm>
int main(){
const int STRING_BUFFER_SIZE=80;//increase this to allow for really long names
struct Individual {
char firstName[STRING_BUFFER_SIZE];
char lastName[STRING_BUFFER_SIZE];
char cityName[STRING_BUFFER_SIZE];
};
bool isGreater(const Individual& a, const Individual& b) {
int result;
return ((result=std::strcmp(a.cityName, b.cityName))?(result>0):((result=std::strcmp(a.lastName,b.lastName))?(result>0):(std::strcmp(a.firstName, b.firstName)>0)));
};
void bubbleSort(Individual* a, int aLength) {
for(bool swapped=true; (swapped && (!(swapped=false))); --aLength)
for(int i=1;i<aLength;++i)
if((isGreater(a[i-1],a[i])) && (swapped=true)) std::swap(a[i-1],a[i]);
};
}

I am told there are 2 errors:
line 10: error: a function-definition is not allowed here before '{' token
line 14: error: a function-definition is not allowed here before '{' token

_____________________________________________________________________________

When I include int main(int argc, char* argv[]) my code looks like:

#include <cstring>
#include <algorithm>
int main(int argc, char* argv[]) {
const int STRING_BUFFER_SIZE=80;//increase this to allow for really long names
struct Individual {
char firstName[STRING_BUFFER_SIZE];
char lastName[STRING_BUFFER_SIZE];
char cityName[STRING_BUFFER_SIZE];
};
bool isGreater(const Individual& a, const Individual& b) {
int result;
return ((result=std::strcmp(a.cityName, b.cityName))?(result>0):((result=std::strcmp(a.lastName,b.lastName))?(result>0):(std::strcmp(a.firstName, b.firstName)>0)));
};
void bubbleSort(Individual* a, int aLength) {
for(bool swapped=true; (swapped && (!(swapped=false))); --aLength)
for(int i=1;i<aLength;++i)
if((isGreater(a[i-1],a[i])) && (swapped=true)) std::swap(a[i-1],a[i]);
};
}


I am told there are the exact same 2 errors as just using int main ():
line 10: error: a function-definition is not allowed here before '{' token
line 14: error: a function-definition is not allowed here before '{' token

Any ideas??? :)
Mar 8, 2012 at 11:19am
You can't define one function inside another. Try this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <cstring>
#include <algorithm>

const int STRING_BUFFER_SIZE=80;//increase this to allow for really long names
struct Individual {
char firstName[STRING_BUFFER_SIZE];
char lastName[STRING_BUFFER_SIZE];
char cityName[STRING_BUFFER_SIZE];
};
bool isGreater(const Individual& a, const Individual& b) {
int result;
return ((result=std::strcmp(a.cityName, b.cityName))?(result>0):((result=std::strcmp(a.lastName,b.lastName))?(result>0):(std::strcmp(a.firstName, b.firstName)>0)));
};
void bubbleSort(Individual* a, int aLength) {
for(bool swapped=true; (swapped && (!(swapped=false))); --aLength)
for(int i=1;i<aLength;++i)
if((isGreater(a[i-1],a[i])) && (swapped=true)) std::swap(a[i-1],a[i]);
};


int main(int argc, char* argv[]) {
//Now do whatever you want
}


And by the way, your code is real messy, if you are on codeblocks, I will suggest doing
Plugin->Source Code Formatter

EDIT:
Remove all ';' after FUNCTION definition
Last edited on Mar 8, 2012 at 11:23am
Mar 8, 2012 at 11:21am
After formatting it looked like

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
27
28
29
30

#include <cstring>
#include <algorithm>

const int STRING_BUFFER_SIZE=80;//increase this to allow for really long names
struct Individual
{
    char firstName[STRING_BUFFER_SIZE];
    char lastName[STRING_BUFFER_SIZE];
    char cityName[STRING_BUFFER_SIZE];
};
bool isGreater(const Individual& a, const Individual& b)
{
    int result;
    return ((result=std::strcmp(a.cityName, b.cityName))?(result>0):((result=std::strcmp(a.lastName,b.lastName))?(result>0):(std::strcmp(a.firstName, b.firstName)>0)));
};
void bubbleSort(Individual* a, int aLength)
{
    for(bool swapped=true; (swapped && (!(swapped=false))); --aLength)
        for(int i=1; i<aLength; ++i)
            if((isGreater(a[i-1],a[i])) && (swapped=true)) std::swap(a[i-1],a[i]);
};
}

int main(int argc, char* argv[])
{
//Now do whatever you want
}

Mar 8, 2012 at 11:24am
Ok, thank u very much for all your help and your advice!!! :) :D
Mar 8, 2012 at 1:05pm
Any ideas on how I can use this code to write the program I need to write??
Topic archived. No new replies allowed.