1

I keep getting the following errors and can't seem to resolve them:

error LNK2019: unresolved external symbol "double __cdecl orderIn(double,double,double)" (?orderIn@@YANNNN@Z) referenced in function _main

fatal error LNK1120: 1 unresolved externals

I know there is something wrong with the way I am trying to pass the variable through the functions but I just can't get it. I want the information gathered and calculated in the first function to pass through and be utilized by the second function. I have tried numerous methods to no avail.

What am I missing here?

Thanks!

#include <iostream>
#include <iomanip>

using namespace std;


double orderIn(double, double, double);
void shippingOut(double, double, double);

double spoolsOrdered, 
    spoolsInStock,
    shipping,  
    total, 
    backordered, 
    charges,
    spoolsShipping;

int main()
{   

orderIn(spoolsOrdered, spoolsInStock, shipping);

shippingOut(spoolsShipping, backordered, total);


return 0;
}//end int main


double orderIn(double &spoolsOrdered, double &spoolsInStock, double &shipping)
{

char extracharge;

//spools ordered
cout << "How many spools would you like to order? ";
cin >> spoolsOrdered;
while (spoolsOrdered < 1)
    {
    cout << "That is not a valid entry ";
    cin >> spoolsOrdered;
    }

//spools in stock
cout << "How many spools are currently in stock? ";
cin >> spoolsInStock;

//extra charges
cout << "Are there any special charges on this order? ";
cin >> extracharge;

//special charges
if ( extracharge == 'Y' || extracharge == 'y')
    {
    cout << "What is the additional charge per spool? ";
    cin >> charges;
    shipping = (10 + charges);
    }
else 
    shipping = 10;
    
return (&spoolsOrdered, &spoolsInStock, shipping);
}

void shippingOut(double spoolsOrdered, double spoolnStock, double shipping)
{

double backordered;
double subTotal;
double totalShipping;
double total;
double spoolsShipping;



if (spoolsOrdered > spoolsInStock)
    {
    backordered=(spoolsOrdered - spoolsInStock);    
    cout << "There are " << spoolsInStock << " spools ready to be shipped./n";
    cout << "The remaining " << backordered <<" are on backorder.";
    spoolsShipping=spoolsInStock;
    }
else
    {
    cout << "All " <<spoolsOrdered << " spools ordered are ready to ship.\n";
    spoolsShipping=spoolsOrdered;
    }

    
    //Product Charges
    subTotal = spoolsShipping * 100;
    cout << "Subtotal: $" << subTotal << endl;
    
    //Shipping Charges
    totalShipping = spoolsOrdered * shipping;
    cout << "S/H Total: $" << totalShipping << endl;
        
    //Total
    total = subTotal + totalShipping;
    cout << "The total of the order ready to ship is: $" << total << endl;

}
Community
  • 1
  • 1
omadison
  • 47
  • 8

2 Answers2

3

You declare:

double orderIn(double, double, double);

and then use it. You later define:

double orderIn(double &spoolsOrdered, double &spoolsInStock, double &shipping)

This is a different function; the argument types are references to double, not simple double.

Fix either the declaration or the definition — it looks like you really need to fix the declaration since you want to set the variables in the calling function:

double orderIn(double &, double &, double &);

You should also review why you have so many global variables, and why the global variable names are shadowed by the parameters. Avoid globals whenever possible.


a) when I tried to run it in the past, I could only get it to work by defining the variables before main. Where should all of these variables be declared?

Normally, you'll declare variables in a function (in this case, main()), and then pass the variables to functions that need to use them. Sometimes, globals are appropriate. So, I expected:

int main()
{   
    double spoolsOrdered = 0.0;
    double spoolsInStock = 0.0;
    double shipping = 0.0;
    double total = 0.0;
    double backordered = 0.0;
    double spoolsShipping = 0.0;

    orderIn(spoolsOrdered, spoolsInStock, shipping);

    shippingOut(spoolsShipping, backordered, total);

    // Use these values?

    return 0;
}

If you don't have a use for the values in main(), why are you passing them around in the first place.

Then I noticed that both orderIn() and shippingOut() return a double, but you don't use the value. What does orderIn() return? There's a surprise:

return (&spoolsOrdered, &spoolsInStock, shipping);

This doesn't do what you think it does. The commas are the comma operator. The address of spoolsOrdered is evaluated and discarded; the address of spoolsInStock is evaluated and discarded; then the value in shipping is returned. You could change the function to return void and remove the return statement altogether, similar to shippingOut().

The global variable charges should be a local variable in orderIn().

b) what is the relationship between the variables and the parameters?

Inside the function orderIn(double &spoolsOrdered, double &spoolsInStock, double &shipping), the parameters each hide a global variable of the same name. Since this is C++, you can still access the global variable by using the scope operator :: like this:

::spoolsOrdered  // The global variable
spoolsOrdered    // The local reference variable -- a reference to the global

Largely by coincidence, it ends up being much the same in this case, but if you had value parameters or pointer parameters, or if the call did not pass the global variables as the reference parameters, the effects would be quite different.

If you use GCC (g++), the -Wshadow option reports shadowing problems.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • I'm in my first C++ class and feel like I have missed on some basics in the course. a) when I tried to run it in the past, I could only get it to work by defining the variables before main. Where should all of these variables be declare? b)what is the relationship between the variables and the parameters? – omadison Oct 07 '14 at 16:56
  • thank you so much for the tips. I revised the code as follows and now my only error is that in no context can I convert from double* to double. Ideas? I'm pretty sure my double & is now consistent. (should I update the above original entry? with the current code?) – omadison Oct 07 '14 at 17:31
  • Read the revision (addition) I just made to my answer. If you need to post more code, please add it rather than replace the original. But add as little as possible of it. Please learn how to create an MCVE ([Minimal, Complete, Verifiable Example](http://stackoverflow.com/help/mcve)) or SSCCE ([Short, Self-Contained, Correct Example](http://sscce.org/)) -- two names and links for the same basic idea. – Jonathan Leffler Oct 07 '14 at 17:42
  • thank you. I wasn't sure how to abbreviate what I posted. I appreciate you not only detailing what I was doing wrong, but also a better way to ask for help. – omadison Oct 07 '14 at 17:49
2

Your function prototype is:

double orderIn(double, double, double);

However your actual function definition is:

double orderIn(double &spoolsOrdered, double &spoolsInStock, double &shipping)
{

}

double and double& are different types, thus you need to either adjust the prototype or the definition.

dom0
  • 7,356
  • 3
  • 28
  • 50
  • I changed to prototype to double orderIn(double&, double&, double&); but it still gave me values of "$0" for the output of the second function. I think what I'm not getting is the basic syntax for outputting a variable that will be utilized by the following function. – omadison Oct 07 '14 at 16:42