简体   繁体   English

C ++程序错误

[英]C++ program error

I am not able to solve 3 error . 我无法解决3错误。 Programs implementation is right but not sure how to get rid of 3 errors 程序实现是正确的,但不确定如何消除3个错误

# include <iostream.h>
# include < conio.h>
void main() {
    class coord {
        float x;
        float y;
        //Constructor
        coord(float init_x,float init_y) {
            x= init_x;
            y= init_y;
        }
        void set(float f1, float f2) {
            x = f1;
            y = f2;
        }
        float get_x() {return x;}
        float get_y() {return y;}
        virtual void plot() {
            cout<<x;
            cout<<y;
        };
        class 3d_coord: public coord {
            float z;
            //constructor
            3d_coord(float init_x,float init_y,float init_z): coord(init_x,init_y) {
                z= init_z;
            }
            void set(float f1,float f2,float f3) {
                coord::set(f1, f2); z = f3;
            }
            float get_z() {  return z; }

            virtual void plot() { 
                coord::plot();
                cout<<z;
            };

            int test
            void *ptr;

            cin>>test;
            coord a(1.1, 2.2);
            3d_coord b(3.0, 4.0, 5.0);
            if (test)
                ptr = &a;
            else
                    ptr = &b;
            ptr-> plot();
        }
    }

I can spot at least three errors: 我可以发现至少三个错误:

  1. The standard library header is <iostream> , not <iostream.h> . 标准库头文件是<iostream> ,而不是<iostream.h> <conio.h> is not a C++ standard library header and is best avoided. <conio.h>不是C ++标准库头,最好避免使用。

  2. main() must return int , not void . main()必须返回int ,而不是void

  3. Standard library names (eg cout ) are in the std namespace; 标准库名称(例如cout )在std名称空间中; you need to qualify them. 您需要使他们合格。

Since you don't say which errors you want solved, I don't know if these are them, but they are three errors nonetheless. 由于您没有说要解决哪些错误,因此我不知道这些是否是它们,但是它们仍然是三个错误。 Just in case, here are some bonus errors: 以防万一,这是一些奖金错误:

  • 3d_coord is not a valid class name; 3d_coord不是有效的类名; a class name must be an identifier, which means it must begin with a letter or an underscore, not a number. 类名必须是一个标识符,这意味着它必须以字母或下划线开头,而不是数字。

  • You shouldn't use inheritance to relate coord and 3d_coord (or whatever you choose to name it after you've fixed bonus error number 1). 您不应该使用继承来关联coord3d_coord (或在固定了奖金错误编号1之后选择命名的任何方式)。 A three-dimensional coordinate is not a two-dimensional coordinate, even though they share two common members. 即使它们共享两个公共成员,三维坐标也不是二维坐标。 Inheritance should be used for is-a relationships. 继承应用于is-a关系。

  • After extracting data from a stream ( cin , in this case), you must test to ensure the extraction succeeded. 从流(在本例中为cin )中提取数据后,必须进行测试以确保提取成功。

  • ptr is of type void* ; ptr的类型为void* ; you cannot call member functions through a void* (there are very few times where it is a good idea to use a void* in a C++ program at all). 您不能通过void*调用成员函数(很少有情况下,最好在C ++程序中使用void* )。

  • It's not really an error, but usually you don't define classes inside of functions (there are exceptions; functors, for example). 这并不是真正的错误,但是通常您不会在函数内部定义类(例如,有例外;例如函子)。

您没有将类定义放入主函数中,也没有将3d_coord类放入coord类中。

I can spot one: 我可以发现一个:

void *ptr;
...
ptr-> plot(); // void::plot() is not
            cin>>test;
            coord a(1.1, 2.2);
            3d_coord b(3.0, 4.0, 5.0);
            if (test)
                ptr = &a;
            else
                    ptr = &b;
            ptr-> plot();

Doesn't appear to be a function... 似乎不是一个功能...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM