hahastephanie
2019-10-13 22:49
采纳率: 13.3%
浏览 1.2k

C++代码运行中出现无法解析的外部命令?

代码的作用是定义一个点类Point,派生出圆类和圆柱体类Circle和Cylinder
功能是输出圆的周长(通过定义的Cirfer()函数)、面积(定义有Area()函数)和圆柱体的表面积和体积

图片说明

//Shape.h
#ifndef SHAPE_H
#define SHAPE_H
#include<iostream>
#define PI 3.1415926
using namespace std;

class Point
{
public:
    Point(double X, double Y);   //构造函数
    void Set(double X, double Y);   //设置点值
    double Getx()const;    //获取横坐标
    double Gety()const;    //获取纵坐标
    virtual void Output(ostream &out) const;   //输出函数
    virtual void Input(istream &in);    //输入函数
protected:
    double x, y;
};

class Circle :public Point
{
public:
    Circle(const Point &p, double R);   
    void Set(double X, double Y, double R);
    void Set(const Point &p, double R);
    double Getr()const;
    double Area();   //计算圆的面积
    double Cirfer();   //计算圆的周长
    void Output(ostream &out)const;
    void Input(istream &in);
protected:
    double r;
};

class Cylinder :public Circle
{
public:
    Cylinder(const Circle& c, double H);
    void Set(const Circle& c, double H);
    double Geth()const;
    double Area();    //计算圆柱体的表面积
    double Volume();    //计算圆柱体体积
    void Output(ostream& out)const;
    void Input(istream& in);
protected:
    double h;
};

ostream& operator<<(ostream& out, const Point& p);  //输出运算符重载
istream& operator>>(istream& in, const Point& p);    //输入运算符重载
#endif

//Shape.cpp
#include<iostream>
#include"Shape.h"
using namespace std;

ostream & operator<<(ostream &out, const Point &p)   //输出运算符重载
{
    p.Output(out);
    return out;
}
istream & operator>>(istream &in, Point &p)    //输入运算符重载
{
    p.Input(in);
    return in;
}

//Point类成员函数
Point::Point(double X, double Y) :x(X), y(Y) {}
void Point::Set(double X, double Y)
{
    x = X, y = Y;
}
double Point::Getx()const
{
    return x;
}
double Point::Gety()const
{
    return y;
}
void Point::Output(ostream &out)const
{
    out << '(' << x << "," << y << ')';
}
void Point::Input(istream& in)
{
    char str[80];
    in.getline(str, 80, '(');
    in.getline(str, 80, ','); x = atof(str);   //atof()是字符从ASCII转为float型数据
    in.getline(str, 80, ')'); y = atof(str);
}

//Circle类成员函数
Circle::Circle(const Point& p, double R) : Point(p), r(R) {}
void Circle::Set(double X, double Y, double R)
{
    x = X, y = Y, r = R;
}
void Circle::Set(const Point& p, double R)
{
    Point::Set(p.Getx(), p.Gety());
    r = R;
}
double Circle::Getr()const
{
    return r;
}
double Circle::Area()   //圆面积
{
    return PI * r * r;
}
double Circle::Cirfer()    //圆周长
{
    return 2 * PI * r;
}
void Circle::Output(ostream& out)const
{
    out << '[' << '(' << x << "," << y << ')' << "," << r << ']';
}
void Circle::Input(istream& in)
{
    char str[80];
    in.getline(str, 80, '[');
    Point::Input(in);
    in.getline(str, 80, ',');
    in.getline(str, 80, ']'); r = atof(str);
}

//Cylinder类成员函数
Cylinder::Cylinder(const Circle& c, double H) :Circle(c), h(H) {}
void Cylinder::Set(const Circle& c, double H)
{
    Circle::Set(Getx(),Gety(),Getr());
    h = H;
}
double Cylinder::Geth()const
{
    return h;
}
double Cylinder::Area()   //圆柱体表面积
{
    return PI*r*r* 2+2*PI*r*h;
}
double Cylinder::Volume()    //圆柱体体积
{
    return PI*r*r* h;
}
void Cylinder::Output(ostream& out)const
{
    out << '{';
    Circle::Output(out);
    out << "," << h << '}';
}
void Cylinder::Input(istream& in)
{
    char str[80];
    in.getline(str, 80, '{');
    Circle::Input(in);
    in.getline(str, 80, ',');
    in.getline(str, 80, '}'); h = atof(str);
}

//test.cpp
#include<iostream>
#include"Shape.h"

int main()
{
    Point p(5, 8);   //初始化各个类的数据成员
    Circle c(p, 10);
    Cylinder y(c, 8);

    cout << c << "\tArea:" << c.Area() << "\t\tCircumference" << c.Cirfer() << '\n'
        << y << "\tArea:" << y.Area() << "\tVolume:" << y.Volume() << endl;

    cout << "\n请注意键盘输入格式。";
    cout << "\n包括圆括号、方括号、花括号、逗号,";
    cout << "其中可以插入或不插入空格、<Tab>、换行。" << endl;

    cout << "请输入一个点 (x, y) : ";
    cin >> p;
    cout << "结果:" << p << endl;

    cout << "请输入一个圆 [(x, y), r] : ";
    cin >> c;
    cout << "结果:" << c << endl;

    cout << "请输入一个圆柱 {[(x, y), r], h} : ";
    cin >> y;
    cout << "结果:" << y << endl;
    return 0;
}
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • JonathanYan 2019-10-14 09:49
    已采纳

    这个错误和你代码想实现的效果没关系,只是函数定义的不对,构造和声明不符,一个有const一个没有

    istream& operator>>(istream &in, const Point &p);      //输入运算符重载
    istream& operator>>(istream &in,       Point &p) {     //输入运算符重载
        //...
    }
    

    无法解析的外部符号是

    class std::basic_istream<char, struct std::char_traits<char>>& _cdecl 
        operator>>(
            class std::basic_istream<char, struct std::char_traits<char>>&,
            class Point const &
        )
    //std::basic_istream<char, struct std::char_traits<char>> 指的是 istream
    //这种繁琐的类名都用define简化了,能认出来就行
    //对应下来指的就是
    class istream& _cdecl operator>>( class istream&, class Point const & )
    //忽略class和_cdecl(和编译过程有关)指的就是输入重载函数了
    
    打赏 评论
  • threenewbee 2019-10-14 09:06

    你把它写到一个文件里有报错么?

    我这里用gcc测试,写在一个文件里,可以编译。

    #include <iostream>
    
    using namespace std;
    
    #define PI 3.14
    
    //gcc不支持atof,简单起见,我写一个让他通过编译
    //float atof(char *) { return 0.0f; }
    
    class Point
    {
    public:
        Point(double X, double Y);   //构造函数
        void Set(double X, double Y);   //设置点值
        double Getx()const;    //获取横坐标
        double Gety()const;    //获取纵坐标
        virtual void Output(ostream &out) const;   //输出函数
        virtual void Input(istream &in);    //输入函数
    protected:
        double x, y;
    };
    
    class Circle :public Point
    {
    public:
        Circle(const Point &p, double R);   
        void Set(double X, double Y, double R);
        void Set(const Point &p, double R);
        double Getr()const;
        double Area();   //计算圆的面积
        double Cirfer();   //计算圆的周长
        void Output(ostream &out)const;
        void Input(istream &in);
    protected:
        double r;
    };
    
    class Cylinder :public Circle
    {
    public:
        Cylinder(const Circle& c, double H);
        void Set(const Circle& c, double H);
        double Geth()const;
        double Area();    //计算圆柱体的表面积
        double Volume();    //计算圆柱体体积
        void Output(ostream& out)const;
        void Input(istream& in);
    protected:
        double h;
    };
    
    ostream& operator<<(ostream& out, const Point& p);  //输出运算符重载
    istream& operator>>(istream& in, const Point& p);    //输入运算符重载
    
    ostream & operator<<(ostream &out, const Point &p)   //输出运算符重载
    {
        p.Output(out);
        return out;
    }
    istream & operator>>(istream &in, Point &p)    //输入运算符重载
    {
        p.Input(in);
        return in;
    }
    
    //Point类成员函数
    Point::Point(double X, double Y) :x(X), y(Y) {}
    void Point::Set(double X, double Y)
    {
        x = X, y = Y;
    }
    double Point::Getx()const
    {
        return x;
    }
    double Point::Gety()const
    {
        return y;
    }
    void Point::Output(ostream &out)const
    {
        out << '(' << x << "," << y << ')';
    }
    void Point::Input(istream& in)
    {
        char str[80];
        in.getline(str, 80, '(');
        in.getline(str, 80, ','); x = atof(str);   //atof()是字符从ASCII转为float型数据
        in.getline(str, 80, ')'); y = atof(str);
    }
    
    //Circle类成员函数
    Circle::Circle(const Point& p, double R) : Point(p), r(R) {}
    void Circle::Set(double X, double Y, double R)
    {
        x = X, y = Y, r = R;
    }
    void Circle::Set(const Point& p, double R)
    {
        Point::Set(p.Getx(), p.Gety());
        r = R;
    }
    double Circle::Getr()const
    {
        return r;
    }
    double Circle::Area()   //圆面积
    {
        return PI * r * r;
    }
    double Circle::Cirfer()    //圆周长
    {
        return 2 * PI * r;
    }
    void Circle::Output(ostream& out)const
    {
        out << '[' << '(' << x << "," << y << ')' << "," << r << ']';
    }
    void Circle::Input(istream& in)
    {
        char str[80];
        in.getline(str, 80, '[');
        Point::Input(in);
        in.getline(str, 80, ',');
        in.getline(str, 80, ']'); r = atof(str);
    }
    
    //Cylinder类成员函数
    Cylinder::Cylinder(const Circle& c, double H) :Circle(c), h(H) {}
    void Cylinder::Set(const Circle& c, double H)
    {
        Circle::Set(Getx(),Gety(),Getr());
        h = H;
    }
    double Cylinder::Geth()const
    {
        return h;
    }
    double Cylinder::Area()   //圆柱体表面积
    {
        return PI*r*r* 2+2*PI*r*h;
    }
    double Cylinder::Volume()    //圆柱体体积
    {
        return PI*r*r* h;
    }
    void Cylinder::Output(ostream& out)const
    {
        out << '{';
        Circle::Output(out);
        out << "," << h << '}';
    }
    void Cylinder::Input(istream& in)
    {
        char str[80];
        in.getline(str, 80, '{');
        Circle::Input(in);
        in.getline(str, 80, ',');
        in.getline(str, 80, '}'); h = atof(str);
    }
    
    int main()
    {
        Point p(5, 8);   //初始化各个类的数据成员
        Circle c(p, 10);
        Cylinder y(c, 8);
    
        cout << c << "\tArea:" << c.Area() << "\t\tCircumference" << c.Cirfer() << '\n'
            << y << "\tArea:" << y.Area() << "\tVolume:" << y.Volume() << endl;
    
        cout << "\n请注意键盘输入格式。";
        cout << "\n包括圆括号、方括号、花括号、逗号,";
        cout << "其中可以插入或不插入空格、<Tab>、换行。" << endl;
    
        cout << "请输入一个点 (x, y) : ";
        cin >> p;
        cout << "结果:" << p << endl;
    
        cout << "请输入一个圆 [(x, y), r] : ";
        cin >> c;
        cout << "结果:" << c << endl;
    
        cout << "请输入一个圆柱 {[(x, y), r], h} : ";
        cin >> y;
        cout << "结果:" << y << endl;
        return 0;
    }
    
    
    打赏 评论

相关推荐 更多相似问题