Thread: Linker error

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    22

    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.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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.
    My best code is written with the delete key.

  3. #3
    Spaced Cadet
    Join Date
    Aug 2003
    Posts
    110
    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

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    22
    Silly me, lol. Must of accidently clicked Windows application. Thanks!

  5. #5
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    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)

    edit: and grid isn't declared in makegrid()... hmmm... I'm really surprised Prelude didn't catch those problems

    edit2: Okay, so it's full of problems 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.
    Last edited by confuted; 08-16-2003 at 08:58 PM.
    Away.

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    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;  
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. Crazy errors caused by class, never seen before..
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 11:54 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM