-
Linker error
Hi, it's been a while since I've done any C++. When i try to compile this code, I get a linker error. There are probably quite
a few bugs in the code. But this shouldn't cause a linker error, should it?
Code:
#include <stdlib.h>
#include <stdio.h>
#include <iostream.h>
char makegrid()
{
char grid[3][3] = {{'W','e','l'},
{'c','o','m'},
{'e','!','!'}};
return grid;
}
int showgrid()
{
for (int y=0;y<3;y++)
{
for (int x=0;x<3;x++)
{
cout<<grid[y][x]<<" ";
}
cout<<endl;
}
return 0;
}
int main()
{
cout<<"Hey!\n";
showgrid();
return 0;
}
This is the linker error I get:
C:\DEV-C_~1\Lib\\libmingw32.a(main.o)(.text+0x8e): undefined reference to `WinMain@16'
Any help would be appreciated.
-
>C:\DEV-C_~1\Lib\\libmingw32.a(main.o)(.text+0x8e): undefined reference to `WinMain@16'
You're trying to compile this as a Windows application. You want a console application. To change it you'll need to create a new project, this time specifying console.
-
first of all the comiler gives me two errors on that code
why not use this?:
Code:
int main() {
char grid[9];
int i = 0;
strcpy(grid,"Welcome!");
while (i < 7) {
cout << grid[i] <<" ";
i++;
}
return 0;
}
I couldn't indent in the borwser window FYI
-
Silly me, lol. Must of accidently clicked Windows application. Thanks!
-
Quote:
Originally posted by Dark Nemesis
first of all the comiler gives me two errors on that code
why not use this?:
Code:
int main() {
char grid[9];
int i = 0;
strcpy(grid,"Welcome!");
while (i < 7) {
cout << grid[i] <<" ";
i++;
}
return 0;
}
I couldn't indent in the borwser window FYI
The reason that he doesn't use what you just posted is obvious - it's completely different and won't do the same thing. What errors did you get compiling that? (I haven't tried compiling it, but it looks fine to me... except that makegrid() is returning a pointer to a local array which will be destroyed (in theory. It might work in practice, for that compiler, on that computer, with this program) You can't return a pointer to memory that your program doesn't own anymore and expect it to give the right results consistently. (grid goes out of scope when makegrid() ends) ) (I probably missed some ). Add them in) :cool:
edit: and grid isn't declared in makegrid()... hmmm... I'm really surprised Prelude didn't catch those problems :eek:
edit2: Okay, so it's full of problems :rolleyes: You never called makegrid() either...what are you expecting showgrid() to output? At this point, I don't honestly expect you to follow everything I've said and convert what you have there to work properly. I'd suggest...umm...starting over, keeping in mind that you can't return an array that doesn't exist anymore.
-
This program should work. You had a couple of problems with your first code, first of all you didnt call makegrid() and secondly showgrid doesnt have access to the array created in makegrid.
Code:
#include <cstdio>
#include <iostream>
using namespace std;
char grid[3][3] = {{'W','e','l'},
{'c','o','m'},
{'e','!','!'}};
int showgrid()
{
for (int y=0;y<3;y++)
{
for (int x=0;x<3;x++)
{
cout<<grid[y][x]<<" ";
}
cout<<endl;
}
return 0;
}
int main()
{
cout<<"Hey!\n";
showgrid();
return 0;
}