Hello guys.
A couple of quick questions. First one, I'd rather not post using my real name, but if I login without Facebook, I can't start a thread. What's the secret?
Secondly, I'm a PHP and JavaScript programmer (experienced with OO), I'm just going through the nice C++ tutorials on this site and doing some experimentation.
I've got to the Templates tutorial:
Template Classes in C++ - Cprogramming.com
But now I'm stuck. I even copied and pasted the code snippets provided from that tutorial into my files, and the compiler still complains. The output I'm getting is this (it's a similar error if I just copy & paste the snippets, but below is "my own" version of the code).
I'm using Code::Blocks and the default compiler setup that comes with that IDE.
Code:
obj\Debug\main.o||In function `main':|
C:\stuff\vics\experiment\TemplateExperiment\TemplateExperiment\main.cpp|11|undefined reference to `Multiplier<int>::multiply(int, int)'|
||=== Build finished: 1 errors, 0 warnings ===|
Below are the three files I'm using:
main.cpp:
Code:
include <iostream>
#include "Multiplier.h"
using namespace std;
int main()
{
int num1 = 5;
int num2 = 10;
Multiplier <int> multiplier;
int result = multiplier.multiply( num1, num2 );
cout << result << "\n\n";
}
Multiplier.h
Code:
#ifndef MULTIPLIER_H
#define MULTIPLIER_H
template <class Type> class Multiplier
{
public:
Type multiply(Type firstNumber, Type secondNumber);
protected:
private:
};
#endif // MULTIPLIER_H
Multiplier.cpp
Code:
#include "Multiplier.h"
/*
Multiplier::Multiplier()
{
// Constructor
}
template <class Type> Type Multiplier<Type>::Multiplier()
{
// Constructor
}
*/
template <class Type> Type Multiplier<Type>::multiply( Type firstNumber, Type secondNumber)
{
return firstNumber * secondNumber;
}