Thread: why am i getting linker errors?

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    61

    why am i getting linker errors?

    The following is a class I built. Compiling the cpp file I get NO errors. But when I build the .h file and .cpp file, I always get these 2 linker errors and I'm not sure why. I followed the step by step process of creating a project workspace using the Microsoft visual C++ compiler and made sure both files were inserted into that workspace. So if anyone could tell me why I'm getting linker errors, I'd really appreciate it. Thanks so much.

    Code:
       
    #define MAX_SIZE 20
    
    
    class List
    {
    public:
      List(); //starts list
    
    
       //member functions
    int Insert(int);//inserts a number at the end(if there is room)
    int Insert(int, int);//if room and if valid position given, inserts value 
                        //in middle
    int Delete(int);//if given a position, deletes a value from list
    int Size();//returns list size
    int Sum(); //returns values' sum in list
    float Average();//returns values' in list
    int Evens();// returns how many even numbers in list after counting up
    int Greater(int);//returns how many numbers are greater than a //given parameter value after counting up
    int WhereIs(int);//gives position of first occurence of a value, if not  //found then 0 is returned
    int Smallest();//returns list's smallest value
    void Clear();//resets list object to represent an empty list
    void Show();//shows the contents of the list
    
    private:
    void CalculateSum(void);
    int arr[MAX_SIZE];//size can be at most 20 integers
    int sum;
    int size;
    
    
    };
    Code:
    
    //This file contains the declarations of the list header//
    
    
    #include <iostream>
    #include "list.h"
    
    using namespace std;
    
    //List member functions
    
    List::List()
    //gives user an empty list, 
    {
    
    Clear();//clears list for inserting
    
    }//end of constructor
    
    int List::Insert (int value)
    {
    
       if (size<MAX_SIZE) {    //determines if size is less than 20
    
       
       arr[size++]=value;
       CalculateSum();
       return 1;
    
    
                           }
       cout<<"*** Invalid List Position Number\n";
       return 0;
    }
    
    
    int List::Insert (int pos, int value)
    {
       
       if ((pos>0) && (pos < size) && (size<MAX_SIZE)) {
    
       
       for (int i=size;i>pos-1;i--) //loop
           arr[i]=arr[i-1];
    
       arr[pos-1]=value;
    
    
       size++;
       CalculateSum();
    
       return 1;
    
    
                                                       }
       cout<<"*** Invalid List Position Number\n";
       return 0;
    
    }
    
    
    int List::Delete (int pos)
    {
       
       if ((pos>0) && (pos < size)) {  //determines if position is greater  
      
                                                       //than 0 and less than size
       
       
         for (int i=pos-1;i<size;i++) //loop
               arr[i]=arr[i+1];
    
           
    
    
           size--;
           CalculateSum();
           
           return 1;
    
                                    }
       cout<<"*** Invalid List Position Number\n";
    
       return 0;
    }
    
    
    
    int List::Size (void) {    //returns size of list
       return size;
                          }
    
    float List::Average (void) {   //returns average of list
       return sum/size;
                               }
    
    int List::Evens (void) {
       int e=0;
    
       
       for (int i=0;i<size;i++) //loop
           if ((arr[i] % 2) == 0) //test for even
               e++;//increment evens
    
       return e;
                            }
    
    int List::Greater(int x) {
       int g=0;
    
       
       for (int i=0;i<size;i++) //loop
           if (arr[i]>x) //test for greater than
               g++;//increment greater
    
       return g;
                             }
    
    
    int List::Smallest (void) {
       int s=32767;
    
       
       for (int i=0;i<size;i++) //loop
           if (arr[i]<s) //test if arr[i] is smaller than s
               s=arr[i]; //set s to arr[i]
    
    
       return s;
                              }
    
    
    int List::WhereIs(int x) {
    
       for (int i=0;i<size;i++) //loop
         if (arr[i]==x)   //test if arr[i] is equal to x
             return i+1;
    
        return 0;
    
                             }
    
    
    
    void List::CalculateSum(void) {
    
       //zero out sum
       sum=0;
    
       //add all the numbers together
       for (int i=0;i<size;i++) 
           sum+=arr[i];
    
                                  }
    
    
    int List::Sum(void) {   //returning sum of list
       return sum;
                        }
    
    
       
    void List::Clear(void) {
       //clears the array element by element
       //set all to zero
       for (int i=0;i<MAX_SIZE;i++) 
           arr[i]=0;
    
       //reset the size
       size=0;
    
                           }
    
    void List::Show(void) {   //displaying contents of list
    
       cout << "Contents: ";
    
       for (int i=0;i<size;i++) 
           cout << arr[i] << ", ";
    
       cout << "\n";
    
    
    
                          }


    Thanks a lot guys.

  2. #2
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    I always get these 2 linker errors
    And those errors are.....?

    The only thing I can see offhand is that you don't protect the header file with the traditional
    Code:
    #ifndef _LIST
    #define _LIST
    
    class List {};
    
    #endif
    If you try and include the file twice, she'll get ........y at you. I'm not sure if there's a proper term for doing that.

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    61
    Oops sorry. I get this whenever I try to build the project:

    Code:
      Linking...
    LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
    Debug/list.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe.

    Why would I have to include the file twice? Thanks.

  4. #4
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    //This file contains the declarations of the list header//
    you mean implementations

    is that the whole project? is there a main function somewhere?
    Code:
    int main(int argc, char** argv) {
    
      // do stuff here  
    
      return 0;
    }

  5. #5
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    Why would I have to include the file twice?
    once instance of many: You have two classes that both need this list class, so their header files both have to include List.h (or whatever you called the file).

  6. #6
    Registered User
    Join Date
    Jul 2004
    Posts
    61
    lol yeah i meant definitions. I'm only using one class which is List.cpp which uses List.h. The first block of code I have is the header, the second is the definition.

    I do have a main to test it out, I just didn't think about putting it on here.

    but here it is.

    Code:
        #include <iostream>
    #include "list.h"
    
    using namespace std;
    
    int main()
    {
       int start, entry;
       List a;
       cout << "Welcome!\n";
       a.Show();
       cout << "How many numbers to start with (1 - 20)? ";
       cin >> start;
       cout << "Please input the " << start << " starting numbers\n> ";
       for (int i = 0; i < start; i++)
       {
    	cin >> entry;
    	a.Insert(entry);
       }
       cout << "Size of list = " << a.Size() << '\n';
       a.Show();
       cout << "Sum of items in list = " << a.Sum() << '\n';
       cout << "Average of list = " << a.Average() << '\n';
    
       if (a.Insert(0,100) == false)	cout << "* Insert 1 failed *\n";
       if (a.Insert(17,200) == false)	cout << "* Insert 2 failed *\n";
       if (a.Insert(300) == false)		cout << "* Insert 3 failed *\n";
       a.Show();
       if (a.Insert(3,400) == false)	cout << "* Insert 4 failed *\n";
       if (a.Insert(7,500) == false)	cout << "* Insert 5 failed *\n";
       a.Show();
       cout << "Size of list = " << a.Size() << '\n';
    
       if (a.Delete(0) == false)		cout << "* Delete 1 failed *\n";
       if (a.Delete(16) == false)		cout << "* Delete 2 failed *\n";
       a.Show();
       if (a.Delete(4) == false)		cout << "* Delete 3 failed *\n";
       a.Show();
       cout << "Size of list = " << a.Size() << '\n';
       cout << "Sum of items in list = " << a.Sum() << '\n';
       cout << "Average of list = " << a.Average() << '\n';
    
       cout << "There are " << a.Evens() << " even numbers in the list\n";   
    
       cout << "Number of values greater than 10 is: " << a.Greater(10) << '\n';
       cout << "Number of values greater than 25 is: " << a.Greater(25) << '\n';
       cout << "Number of values greater than 0 is: " << a.Greater(0) << '\n';
    
       cout << "The smallest number in the list is: " << a.Smallest() << '\n';
    
       int x = a.WhereIs(30); 
       if (x == 0)   cout << "30 does not appear in the list\n"; 
       else          cout << "30 first appears at position " << x << '\n'; 
    
       x = a.WhereIs(10); 
       if (x == 0)   cout << "10 does not appear in the list\n"; 
       else          cout << "10 first appears at position " << x << '\n'; 
    
       // Extra credit only.  Uncomment these lines to do the extra credit
    /*
       a.Rotate(-4);
       cout << "New list:\n";
       a.Show();
    
       a.Rotate(0);
       cout << "New list:\n";
       a.Show();
    
       a.Rotate(15);
       cout << "New list:\n";
       a.Show();
    */ 			
       // end extra credit
    
       a.Clear();
       cout << "Final list:\n";
       a.Show();
    
       return 0;
    }

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    If you have seperate files, you need to compile/link them together. Does this FAQ help:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    Jul 2004
    Posts
    61
    I went to the site Hammer and I did put both files in the same location. I still dont know why I'm getting linker errors. I've compiled classes before, I just am not very well associated with Microsoft visual. I'm more of a g++ kinda gal.

  9. #9
    Registered User
    Join Date
    Sep 2003
    Posts
    133
    I think you forgot to add the main.cpp file to your project. And also do remember to add #ifndef/#define statement to your list.h

  10. #10
    Registered User
    Join Date
    Jul 2004
    Posts
    61
    "And also do remember to add #ifndef/#define statement to your list.h" I've never done that before, whats the point of that? i've never had to do it and I've been working with classes for a while. I'll try adding the main and see what happens.

  11. #11
    Registered User
    Join Date
    Jul 2004
    Posts
    61
    Hmm, well it seems adding the Main program to the project did the trick. Thanks guys. I appreciate it.

  12. #12
    Registered User
    Join Date
    Sep 2003
    Posts
    133
    The #ifndef/#define statement is used to ensure that a header file is included only once in a program. A compiler will give out an error message if some statements are included more than once.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linker errors
    By jw232 in forum C++ Programming
    Replies: 3
    Last Post: 06-12-2009, 12:56 PM
  2. Linker errors in VC++ 2005
    By C+/- in forum C++ Programming
    Replies: 0
    Last Post: 05-18-2007, 07:42 AM
  3. Sneaky little linker errors...
    By Tozar in forum C++ Programming
    Replies: 8
    Last Post: 10-25-2006, 05:40 AM
  4. Linker errors with Visual C++
    By codegirl in forum C++ Programming
    Replies: 4
    Last Post: 09-11-2003, 09:20 AM
  5. MSVis-Studio C++ libraries and linker errors
    By kellydj in forum Windows Programming
    Replies: 10
    Last Post: 03-12-2002, 02:03 PM