-4

Header file

#ifndef WIKI_H
#define WIKI_H

template<class T>

class Rectangle
{

    private:

        T length;
        T breadth;

        void printarea();

        public:

            Rectangle();
};

source file

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

using namespace std;

void Rectangle::printarea()
{

    cout<< "Area = " << length*breadth <<endl; 
}

Rectangle::Rectangle()
{

    cout<< "Enter Length" <<endl;
    cin>> lenght;
    cout<< "Enter Breadth" <<endl;
    cin>> breadth;

    printarea();
} 

main file

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

using namespace std;

int main()
{

    Rectangle<int>X;

    return 0;
}
Utsav
  • 19
  • 1
  • 1
  • 1

1 Answers1

10

You need to add a #endif to the end of your header file.

Liam Potter
  • 1,732
  • 8
  • 24
  • 47
  • After I corrected mistake in my header and source file still the compiler show the error - undefined reference to `Rectangle::Rectangle()' – Utsav Jun 14 '18 at 11:08
  • 2
    @Utsav https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file – HolyBlackCat Jun 14 '18 at 11:43
  • For anyone arriving here who is thinking 'I have an #endif but this error is still showing', it can also be caused by circular include statements. – Burrough Clarke Aug 04 '20 at 00:27