Thread: Unknown Errors in simple program

  1. #1
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640

    Unknown Errors in simple program

    I have to write a program that implements the selection sort algorithm using recursion. I still have yet to test out my code because I'm getting errors that I don't know how to fix.
    I'm using DevCpp (latest version) and I get a bunch of errors in the sort function. I copied the code to VS .NET and it compiled fine. For some reason VC .NET has linker errors with STL_ or some bull crap which is why I use DevCpp. Can anyone else compile this on devcpp? Or know why i'm getting errors?

    [Warning] In function `int sort(int*, int)':
    parse error before ` =' token
    parse error before ` )' token
    ..a bunch of errors about scope and variables not being declared even though they are. Anyone know wtf is going on?

    Code:
    #include <iostream>
    using namespace std;
    int sort(int [], int);
    const int MAX_ELEMENTS = 10;
    int main()
    {
        int unsorted[MAX_ELEMENTS] = {5,3,748,6,8,65,8,66,4,13};
        sort(unsorted, 0);
        cout << "{ ";
        for (int i=0; i<=MAX_ELEMENTS; i++)
            cout << unsorted[i] << " ";
        cout << "}" << endl;
    }
    
    int sort(int array[], int loc)
    {
        if (loc == MAX_ELEMENTS)
        {
            return 0;
        }
        int small = array[loc];
        int tmp = 0;
        int locsmall = 0;
        for (int i=loc; i<=MAX_ELEMENTS; i++)
        {
            if (array[i] < small)
            {
                small = array[i];
                locsmall = i;
            }
        }
        tmp = array[loc];
        array[loc] = small;
        array[locsmall] = tmp;
        sort(array, loc + 1);
    }
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    > for (int i=loc; i<=MAX_ELEMENTS; i++)
    Should just be i < MAX_ELEMENTS. You're hitting memory you don't own otherwise.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    That was an issue (I might have caught it if I were able to run the program). However, it is legal in c++ and doesn't cause any of the compilation errors I get. I changed it and still get the same problems.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    I don't know why it isn't compiling, maybe if you changed the name of sort to something less common.

    But while we're here, check out these two lines. There is an error on each that is causing your algorithm to not work properly:
    Code:
        int locsmall = 0;
        for (int i=loc; i<=MAX_ELEMENTS; i++)
    Also, since you are ignoring the return value, you could make it void. If not, then don't forget to return something at the end.

    [EDIT] - Oops, the second algoithm error was already pointed out! I'm sure you'll find the first one once the program runs.

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    It compiles fine for me using GCC3.3 and Gentoo Linux. I'd blame the compiler.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    There aren't any syntactic errors in your code, however, aside from the point, I would like to point out that when your functions say they return something, they really should return something (such as in int main() and int sort()).

  7. #7
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >int main()
    Not required.

    >int sort()
    Required (I think).
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  8. #8
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    I probably would've fixed all those problems (I recognize myself as someone who makes those mistakes but ends up cleaning up the code and fixing that stuff regularly) but I can't test the program because neither DevCpp or VS .NET are working. wtf man, not pie
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  9. #9
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    In that case, could you copy all of the error messages from Dev-Cpp into a post please?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  10. #10
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Sorry, took me a minute to find out how to get the output.

    Code:
    C:/Documents and Settings/christopher/Desktop/pa4a.cpp: In function `void 
       selsort(int*, int)':
    C:/Documents and Settings/christopher/Desktop/pa4a.cpp:24: parse error before `
       =' token
    C:/Documents and Settings/christopher/Desktop/pa4a.cpp:29: parse error before `
       )' token
    C:/Documents and Settings/christopher/Desktop/pa4a.cpp: At global scope:
    C:/Documents and Settings/christopher/Desktop/pa4a.cpp:35: ISO C++ forbids 
       declaration of `tmp' with no type
    C:/Documents and Settings/christopher/Desktop/pa4a.cpp:35: `array' was not 
       declared in this scope
    C:/Documents and Settings/christopher/Desktop/pa4a.cpp:35: `loc' was not 
       declared in this scope
    C:/Documents and Settings/christopher/Desktop/pa4a.cpp:36: `loc' was not 
       declared in this scope
    C:/Documents and Settings/christopher/Desktop/pa4a.cpp:36: ISO C++ forbids 
       declaration of `array' with no type
    C:/Documents and Settings/christopher/Desktop/pa4a.cpp:36: parse error before `
       ;' token
    C:/Documents and Settings/christopher/Desktop/pa4a.cpp:37: `locsmall' was not 
       declared in this scope
    
    C:/Documents and Settings/christopher/Desktop/pa4a.cpp:37: ISO C++ forbids 
       declaration of `array' with no type
    C:/Documents and Settings/christopher/Desktop/pa4a.cpp:37: assignment (not 
       initialization) in declaration
    C:/Documents and Settings/christopher/Desktop/pa4a.cpp:38: `array' was not 
       declared in this scope
    C:/Documents and Settings/christopher/Desktop/pa4a.cpp:38: `loc' was not 
       declared in this scope
    C:/Documents and Settings/christopher/Desktop/pa4a.cpp:38: ISO C++ forbids 
    
       declaration of `selsort' with no type
    C:/Documents and Settings/christopher/Desktop/pa4a.cpp:38: `int selsort' 
       redeclared as different kind of symbol
    
    C:/Documents and Settings/christopher/Desktop/pa4a.cpp:19: previous declaration 
       of `void selsort(int*, int)'
    C:/Documents and Settings/christopher/Desktop/pa4a.cpp:38: initializer list 
       being treated as compound expression
    C:/Documents and Settings/christopher/Desktop/pa4a.cpp:39: parse error before `
       }' token
    
    Execution terminated
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  11. #11
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Now can you post the code you're compiling, because you've obviously changed some things from the original code you posted.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  12. #12
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Not a whole lot changed, same errors as before, just corrected some of the things you guys saw.

    Code:
    #include <iostream>
    using namespace std;
    const int MAX_ELEMENTS = 10;
    void selsort(int [], int);
    int main()
    {
        int unsorted[MAX_ELEMENTS] = {5,3,748,6,8,65,8,66,4,13};
        selsort(unsorted, 0);
        cout << "{ ";
        for (int i=0; i<MAX_ELEMENTS; i++)
        {
            cout << unsorted[i] << " ";
        }
        cout << "}" << endl;
        return 0;
    }
    
    void selsort(int array[], int loc)
    {
        if (loc == MAX_ELEMENTS)
        {
            return;
        }
        int small = array[loc];
        int tmp = 0;
        int locsmall = 0;
        for (int i=loc; i<MAX_ELEMENTS; i++)
        {
            if (array[i] < small)
            {
                small = array[i];
                locsmall = i;
            }
        }
        tmp = array[loc];
        array[loc] = small;
        array[locsmall] = tmp;
        selsort(array, loc + 1);
    }
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  13. #13
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Try changing the 'small' variable to something else, and see what happens then.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  14. #14
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    And try changing "array" to a different name. If those don't work, try re-typing the quotes and braces, sometimes a strange quote character can throw everything off.

  15. #15
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Weird, that fixed everything. Thanks, now I just gotta get it to work right.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

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. Few Simple Program Errors
    By pobri19 in forum C Programming
    Replies: 3
    Last Post: 05-22-2008, 06:12 PM
  3. Builder C++ large files support for simple C program
    By Murfury in forum C Programming
    Replies: 3
    Last Post: 11-23-2007, 03:47 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Linker errors with simple static library
    By Dang in forum Windows Programming
    Replies: 5
    Last Post: 09-08-2001, 09:38 AM