编程论坛's Archiver

winnerflyer 发表于 2008-5-9 13:19

不明白的错误,帮解决下,谢谢

是一个求三角形面积的

大家看下:

[color=Lime]//point.h[/color]

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

class point
{
public:
        point()
        {
                X=Y=0;
        }

        point(double x,double y)
        {
                X=x;
                Y=y;
                cout<<"Cpoint::Cpoint is called"<<"\n";

        }

        double X;
        double Y;
};


[color=Lime]//tria.h[/color]
#include<iostream.h>
#include<math.h>
#include"point.h"


class Tria
{
private:
        point A;
        point B;
        point C;
        double a;double b;double c;double s;
public:
        Tria()
        {
                A(1,0); B(0,1); C(0,0);
                Distance();
                cout<<"Tria::Tria() is called"<<endl;
        }

        Tria(double a,double b,double c,double d,double e,double f)
        {
                A(a,b); B(c,d);C(d,f);
                if(ifTria(A,B,C)==0)
                {
                        cout<<"the value is not availeble"<<endl;
                        A(1,0); B(0,1)l; C(0,0);
                }
                Distance();
                cout<<"Tria::Tria(double) is called"<<endl;
        }

        double distance(const point &a, const point &b)  //求两点间的距离
        {
                return sqrt( (a.X-b.X)*(a.X-b.X)+(a.Y-b.Y)*(a.Y-b.Y) );
        }

        void Distance()  //求出边长和周长
        {
                c=distance(A,B);
                b=distance(A,C);
                a=distance(B,C);
        }

        int ifTria(const point &a, const point &b,const point &c) //判断三点是否构成三角形,并返回判断结果
        {
                if( (a.Y-b.Y)*(b.X-c.X)==(a.X-b.X)*(b.Y-c.Y) )
                        return 0;
                else return 1;

        }

        int setA(double x,double y) // 给点赋值
   {
           point t;
           t.X=x; t.Y=y;
           if( ifTria(t,B,C)==1)
           { A=t;
                 Distance();
             return 1;
           }
           else return 0;
   }

   int setB(double x,double y)  // 给点赋值
   {
           point t;
           t.X=x; t.Y=y;
           if( ifTria(A,t,C)==1)
           { B=t;
                 Distance();
             return 1;
           }
           else return 0;
   }

   int setC(double x,double y)  // 给点赋值
   {
           point t;
           t.X=x; t.Y=y;
           if( ifTria(A,B,t)==1)
           { C=t;
                 Distance();
             return 1;
           }
           else return 0;
   }

   double Area()  //求此三角形的面积
   {   
           double temp,p;
           p=s/2;
           temp=p*(p-a)*(p-b)*(p-c);
           return ( sqrt(temp));

   }

   void show() const
   {
           cout<<"A("<<A.x<<"."<<A.Y<<")"<<endl;
           cout<<"B("<<B.x<<"."<<B.Y<<")"<<endl;
           cout<<"C("<<C.x<<"."<<C.Y<<")"<<endl;
           cout<<"a="<<a<<endl;
           cout<<"b="<<b<<endl;
           cout<<"c="<<c<<endl;
           cout<<"the area is "<<Area<<endl;

};


[color=SeaGreen]// Triamian[/color]
#include <iostream.h>
#include "tria.h"
int main()
{
        Tria ob1;
        Tria ob2(1,3,5,7,9,10);

        ob1.show();
        ob2.show();       
         return 0;
}


编译出现一下错误:
[color=Magenta][color=Lime]error C2059: syntax error : 'PCH creation point'
triamain0.cpp(8) : error C2334: unexpected token(s) preceding '{'; skipping apparent function body
triamain0.cpp(17) : fatal error C1004: unexpected end of file found[/color][/color]执行 cl.exe 时出错.

winnerflyer 发表于 2008-5-9 17:59

没人回复 摸索的路好难啊.....我又把东西改了下 能运行了

//point.h

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

class point
{
public:
        point()
        {
                X=Y=0;
        }

        point(double x,double y)
        {
                X=x;
                Y=y;
                cout<<"Cpoint::Cpoint is called"<<"\n";

        }

        double X;
        double Y;
};

//tria.h
#include<iostream.h>
#include<math.h>
#include"point.h"


class Tria
{
private:
        point A;
        point B;
        point C;
        double a;double b;double c;double s;
public:
        Tria():        A(1,0), B(0,1), C(0,0)    //要用初始化列表
        {
       
                Distance();
                cout<<"Tria::Tria() is called"<<endl;
        }

        Tria(double a,double b,double c,double d,double e,double f):A(a,b), B(c,d),C(d,f)    //要用初始化列表
        {
               
                if(ifTria(A,B,C)==0)
                {
                        cout<<"the value is not availeble"<<endl;
                        //setA(1,0); setB(0,1); setC(0,0);
                }
                Distance();
                cout<<"Tria::Tria(double) is called"<<endl;
        }

        double distance(const point &a, const point &b)  //求两点间的距离
        {
                return sqrt( (a.X-b.X)*(a.X-b.X)+(a.Y-b.Y)*(a.Y-b.Y) );
        }

        void Distance()  //求出边长和周长
        {
                c=distance(A,B);
                b=distance(A,C);
                a=distance(B,C);
        }

        int ifTria(const point &a, const point &b,const point &c) //判断三点是否构成三角形,并返回判断结果
        {
                if( (a.Y-b.Y)*(b.X-c.X)==(a.X-b.X)*(b.Y-c.Y) )
                        return 0;
                else return 1;

        }

        int setA(double x,double y) // 给点赋值
   {
           point t;
           t.X=x; t.Y=y;
           if( ifTria(t,B,C)==1)
           { A=t;
                 Distance();
             return 1;
           }
           else return 0;
   }

   int setB(double x,double y)  // 给点赋值
   {
           point t;
           t.X=x; t.Y=y;
           if( ifTria(A,t,C)==1)
           { B=t;
                 Distance();
             return 1;
           }
           else return 0;
   }

   int setC(double x,double y)  // 给点赋值
   {
           point t;
           t.X=x; t.Y=y;
           if( ifTria(A,B,t)==1)
           { C=t;
                 Distance();
             return 1;
           }
           else return 0;
   }

   double Area()  //求此三角形的面积
   {   
           double temp,p;
           p=s/2;
           temp=p*(p-a)*(p-b)*(p-c);
           return ( sqrt(temp));

   }

   void show()
   {
           cout<<"A("<<A.X<<"."<<A.Y<<")"<<endl;
           cout<<"B("<<B.X<<"."<<B.Y<<")"<<endl;
           cout<<"C("<<C.X<<"."<<C.Y<<")"<<endl;
           cout<<"a="<<a<<endl;
           cout<<"b="<<b<<endl;
           cout<<"c="<<c<<endl;
           cout<<"the area is "<<Area()<<endl;
   }

};


// Triamian

#include <iostream.h>
#include "tria.h"


int main()
{
        Tria ob1;
        Tria ob2(1,3,5,7,9,10);
        cout<<endl<<endl;
    cout<<"ob1::show:"<<endl;
        ob1.show();
        cout<<endl<<endl;
        cout<<"ob2::show:"<<endl;
        ob2.show();
       
return 0;
}

忘记喧嚣 发表于 2008-5-10 00:59

}

   void show() const
   {
       cout<<"A("<<A.x<<"."<<A.Y<<")"<<endl;
       cout<<"B("<<B.x<<"."<<B.Y<<")"<<endl;
       cout<<"C("<<C.x<<"."<<C.Y<<")"<<endl;
       cout<<"a="<<a<<endl;
       cout<<"b="<<b<<endl;
       cout<<"c="<<c<<endl;
       cout<<"the area is "<<Area<<endl;

};


其实就是这里少了一个  }
好象你已经改过来了

页: [1]

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.