简体   繁体   中英

C++ program error

I am not able to solve 3 error . Programs implementation is right but not sure how to get rid of 3 errors

# 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> . <conio.h> is not a C++ standard library header and is best avoided.

  2. main() must return int , not void .

  3. Standard library names (eg cout ) are in the std namespace; 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; 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). 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.

  • After extracting data from a stream ( cin , in this case), you must test to ensure the extraction succeeded.

  • ptr is of type 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).

  • 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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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