C ++错误"未定义引用"编译测试代码时

时间:2014-09-19 01:00:24

标签: c++ reference undefined undefined-reference

我一直在尝试编译我的代码并最终将其归结为一个错误。如果有人可以解释什么事情会很棒!

这是我的头文件

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

和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;
}

和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;
}

在命令行编译时我使用:     g ++ -std = c ++ 11 -g test.cpp rates.cpp -o test

这是错误

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

任何帮助或推动正确的方向都会很棒!谢谢!

2 个答案:

答案 0 :(得分:0)

在rates.cpp中,您创建的全局函数EnergyCostpower::EnergyCost不同。这意味着power::EnergyCost仍未实现。

您应该在EnergyCost命名空间内实现power,如下所示:

// rates.cpp

namespace power {

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

}

答案 1 :(得分:0)

我认为EngergyCost函数的实现应该包含在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;
    //:
    }
}