Hey!
i've been trying to create a header file using dev C++ but i'm stuck with sum errors. Can anyone help me out in this?

#ifndef SUM_H
#define SUM_H
template <class T>
class add
{
    private: 
        const T a,b;
    public:
        T sum(T,T);
        void displaysum();  
};
template <class T>
T add::sum(T x,T y)
{
    a=x;b=y;
    return a+b;
}

template <class T>
void add::displaysum()
{
    cout<<endl<<"sum is "<<right<<a+b;
}

#endif

I get an error on compiling..

[Error] expected '=', ',', ';', 'asm' or '__attribute__' before '<' token

It looks okay aside from the class name not being fully qualified in your definitions. They should look like this (notice the template argument is included on the class name):

template <class T>
T add<T>::sum(T x,T y)
{
    a=x;b=y;
    return a+b;
}
template <class T>
void add<T>::displaysum()
{
    cout<<endl<<"sum is "<<right<<a+b;
}

ohkay! i made it correct now. but, still there is some linking problem.

#ifndef SUM_H
#define SUM_H
#include "iostream"
using namespace std;
template <class T>
class add
{
  private:
    const T a,b;
  public:
    T sum(T,T);
    void displaysum();
};
template <class T>
T add<T>::sum(T x,T y)
{
  a=x;b=y;
  return a+b;
}
template <class T>
void add<T>::displaysum()
{
  cout<<endl<<"sum is "<<right<<a+b;
}
#endif

i tried to make a program including sum.h

#include<iostream>
#include "sum.h"
using namespace std;
int main()
{
    sum(5,4);
    displaysum();
    cin.get();
}

here are the errors i get.

[Error] 'sum' was not declared in this scope 
[Error] 'displaysum' was not declared in this scope 

yeah i had a thought of it.. But that doesn't happen with other header files isn't it?? We directly use the functions?

But that doesn't happen with other header files isn't it??

It has nothing to do with header files, the rules of C++ don't change just because you break your program down into different files. In fact, headers are nothing more than a glorified cut and paste. When you say #include "sum.h", the entire contents of the sum.h header file are textually pasted over that directive. So this:

#include<iostream>
#include "sum.h"
using namespace std;
int main()
{
    sum(5,4);
    displaysum();
    cin.get();
}

Becomes this:

// The entire contents of iostream go here
// The entire contents of sum.h go here
using namespace std;
int main()
{
    sum(5,4);
    displaysum();
    cin.get();
}

We directly use the functions?

You can directly use a member function without qualification inside the class definition because this is implied. So this:

class Foo {
    void Bar() { ... }
    void Baz()
    {
        Bar();
    }
};

Is equivalent to this:

class Foo {
    void Bar() { ... }
    void Baz()
    {
        this->Bar();
    }
};

Outside of a class definition, no such implicit object exists and the compiler will assume that an unqualified function call refers to a non-member function.

oh.. so that's what actually happpens!! I was thinking that I'm creating a header.. foolish me!! :P
thanks! I'll checkout for this pointer!

I was thinking that I'm creating a header..

Well, you are creating a header. It's just that a "header" in C++ doesn't really mean very much. It's just a file with C++ code (usually declarations) that can be easily and conveniently reused with the #include preprocessor directive.

This article has been dead for over six months. Start a new discussion instead.