undefined reference

This is a discussion on undefined reference within the C++ Programming forums, part of the General Programming Boards category; Hey! When trying to compile the code for a ordered vector class I get the following error: [Linker error] undefined ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    4

    Post undefined reference

    Hey! When trying to compile the code for a ordered vector class I get the following error:
    [Linker error] undefined reference to `WinMain@16'

    Anyone have any idea what I might be doing wrong? This is driving me crazy and any help would be greatly appreciated.

    Code is as follows:

    Code:
    #include <stdlib.h>
    #include <iostream>
    #ifndef _ORDEREDVECTOR_H
    #define _ORDEREDVECTOR_H
    
    template <class Object>
    class orderedVector
    {
        public:
            orderedVector(int n);
            orderedVector( );
            orderedVector(const orderedVector & v);
            virtual ~orderedVector( );
            void dissolve ( );
            void add (const Object x);
            void remove (const Object x);
            void removeAt (int i);
            Object & operator [](int i);
            int length( ) const;
            bool isEmpty( ) const;
            bool contains(const Object x);
        private:
            Object * buffer;
            int size, capacity;
            void reserve(int newCapacity);
            void resize(int newCap) {reserve(newCap);}
        };
        
        template <class Object>
        orderedVector<Object>::orderedVector( ) :size(0),capacity(0),buffer(0){}
        
        template <class Object>
        orderedVector<Object>::orderedVector(int n): size(0), capacity(n), buffer(0) 
        {
            if (n < 0)
            {
                cerr << "Error: You can't create a vector with a negative capacity!\n";
                exit(1);
            }
        }
        
        template <class Object>
        orderedVector<Object>::orderedVector(const orderedVector & v) 
        {
            if (this == &v) 
            {
                cerr << "Error: You can't copy a vector onto itself!\n";
                exit(1);
            }
            buffer = 0;
            buffer = new Object[v.capacity];
            size = v.length( );
            capacity = v.capacity;
            
            for (int i = 0; i < v.length( ); i++) 
            {
                buffer[i] = v.buffer[i];
            }
        }
        
        template <class Object>
        orderedVector<Object>::~orderedVector( ) 
        {
            delete [ ] buffer;
        }
        
        template <class Object>
        void orderedVector<Object>::add(const Object x) 
        {
            if (size == capacity)
                    resize(capacity + 5);
            
            if (size == 0)
                    buffer[0] = x;
            else 
            {
                for (int i = 0; i < size; i++) 
                {
                    if (x <= buffer[i])
                    {
                        int j = size;
                        while (j > i) 
                        {
                            buffer[j] = buffer[j-1];
                            j--;
                        }
                        buffer[i] = x;
                    }
                }
            }
            size++;
        }
        
        template <class Object>
        void orderedVector<Object>::remove(const Object x) 
        {
            bool temp;
            for (i = 0; i < size; i++) 
            {
                if (buffer[i] == x)
                    temp = true;
                else
                    temp = false;
            }
            
            if (temp == false) 
            {
                cerr << "Error: Object doesn't exist!\n";
                exit(1);
            }
                
            else 
            {
                for (int i = 0; i < size; i++)
                {
                    int j = i;
                    while (j < size - 1) 
                    {
                        buffer[j] = buffer[j+1];
                        j++;
                    }
                }
                size--;    
            }
        }
        
        template <class Object>
        void orderedVector<Object>::removeAt(const int i) 
        {
            if (i < 0 || i >= size) 
            {
                 cerr << "Error: Index out of range!\n";
                 exit(1);
            }
            
            int j = i;
            while (j < size - 1)
            {
               	buffer[j] = buffer[j+1];
                j++;
            }
            size--;
       }
       
       template <class Object>
       Object & orderedVector<Object>::operator [] (int i)
       {
            if (i < 0 || i >= size) 
            {
                 cerr << "Error: Index out of range!\n";
                 exit(1);
            }
            return buffer[i];
       }
       
       template <class Object>
       int orderedVector<Object>::length( ) const 
       {
           return size;
       }
       
       template <class Object>
       bool orderedVector<Object>::isEmpty( ) const 
       {
           return size == 0;
       }
       
       template <class Object>
       bool orderedVector<Object>::contains(const Object x) 
       {
            bool temp;
            for (i = 0; i < size; i++) 
            {
                if (buffer[i] == x)
                    temp = true;
                else
                    temp = false;
                
            }
            return temp;
        }
        
        template <class Object>
        void orderedVector<Object>::reserve(int newCap)
        {
            if (buffer == 0)
            {
                size = 0;
                capacity = 0;
            }
            
            if (newCap <= capacity)
                    return;
            
            Object * newBuffer = new Object[newCap];
            
            for (int i = 0; i < size; i++)
                    newBuffer[i]=buffer[i];
                    
            capacity = newCap;
            delete [ ] buffer;
            buffer = newBuffer;    
        }
    #endif

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    125
    You're not really doing anything wrong. In fact, if you get a linker error your code's (probably) compiling fine, but a linker is meant to make a working executable file. Since this is just part of a program, you can't compile it any further than an object file. In other words, if you want to stop getting that error, either build a program around that class (using a main or WinMain function) or disable linking in your IDE. (assuming you're using an IDE)

    If you want to make this into a library for later use, you'll have to do some other stuff.
    Typing stuff in Code::Blocks 8.02, compiling stuff with MinGW 3.4.5.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    5,871
    You're using a compiler that targets windows, and is configured to create a windows application (for which the starting function is named something like winmain() rather than main()).

    Look into configuration options for your compiler, and look for a setting concerned with creating console applications. A console application is essentially one that starts with main() rather than winmain().

    That is a generic description. If youy identify your compiler (and version) someone may be able to give you more specific hints to fix your problem.

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    4

    Post

    I'm using Bloodshed Dev-C++, version 4.9.9.0

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Then use:
    Execute -> Compile

    Or the compile button which is the four little boxes icon.

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    4
    I am running the compile, thats when the error shows up under messges.

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Tools -> Compiler Options
    Check the box that says "Add the following commands when calling compiler:"
    then inside the big box put
    -c

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21