1

I'm learning to use templates within C++ for defining generic functions. I'm having issues with getting a simple program to compile. I keep getting an error saying that the function call in main is an undefined reference.

I'm probably making a fundamental mistake somewhere in how I declare the template. Possibly with try to pass a const reference.

Please be gentle, I haven't coded C++ in a few months.

Thanks, -Izzo

main.cpp

#include <iostream>
#include <Probability.h>

using namespace std;

int main()
{

    int hoor[] = {3, 3, 1, 1};

    cout << "Hello world!" << endl;
    Probability prob;

    prob.ExpectedValueDataSet(hoor);

}

Probability.h

#ifndef COFFEEDEVMATH_PROBABILITY_H
#define COFFEEDEVMATH_PROBABILITY_H


class Probability
{
    public:
        Probability();
        template <typename T> void ExpectedValueDataSet(const T& data);

    protected:
    private:
};

#endif // COFFEEDEVMATH_PROBABILITY_H

Probability.cpp

#include "Probability.h"
#include <iostream>

Probability::Probability(void)
{
    //ctor
}

/* Expected Value */
template <typename T> void ExpectedValueDataSet(const T& data)
{
    bool error = 1;
    typename T::iterator it;
    double avg = 0;


    for(it = data.begin(); it != data.end(); it++)
    {
        avg = ((it - data.begin())*avg)/avg + data[it]/it;// Do nothing for now
        std::cout << avg;
        std::cout << "/n";
    }


}
Izzo
  • 4,461
  • 13
  • 45
  • 82

0 Answers0