#include "classfile.h"
#include "test1.h"
int main()
{
test t0;// test class define in header file "classfile.h"
t0.getstring();
t0.printstring();
test1 t1(10,20);// test1 class defined in header file "test1.h"
cout<<"The sum in 1st case is "<<t1.sum()<<endl;// using the parameterised constructor
int a,b;
test1 t2(5,5);//using the default constructor
cout<<"Sum using default parameters is "<<t2.sum()<<endl;
cout<<"Enter the numbers a and b ";
cin>>a>>b;
t2.getdata(a,b);// using function getdata() to input data
t2.sum();
cout<<"The sum in 2nd case is "<<t2.sum();
cin.clear();
cin.ignore(255,'\n');
cin.get();
return 0;
}
The test1.h header file interface file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
//class interface file. definiton in the test1
#ifndef class_h
#define class_h
class test1
{
public:
test1(int,int);
void getdata(int,int);
int sum();
private:
int a;
int b;
};
#endif
Definition of functions for the header file test1.h (test1.cpp)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
//definiton for the header file test1.h
#include "test1.h"
test1::test1(int x=0,int y=0)
{
a=x;
b=y;
}
void test1::getdata(int x,int y)
{
a=x;
b=y;
}
int test1::sum()
{
return a+b;
}
This is the definition of the header file classfile.h
//this header file has interface and definiton in the same file
#include<iostream>
using std::cout;
using std::cin;
using std::endl;
#include<string>
using std::string;
class test
{
string str;
public:
void getstring()
{
cout<<"Enter string ";
getline(cin,str);
}
void printstring() const
{
cout<<"\nThe string is "<<str;
}
};
I have put all the four files(mainfile.cpp,test1.h,test1.cpp and classfile.h) in the same directory and using bloodshed dev c++ ide. Im getting linker error for the class test1.h . The other class has no such problem and the code runs file when i don't use test1.h . What is the error im making?