使用C ++的代码块中的链接器错误问题

时间:2014-04-08 12:24:08

标签: c++

这是我第一次在这里发帖,所以有点温柔吗?另外,我正在上我的第一个编程课程,虽然我理解我们在课堂上所做的一切,但我在这里阅读的很多东西都在我脑海中。如果你可以请尽量保持你的答案尽可能基本我会很感激。

话虽如此,我正在使用mac上的代码块进行C ++编程。

这是我从代码块

获得的错误
Undefined symbols for architecture x86_64:
"totalBill(double, char, double&, double&, double&, double&)", referenced from:
  _main in Watkins-wk6-prog1.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Process terminated with status 1 (0 minute(s), 0 second(s))
0 error(s), 0 warning(s) (0 minute(s), 0 second(s))

我还会发布代码,这样你就可以看一下,看看是否有任何亮点。

#include <iostream>
#include <iomanip>
#include <cctype>

using namespace std;

//Global Constants

double const COM_PRICE = 22.91;
double const MID_PRICE = 25.76;
double const FULL_PRICE = 28.87;
double const SUV_PRICE = 98.88;
int const COM_MILES = 20;
int const MID_MILES = 25;
int const FULL_MILES = 30;
string const SUV_MILES = "Unlimited";
double const COM_OVER_MILES = 0.05;
double const MID_OVER_MILES = 0.07;
double const FULL_OVER_MILES = 0.09;
string const SUV_OVER_MILES = "N/A";
int const WEEK = 7;
double const WEEK_RATE = 6.5;
double const TAX = .115;
string const PROMPT1 = "Enter the number of days for the rental";
string const PROMPT2 = "Enter the number of miles driven";
string const PROMPT3 = "Enter the type of car to be rented (C,M,F,S)";


// function prototypes
char carSize(string prompt);
double inputs (string prompt);
double ChargeForCar(char carType, int numDays);
double carPrice (char carType);
double ChargeforMiles(double miles, char carType);
void totalBill(double miles, char carType, double& taxTotal, double& mileCharge, double&         
               grandTotal, double& carCharge);
string carSize (char carType);
void billOutput(string carTypeName, int numDays, double miles, double carCharge, double          
milageCharge, double taxTotal, double grandTotal);


int main()
{
  int numDays;    // number of days of rental
  int weeks   ;   // number of weeks in rental period
  int extraDays;  // number of extra days if car rented for more than a week
  double mileCharge = 0;
  double carCharge = 0;
  double taxTotal = 0;
  double grandTotal = 0;
  char carType;
  double miles;
  string carTypeName;

  cout << "Program to calculate the charge for a rental car." << endl << endl;

  carType = carSize(PROMPT3);   // calls function to get the type of car from
                              // the user
  numDays = static_cast<int> (inputs(PROMPT1));     // calls function to get number of days  in            
                                                    //the rental period
  miles = inputs(PROMPT2);       // calls function to get number of mile driven
                             // during rental period


      // calls function to calculate the total rental car bill
  totalBill(miles, carType, taxTotal, grandTotal, mileCharge, carCharge);

  carTypeName = carSize(carType);  // calls function to save car size as string

    //calls function to output the bill componenets
  billOutput(carTypeName, numDays, miles, carCharge, mileCharge, taxTotal, grandTotal);

  return 0;
}

char carSize(string prompt)  // function to present car choices and read in user input
{
  char carType;
  bool valid = false;


  cout << "Type of Car" << setw(16) << "Price per day" << setw(15);
  cout << "Included Milage" << setw(24) << "Extra charge per mile" << endl;
  cout << setw(66) << setfill ('-') << "-" << endl;
  cout << setfill(' ') << "C - Compact size" << setw(8) << COM_PRICE;
  cout << setw(5) << COM_MILES << setw(5) << COM_OVER_MILES << endl;
  cout << "M - Mid-Size" << setw(8) << MID_PRICE;
  cout << setw(5) << MID_MILES << setw(5) << MID_OVER_MILES << endl;
  cout << "F - Full size" << setw(8) << FULL_PRICE << setw(5) ;
  cout << FULL_MILES << setw(5) << FULL_OVER_MILES << endl;
  cout << " S - SUV" << setw(8) << SUV_PRICE << setw(5) ;
  cout << SUV_MILES << setw(5) << SUV_OVER_MILES << endl;

  do
  {
    cout << prompt ;
    cin >> carType;

    carType = toupper(carType);

    switch(carType)
    {
    case 'C':
    case 'F':
    case 'M':
    case 'S':
        valid = true;
        break;
    default:
        cout << "Invalid entry.  Please enter a letter from the list";
        break;
    }

  }
  while (valid == false);

  return carType;
}

double inputs(string prompt)  // general function to read a prompt and output user input
{
  double input;

  do
  {
    cout << prompt;
    cin >> input;

    if (input <= 0)
    {
        cout << "Invalid input." << endl;
        cout << "Please enter a positive, nonzero interger." << endl;
    }
  }
  while (input <= 0);

  return input;
}

double ChargeForCar(char carType, int numDays)  // function to calculate the charge for the  
                                                //rental period
{
  double carCharge;
  int weeks;
  int extraDays;
  double carRate;

  carRate = carPrice(carType);

  if (numDays >= WEEK)
  {
    weeks = numDays / WEEK;
    extraDays = numDays % WEEK;
    carCharge = (weeks * WEEK_RATE * carRate) + (extraDays * carRate);
  }
  else
  {
    carCharge = carRate * numDays;
  }

  return carCharge;
}

double carPrice (char carType)  // function to determine which price to use for rental car
{
  double carRate;

  switch (carType)
  {
  case 'C':
    carRate = COM_PRICE;
    break;
  case 'F':
    carRate = FULL_PRICE;
    break;
  case 'M':
    carRate = MID_PRICE;
    break;
  case 'S':
    carRate = SUV_PRICE;
    break;
  default:
    cout << "Unknown car type" << endl;
    break;
  }

  return carRate;
}

double ChargeForMiles(double miles, char carType)  // function to calculate the extra milage 
                                                   //cost
{
  double milesCost;
  double extraMiles;

  switch(carType)
  {
  case 'C':
    if (miles > COM_MILES)
    {
        extraMiles = miles - COM_MILES;
        milesCost = extraMiles * COM_OVER_MILES;

    }
    else
    {
        milesCost = 0;
    }
    break;
  case 'F':
    if (miles > FULL_MILES)
    {
    extraMiles = miles - FULL_MILES;
    milesCost = extraMiles * FULL_OVER_MILES;
    }
    else
    {
        milesCost = 0;
    }
    break;
  case 'M':
    if (miles > MID_MILES)
    {
        extraMiles = miles - MID_MILES;
        milesCost = extraMiles * MID_OVER_MILES;
    }
    else
    {
        milesCost = 0;
    }
    break;
  }

  return milesCost;
}

void totalBill(double miles, double numDays, char carType, double& taxTotal, double& mileCharge,   
                double& grandTotal, double& carCharge)  

// function to calculate the program totals
{
  switch(carType)
  {
    case 'C':
    case 'M':
    case 'F':
        mileCharge = ChargeForMiles(miles, carType);
        break;
    default:
        mileCharge = 0;
        break;
  }
  carCharge = ChargeForCar(carType, numDays);  // call to the function to calculate the charge            
                                               //for the rental car

  taxTotal = (mileCharge + carCharge) * TAX;
  grandTotal = mileCharge + carCharge + taxTotal;
}

string carSize(char carType) //function for setting car type name as a string type
{
  string typeName;

  if (carType == 'C')
  {
    typeName = "Compact size";
  }
  else if (carType == 'M')
  {
    typeName = "Mid size";
  }
  else if (carType == 'F')
  {
    typeName = "Full size";
  }
  else if (carType == 'S')
  {
    typeName = "SUV size";
  }

  return typeName;
}

void billOutput(string carTypeName, int numDays, double miles, double carCharge, double 
                milageCharge, double taxTotal, double grandTotal)
{
  cout << "Rent2U Rental Details" << endl << endl;
  cout << "Car Size: " << carTypeName << endl;
  cout << "Days Rented" << numDays << "days" << endl;
  cout << "Miles Driven" << miles << "miles" << endl << endl;

  cout << "BILL" << endl;
  cout << "Car Charge" << "$" << carCharge << endl;
  cout << "Mialge Charge" << "$" << milageCharge << endl;
  cout << "Tax" << "$" << taxTotal << endl;
  cout << setw(21) << " " << setw(11) << setfill('-') << "-" << endl;
  cout << setfill(' ') << "Total Bill" << "$" << grandTotal << endl << endl;
}

1 个答案:

答案 0 :(得分:3)

这是因为totalBill的实现与声明有不同的签名(即不同的参数)。一个需要两个双打,然后是一个炭,另一个,一个加倍然后一个炭。他们需要匹配。