-1

I've been trying to compile my code and finally got it down to just one error. If someone could explain whats going it would be great!

Here is my header file

rates.h

#ifndef RATES_H
#define RATES_H

#include <vector>

namespace power {


//! Energy usage over a period of time.
struct EnergyInterval
{
    //! Constant or average power used during the interval [W].
    const double power;

    //! Length of the interval [s].
     const double time;
};


//! The cost of using energy, within a specified bound.
struct Rate
{
    /*!
     * How much power this rate applies to, e.g., "the
     * first 650 kWh", "the next 400 kWh" [kWh]
     */
    const double amount;

    //! The price of energy in this block [cents/kWh]
    const double price;
};


/*!
 * Calculates the cost of using energy over a billing period,
 * broken down into time slices. Costs are calculated with the
 * commonly-employed "so much at this price, so much at that price"
 * model of rate schedules.
 *
 * @param[in]  usage         energy consumed in the billing period
 * @param[in]  rateSchedule  the schedule of energy costs
 *
 * @returns    total cost of energy used in the billing period [cents]
 */
double EnergyCost(const std::vector<EnergyInterval>& usage,
                  const std::vector<Rate>& rateSchedule);


} // namespace power

#endif

and rates.cpp

#include "rates.h"
#include <vector>
using namespace power;

double EnergyCost(const std::vector<EnergyInterval>& usage,
                  const std::vector<Rate>& rateSchedule){

    double energyUsage = 0;
    double cost = 0;
    const double uSize = usage.size();
    const double rsSize = rateSchedule.size();
    double totalAmount = 0;
    double secondTotal = 0;

    for(int i=0; i < uSize; i++){
        energyUsage += (usage[i].power*(usage[i].time/3600));
    }

    for(int j=0; j < rsSize; j++){
        totalAmount += rateSchedule[j].amount;
        if (energyUsage > totalAmount){
            cost =+ rateSchedule[j].amount*rateSchedule[j].price;
        }else if(energyUsage < totalAmount){
            cost =+ (energyUsage - secondTotal)*rateSchedule[j].price;
            break;
        }
        secondTotal += rateSchedule[j].amount;
    }

    return cost;
}

and test.cpp

#include "rates.h"
#include <vector>
#include <iostream>
using namespace std;
using namespace power;

int main()
{
   vector<EnergyInterval> usage =
   {
        {.power = 60, .time = 60*60},
        {.power = 2460, .time = 35*60},
        {.power = 60, .time = 60*60}
   };

   vector<Rate> rateSchedule =
   {
       {.amount = 2000, .price = 10},
       {.amount = 5000, .price = 5}
   }; 

   double totalCost = EnergyCost(usage, rateSchedule);

   cout<<"The total cost is: " << totalCost;

   return 0;
}

and when compiling in the command line I use: g++ -std=c++11 -g test.cpp rates.cpp -o test

This is the error

C:>g++ -std=c++11 -g test.cpp rates.cpp -o test
C:\cchqT8Ro.o: In function `main':
C:\test.cpp:22: undefined reference to `power::EnergyCost(std::vector<power::EnergyInterval,
std::allocator<power::EnergyInterval> > const&, std::vector<power::Rate, 
std::allocator<power::Rate> > const&)'
collect2.exe: error: ld returned 1 exit status

Any help or a push in the right direction would be great! Thanks!

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
HockeyChat
  • 11
  • 4
  • Sorry I brain farted and forgot to put the error message in! – HockeyChat Sep 19 '14 at 01:10
  • [What is an undefined reference/unresolved external symbol error and how do I fix it?: Function/variable declared in one namespace, defined in another.](http://stackoverflow.com/a/12574403/902497) – Raymond Chen Sep 19 '14 at 01:25

2 Answers2

0

In rates.cpp, you are creating a global function EnergyCost which is not the same as power::EnergyCost. This means that power::EnergyCost is still unimplemented.

You should implement EnergyCost inside the power namespace as follows:

// rates.cpp

namespace power {

double EnergyCost(const std::vector<EnergyInterval>& usage,
                  const std::vector<Rate>& rateSchedule){
   // ..
   // your code
   // ..
}

}
Marlon
  • 19,924
  • 12
  • 70
  • 101
0

I think EngergyCost function implementation should contained in the power namespace.

namespace Power {
    double EnergyCost(const std::vector<EnergyInterval>& usage,
    const std::vector<Rate>& rateSchedule){


    double energyUsage = 0;
    double cost = 0;
    const double uSize = usage.size();
    const double rsSize = rateSchedule.size();
    double totalAmount = 0;
    //:
    }
}
Alexander O'Mara
  • 58,688
  • 18
  • 163
  • 171
mika
  • 11
  • 1