注册 登录
编程论坛 C++教室

error C2064: term does not evaluate to a function

稻草人25 发布于 2016-04-20 13:35, 4557 次点击
#include<iostream>
#include<math.h>
using namespace std;
class Shape
{
public:
    virtual void ShapeName() const=0;
};
class Circle:public Shape
{
public:
    Circle(double r):radius(r){}
    double area(){return(3.14159*radius*radius);}
    virtual void ShapeName() const {cout<<"Circle:";}
    virtual void printArea();
protected:
    double radius;
};
void Circle::printArea()
{cout<<Circle::area()<<endl;}
class Rectangle:public Shape
{
public:
    Rectangle(float l,float w):length(l),width(w){}
    float area(){return length*width;}
    virtual void ShapeName() const {cout<<"Rectangle:";}
    void printArea();
protected:
    float length;
    float width;
};
void Rectangle::printArea()
{cout<<Rectangle::area()<<endl;}
class Triangle:public Shape
{
public:
    Triangle(float x=0,float y=0,float z=0);
    float area()
    {
        float p;
        p=(x+y+z)/2;
        return sqrt(p(p-x)(p-y)(p-z));
    }
    virtual void ShapeName() const {cout<<"Triangle:";}
    void printArea();
protected:
    float x;
    float y;
    float z;
};
void Triangle::printArea()
{cout<<Triangle::area()<<endl;}
int main()
{
    Circle a(2);
    Rectangle b(5,3.5);
    Triangle c(2,3,4);
    a.printArea();
    b.printArea();
    c.printArea();
    return 0;
}
出现2.cpp(42) : error C2064: term does not evaluate to a function如何解决
4 回复
#2
rjsp2016-04-20 13:46
直接说 return sqrt(p(p-x)(p-y)(p-z)) 这一句编译失败
return sqrt( p*(p-x)*(p-y)*(p-z) );
#3
稻草人252016-04-20 14:32
回复 2楼 rjsp
谢谢  才发现
#4
稻草人252016-04-21 12:05
回复 楼主 稻草人25
谢谢  程序上还有没有其他问题??
#5
rjsp2016-04-21 16:12
回复 4楼 稻草人25
建议你找一本真正的C++书看看

先看main函数,也就是先决定怎么使用,后决定怎么实现
程序代码:
#include <iostream>

#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

class Shape
{
public:
    virtual const char* Name() const = 0;
    virtual double Area() const = 0;
protected:
    virtual std::ostream& output_( std::ostream& os ) const = 0;

    friend std::ostream& operator<<( std::ostream& os, const Shape& s );
};
std::ostream& operator<<( std::ostream& os, const Shape& s )
{
    return s.output_(os);
}

class Circle : public Shape
{
public:
    explicit Circle( double r ) : radius_(r)
    {
    }
    virtual const char* Name() const
    {
        return "Circle";
    }
    virtual double Area() const
    {
        return M_PI * radius_ * radius_;
    }
protected:
    double radius_;

protected:
    virtual std::ostream& output_( std::ostream& os ) const
    {
        return os << Name() << "(radius=" << radius_ << ") area=" << Area();
    }
};

class Rectangle : public Shape
{
public:
    Rectangle( double width, double height ) : width_(width), height_(height)
    {
    }
    virtual const char* Name() const
    {
        return "Rectangle";
    }
    virtual double Area() const
    {
        return width_*height_;
    }
protected:
    double width_;
    double height_;

protected:
    virtual std::ostream& output_( std::ostream& os ) const
    {
        return os << Name() << "(width=" << width_ << ", height=" << height_ << ") area=" << Area();
    }
};

#include <iostream>
using namespace std;

int main( void )
{
    Circle c( 2.0 );
    cout << c << endl;

    Rectangle r( 3.0, 4.0 );
    cout << r << endl;

    Shape& s1 = c;
    Shape& s2 = r;
    cout << s1 << '\n' << s2 << endl;

    return 0;
}

1