我做错了什么C ++错误

时间:2014-05-02 01:24:10

标签: c++

我正在尝试执行以下程序并获得以下错误。请告诉我我错在哪里。 " main.cpp(289):错误C2535:' void __thiscall Line :: setdefaultvalues(void)' :成员函数已经定义或声明"

#include<iostream>
#define PI 3.14
#include<conio.h>
using namespace std;
class Point
{
    int x;
    int y;
    public:
    Point(int=0,int=0);
    int getX();
    int getY();
    void setX(int);
    void setY(int);

};

    Point::Point(int ax,int ay){
        x = ax;
        y = ay;
    }
    int Point::getX(){
        return x;
    }
    int Point::getY(){
        return y;
    }
    void Point::setX(int ax){
        x = ax;

    }
    void Point::setY( int ay){
        y =ay;
    }


class Shape
{
    public:

    virtual void draw()
    {
        cout<<"draw a shape"<<endl;
    }
   virtual int calcarea()
   {
       cout<<"area of shape is"<<endl;
        return 0;
   }

};
  class Rectangle:public Shape

  {
  Point p1;
  Point p2;
  Point p3;
  Point p4;
  public:
      Rectangle(Point &a ,Point &b ,Point &c ,Point &d);
          Point getp1();
          Point getp2();
          Point getp3();
          Point getp4();

virtual void draw();
virtual int calcarea();
    int getlength();
    int getWidth();

      void show()
      {
          cout<<"P1 ( "<<p1.getX()<<","<<p1.getY()<<" )"<<endl;
            cout<<"P2 ( "<<p2.getX()<<","<<p2.getY()<<" )"<<endl;
            cout<<"P3( "<<p3.getX()<<","<<p3.getY()<<" )"<<endl;
             cout<<"P4( "<<p4.getX()<<","<<p4.getY()<<" )"<<endl;
            //p3.setX(8);
           // p3.setY(4);
            //p4.setX(0);
            //p4.setY(4);
      }

      void defaultvalues (){

            p1.setX(0);
            p1.setY(0);
            p2.setX(8);
            p2.setY(0);
            p3.setX(8);
            p3.setY(4);
            p4.setX(0);
            p4.setY(4);
        }
};
      Rectangle::Rectangle(Point &a,Point &b,Point &c,Point &d){

       if ((a.getX()<0||a.getX()>20)||(a.getY()<0||a.getY()>20)||
           (b.getX()<0||b.getX()>20) ||(b.getY()<0||b.getY()>20)||
           ( c.getX()<0||c.getX()>20) ||(c.getY()<0||c.getY()>20)||
            (d.getX()<0||d.getX()>20)  ||(d.getY()<0||d.getY()>20))


       {

          std::cout << "Incorect Point value for constructor replacing with default values" <<std::endl;

             defaultvalues();
       }
       else
       {
            p1.setX(a.getX());
            p1.setY(a.getY());
            p2.setX(b.getX());
            p2.setY(b.getY());
            p3.setX(c.getX());
            p3.setY(c.getY());
            p4.setX(d.getX());
            p4.setY(d.getY());
       }


       /* if( a.getX() != c.getX() )
            cout<<"error:a and c must have same x values"<<endl;
          if( b.getX() != d.getX() )
            cout<<"error:b and c must have same x values"<<endl;
*/
      }


          int Rectangle ::getlength(){


                    int l;

                    if(p1.getX()==p2.getX())
                     {
                         l=p2.getY()-p1.getY();

                     }
                     else if(p2.getX()==p3.getX())
                     {
                         l=p3.getY()-p1.getY();

                     }
                     else if(p1.getX()==p4.getX())
                     {
                         l=p4.getY()-p1.getY();
                     }
                     else
                     {
                         l=0.0;
                     }

                int w;

                if(p1.getY()==p2.getY())
                     {
                         w=p2.getX()-p1.getX();

                     }
                     else if(p2.getY()==p3.getY())
                     {
                         w=p3.getX()-p1.getX();

                     }
                     else if(p1.getY()==p4.getY())
                     {
                         w=p4.getX()-p1.getX();
                     }
                     else
                     {
                         w=0.0;
                     }

          if (l>w)
          {
              return l;
          }
          else
          {
              return w;
          }

                };
                int Rectangle::getWidth()
                {
                    int l;
                    int w;


                    if(p1.getX()==p2.getX())
                     {
                         l=p2.getY()-p1.getY();

                     }
                     else if(p2.getX()==p3.getX())
                     {
                         l=p3.getY()-p1.getY();

                     }
                     else if(p1.getX()==p4.getX())
                     {
                         l=p4.getY()-p1.getY();
                     }
                     else
                     {
                         l=0.0;
                     }



                if(p1.getY()==p2.getY())
                     {
                         w=p2.getX()-p1.getX();

                     }
                     else if(p2.getY()==p3.getY())
                     {
                         w=p3.getX()-p1.getX();

                     }
                     else if(p1.getY()==p4.getY())
                     {
                         w=p4.getX()-p1.getX();
                     }
                     else
                     {
                         w=0.0;
                     }

                if (l<w)
                {
                    return l;
                }
                else
                {
                    return w;
                }
      }
        int Rectangle::calcarea()
        {
            int l=getlength();
            int w=getWidth();
            return l*w;
        }
         void Rectangle:: draw()
         {
             cout<<"this is a rectangle"<<endl;
         }





class Line : public Shape {

Point p1;
Point p2;
public:
     void  setdefaultvalues();

        Line(Point&a,Point&b)
        {
        if((a.getX()<0||a.getX()>20)
        ||(a.getY()<0||a.getY()>20)
        ||(b.getX()<0||b.getX()>20)
        ||(b.getY()<0||b.getY()>20))
        {
         setdefaultvalues();
        }
        else
        {
        if((a.getX()==b.getX()||a.getY()==b.getY()))
        {
         setdefaultvalues();
        }
        else
        {
            p1.setX(a.getX());
            p1.setY(a.getY());
            p2.setX(b.getX());
            p2.setY(b.getY());

        }
        }
        }

    void  setdefaultvalues()
    {
        p1.setX(0);
        p1.setY(0);
        p2.setX(8);
        p2.setY(8);
    }


    virtual void draw()
    {
        cout<<"this is a line"<<endl;
    }

    void show()

    {
                 cout<<"P1 ( "<<p1.getX()<<","<<p1.getY()<<" )"<<endl;
                 cout<<"P2 ( "<<p2.getX()<<","<<p2.getY()<<" )"<<endl;

    }
};


    /*class Circle : public Shape {
     Point centre;
    double radius;
public:
    Circle(Point&a, double r)
    {
        if(r>0&&r<20)
        {
            radius=r;
            centre.setX(a.getX());
            centre.setY(a.getY());

        }
        else
        {
            radius=3;
            centre.setX(0);
            centre.setY(0);

        }

    }

    virtual void draw()
    {


    cout<<"this is a circle"<<endl;
    }
    virtual int calcarea()

    {
      cout<<PI*radius*radius<<endl;
    }
};*/
class Triangle:public Shape{

Point p1;
Point p2;
Point p3;
public:

      Triangle(Point&a,Point&b,Point&c){

     if((a.getX()==b.getX())&&(a.getY()==b.getY())
     &&(a.getX()==c.getX())&&(a.getY()==c.getY())
     &&(b.getX()==c.getX())&&(b.getY()==c.getY()))

        {

          std::cout << "Incorect values" <<std::endl;
        }
    else
    {  p1.setX(15);
       p1.setY(15);
       p2.setX(23);
       p2.setY(30);
       p3.setX(50);
       p3.setY(25);
    }


  virtual int calcarea();
  virtual void draw();

  }


virtual int calcarea()
{  int area;
   area=[p1*getX()(p2*getY()-p3*getY())+p2*getX()(p3*getY()-p1*getY())+p3*getX()(p1*getY()-p2*getY())]/2;
    return area;

}
};

void drawshapes();

int main() {
    Point p1(0,0),p2(0,4),p3(4,0),p4(4,4);
    Shape* _shape[ 4 ];
    _shape[0]=new Rectangle(p1,p2,p3,p4);
    /*drawshapes(_shape,4);
    calarea(_shape,4);
    */
    Line l1(p1,p2),l2(p1,p3);
    //_shape[0]=new circle(p1,3);
    _shape[1]=new Line(p1,p4);
    _shape[2]=new Line(p2,p3);
    /* drawshapes(_s,4);
     calcarea(_s,4);*/
     return 0;
}

1 个答案:

答案 0 :(得分:4)

您有以下代码:

class Line : public Shape
{

  Point p1;
  Point p2;
public:
  void  setdefaultvalues();
  ...

  void  setdefaultvalues()
  {
    p1.setX(0);
    p1.setY(0);
    p2.setX(8);
    p2.setY(8);
  }
  ...
}

将该代码转换为:

class Line : public Shape
{

  Point p1;
  Point p2;
public:
  void  setdefaultvalues()
  {
    p1.setX(0);
    p1.setY(0);
    p2.setX(8);
    p2.setY(8);
  }
  ...
}

换句话说,取 setdefaultvalues 函数的两个声明并将其减少为一个。

编辑:将定义更改为声明。