Problem with class

So here is all files..
1
2
3
4
5
6
7
8
9
10
11
12
//polygon.h
#ifndef POLYGON_H
#define POLYGON_H
class polygon{
 
 protected:
    float x,y;
 public: polygon();
        ~polygon();
        void setData(float,float);
};
#endif 


1
2
3
4
5
6
7
8
9
10
11
//triangle.h
#ifndef TRIANGLE_H
#define TRIANGLE_H

class triangle{
          
public:  triangle();
         ~triangle();  
         float areaTR();
};
#endif 


1
2
3
4
5
6
7
8
9
10
11
//rectangle.h
#ifndef RECTANGLE_H
#define RECTANGLE_H

class rectangle{
    public:      
    rectangle();
    ~rectangle();    
     float areaRE();	 
};
#endif 


1
2
3
4
5
6
7
//polygon.cpp
#include "polygon.h"

void polygon::setData(float a,float b)
{
a=x;b=y;
}


1
2
3
4
5
6
//triangle.cpp
#include "triangle.h"

public: polygon{     
float triangle::areaTR(){return x*y/2;}
} 


1
2
3
4
5
6
//rectangle.cpp
#include "rectangle.h"

public: polygon{
    float rectangle::areaRE(){return x*y;}
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//500.cpp
#include<iostream>
#include "polygon.h"
#include "triangle.h"
#include "rectangle.h"
using namespace std;
 main(){
 
 triangle tr;
 rectangle re;
 tr.setData(2.,5.);
 re.setData(2.,5.);
 cout<<tr.areaTR()<<endl;
 cout<<re.areaRE()<<endl;
 return 0;
 }
 


When i try to compile ir writing g++ 500.cpp polygon.cpp triangle.cpp rectangle.cpp -o 500 it shows these errors:

g++ 500.cpp polygon.cpp rectangle.cpp triangle.cpp -o 500

500.cpp: In function ‘int main()’:
500.cpp:11: error: ‘class triangle’ has no member named ‘setData’
500.cpp:12: error: ‘class rectangle’ has no member named ‘setData’
rectangle.cpp:4: error: expected unqualified-id before ‘public’
triangle.cpp:4: error: expected unqualified-id before ‘public’

Can anyone tell me what can i do to make it work? Exercise of this program
is to get triangle and rectangle areas.
Last edited on
Include the .cpp files not the .h files.
Generally you don't include cpp files.

You're deriving your classes wrong. If you want rectangle to be derived from polygon you do it like so:

1
2
3
4
class rectangle : public polygon
{
 // rectangle stuff here
};


This also means you'll have to #include "polygon.h" before "rectangle.h" in any file that uses it

Also I'm not sure where you got the syntax for your member function bodies in rectangle/triangle.cpp, but it should be like this:

1
2
3
4
5
6
7
8
// rectangle.cpp
#include "polygon.h"
#include "rectangle.h"

float rectangle::areaRE()
{
  return x*y;
}


Strange that you did this properly for polygon::setData
Last edited on
Tnx you guys now its working, i dont know what would i do without you :)

Last edited on
Topic archived. No new replies allowed.