首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

已知矩形两点求总面积,要求用point类的组合,咋就编译不通过呢,求大大指点

2014-01-19 
已知矩形两点求面积,要求用point类的组合,咋就编译不通过呢,求大大指点。#includeiostream#includemath.

已知矩形两点求面积,要求用point类的组合,咋就编译不通过呢,求大大指点。
#include<iostream>
#include<math.h>
#include<Windows.h>
 using namespace std;
class Point
 {
 public:
 Point(int xx=0,int yy=0){X=xx;Y=yy;}
         Point(Point &p);
 int GetX(){return X;}
 int GetY(){return Y;}
 private:int X,Y;
 };


 Point::Point(Point &p)
 { X=p.X;
   Y=p.Y;
   cout<<"point拷贝以调用"<<endl;
 }

   
class Rectangle{
   public:
   Rectangle(Point xp1,Point xp2);
   double Area();
   private:
   Point p1,p2;
   };


Rectangle::Rectangle(Point xp1,Point xp2):p1(xp1),p2(xp2){};

  
   
   double Rectangle::Area()
   { 
   double x=double(p1.GetX()-p2.GetX());
       double y=double(p1.GetY()-p2.GetY());
   return x*y;
   }

   

void main()
   {  
   Point myp1(1,1);
   Point myp2(4,5);
  Rectangle rectangle(myp1,myp2);
   cout<<rectangle.Area()<<endl;
    }


>c:\users\alone\documents\visual studio 2012\projects\project1\project1\源.cpp(45): error C2146: 语法错误: 缺少“;”(在标识符“rectangle”的前面)
1>c:\users\alone\documents\visual studio 2012\projects\project1\project1\源.cpp(45): error C3861: “rectangle”: 找不到标识符
1>c:\users\alone\documents\visual studio 2012\projects\project1\project1\源.cpp(46): error C2065: “rectangle”: 未声明的标识符
1>c:\users\alone\documents\visual studio 2012\projects\project1\project1\源.cpp(46): error C2228: “.Area”的左边必须有类/结构/联合


为啥这个Rectangle rectangle不对呢

[解决办法]
很古怪的错误,你的类名Rectangle和你包含的头文件(应该是windows.h)中的某个类有冲突还是咋的,我把所有的Rectangle替换成Rectangle__, 齐了。
[解决办法]
做了一些我认为必要或有价值的调整


#include<iostream>
#include<math.h>
#include<Windows.h>

using namespace std;
class Point
{
public:
Point(int xx=0,int yy=0){X=xx;Y=yy;}
Point(const Point &p);
int GetX()const{ return X; }
int GetY()const{ return Y; }

private:
int X,Y;
};


Point::Point(const Point &p)
{
X=p.X;
Y=p.Y;
cout<<"point拷贝以调用"<<endl;
}

   
class Rectangle__
{
public:
Rectangle__(const Point& xp1, const Point& xp2);
double Area()const;

private:
Point p1,p2;
};


Rectangle__::Rectangle__(const Point& xp1, const Point& xp2)
:p1(xp1),p2(xp2)
{
}

  
// be careful of negative area!!   
double Rectangle__::Area()const
{
return std::abs(double(p1.GetX()-p2.GetX())*(p1.GetY()-p2.GetY())); 
}

   

void main()
{  
Point myp1(1,1);
Point myp2(4,5);
Rectangle__ rectangle(myp1,myp2);

cout<<rectangle.Area()<<endl;

}

[解决办法]
告诉LZ一个好办法,直接给自己的类加个命名空间不就得了~

热点排行