Thread: Please help with C++ templates - going through the tutorials

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    11

    Please help with C++ templates - going through the tutorials

    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;
    }
    Last edited by Vic Webster Jnr; 11-01-2012 at 02:32 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tutorials
    By polonyman in forum Windows Programming
    Replies: 6
    Last Post: 09-14-2004, 11:10 AM
  2. plz help - CP.com Tutorials
    By polonyman in forum C++ Programming
    Replies: 5
    Last Post: 09-09-2004, 03:33 PM
  3. SDL tutorials???
    By Ace Bowers in forum Game Programming
    Replies: 8
    Last Post: 07-19-2003, 10:16 AM
  4. Tutorials
    By Chr1s in forum C++ Programming
    Replies: 3
    Last Post: 02-17-2002, 12:10 PM
  5. Tell me tutorials
    By Unregistered in forum Game Programming
    Replies: 0
    Last Post: 01-09-2002, 03:33 AM