Thread: Linker error?

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    7

    Linker error?

    Hey guys,

    So again, I'm trying to create one of the practice programs from my textbook without looking at the book's code. I want to test my skills...

    But failure has befallen me and I've given up for tonight. Hopefully one of you can figure this one out.

    So far, I have created three arrays: One for product numbers (int), one for the number of units sold (int), and another for sales (double). The directions say to display the list of products in the order of their sales from highest to lowest. The point of the exercise is to practice the selection sort algorithm.

    Now, I'm not 100% sure that I did the sort algorithm correctly, so I ask that you don't point out any errors in that function unless you think that it is the cause of my problem:

    Even with the dualSort function commented out (as well as the function call, of course), I cannot get the displaySales function to work. I get two linker errors:

    Error 1 error LNK2019: unresolved external symbol "void __cdecl displaySales(double * const,int)" (?displaySales@@YAXQANH@Z) referenced in function _main C:\Users\Rob\Rob's Text Files\School Work\Fall 2011\Programming\Labs\Practice\Practice.obj

    and

    Error 2 error LNK1120: 1 unresolved externals C:\Users\Rob\Rob's Text Files\School Work\Fall 2011\Programming\Labs\Practice\Debug\Practice.exe 1


    I've tried a whole bunch of things but I just don't know why I can't call this simple function. Perhaps I just need some sleep, but if anybody could give me a hand, I would very much appreciate it.

    Code:
    //When sorting the Units Sold, also sort the Product Number, using the same index number.
    //Remember to properly swap the places of values in BOTH arrays.
    
    #include <iostream>
    using namespace std;
    
    //Function prototypes
    void dualSort(int [], double [], int);    //highest to lowest
    int totalUnits(int[], int);        //calc total units
    double totalSales(int[], int);    //calc total sales
    void displaySales(double[], int);    //TEMPORARY for testing sort function!!!
    
    //Global constant
    int const NUM_PROD = 9;
    
    int main()
    {
        int prodNum[NUM_PROD] = {914,915,916,917,        //Product Numbers
                                918,919, 920, 921,922};
    
        int units[NUM_PROD] = {842,416,127,514,            //Number of units sold per month
                                437,269,97,492,212};
    
        double sales[NUM_PROD] = {12.95,14.95,18.95,    //total sales per month
                                    16.95,21.95,31.95,
                                        14.95,14.95,16.95};
    
    
        cout << "Displaying non-sorted sales:" << endl;
    
        //call display sales
        displaySales(sales, NUM_PROD);
    
        //call sort function
        dualSort(prodNum, sales, NUM_PROD);
    
        cout << "Displaying sorted sales from HIGHEST to LOWEST:" << endl;
        displaySales(sales, NUM_PROD);
    
    return 0;
    }
    
    //dualSort function
    
    void dualSort(int num[],double sales[],int size)
    {
        //Hold the max values of sales
        double maxSale;
        int maxSaleIndex, startScan;
    
        for (startScan = 0; startScan < (size - 1); startScan++)
        {
            maxSaleIndex = startScan;
    
            for (int index = (startScan + 1); index < size; index++)
            {
                if (sales[index] > sales[maxSaleIndex])
                {
                    maxSale = sales[index];
                    maxSaleIndex = index;
                }
            }
    
            sales[maxSaleIndex] = sales[startScan];
            sales[startScan] = maxSale;
        }
    }
    
    void displaySales(double sales[], double size)
    {
        for (int i = 0; i<size; i++)
            cout << sales[i] << "   " << endl;
    }
    There is just one other question that I have: Why do I need to use the NUM_PROD constant as an argument in my function calls? I thought that the whole point of a global variable/constant was that you could reference it in any function. :S

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Compare your prototype:
    Code:
    void displaySales(double[], int);
    with your definition:
    Code:
    void displaySales(double sales[], double size)
    Do they match exactly?

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    7
    *face-palm* Alright. Thank you, rags_to_riches. How on earth did I over-look that so many times? We'll never know.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    void dualSort(int [], double [], int);    //highest to lowest
    int totalUnits(int[], int);        //calc total units
    double totalSales(int[], int);    //calc total sales
    void displaySales(double[], int);    //TEMPORARY for testing sort function!!!
    Don't do this.
    SourceForge.net: Do not remove parameter names - cpwiki
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    7
    ^ Thank you for the tip. I have just been following the format presented in my textbook, but I found that (especially while writing this code) it was really confusing trying to make sure that I had all of the arguments of each function body arranged properly since two had a mix of integers and doubles.

    I'm really making an effort not to get into any bad habits. I want to learn how to code and I want to learn to do it properly.

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    7
    Quick question regarding that function prototype formatting:

    Do the variables that you use as parameters within the main function have to be the same (or different from) what you call them in the prototype?

    I guess not, right? The whole point of a function is to be able to repeat a procedure with a variety of different variables... yes?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by jester-race
    Do the variables that you use as parameters within the main function have to be the same (or different from) what you call them in the prototype?
    No, they do not have to have the same names.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linker error help
    By bonks in forum C Programming
    Replies: 14
    Last Post: 03-17-2011, 08:18 AM
  2. Linker error with VC++ 6.0
    By dave the coder in forum C++ Programming
    Replies: 7
    Last Post: 01-29-2008, 02:06 PM
  3. Linker Error
    By Bash in forum C++ Programming
    Replies: 13
    Last Post: 12-07-2007, 02:11 AM
  4. Linker error
    By Necrofear in forum Game Programming
    Replies: 3
    Last Post: 07-19-2005, 03:51 PM
  5. Need help with this linker error... please
    By radroach in forum C++ Programming
    Replies: 4
    Last Post: 06-24-2005, 09:11 AM