1

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();
        }
    }
3
  • 1
    Please format your code by putting four spaces in front of every line. The "0101" button does this automatically if you select the text to format. Thanks! Commented Oct 1, 2010 at 3:24
  • 11
    The Stack Overflow community is not a C++ compiler. If your C++ compiler is giving you errors, you should indicate what those errors are (exact text please!) and exactly where in the source the compiler says the errors are. Commented Oct 1, 2010 at 3:25
  • I am geting following errors: Commented Oct 1, 2010 at 3:28

4 Answers 4

6

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 (e.g. 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).

Sign up to request clarification or add additional context in comments.

15 Comments

hhi james... can u send me the correct code? that wud be gr8 james
if i m replacing 3d_coord ...with some other name..its hwoing me 16 error
@Julia : It would be better if you go and read a good C++ book first and then ask your doubts/problems here.
@Julia: You will not get very far with that attitude. We are not paid to help you, we do it because we are kind, and we expect that you put in as much effort toward solving your own problem as we do to help you.
@Ben Voigt: Even if you honestly did not understand what Prasoon meant, there is absolutely no call for condescension.
|
0

You don't put the class definition inside the main function, and you don't put the 3d_coord class inside the coord class.

2 Comments

Erros I am geting: on line 28 { expected
Line 63 declaration termination incorrectly and line 63 declaration missing ;
0

I can spot one:

void *ptr;
...
ptr-> plot(); // void::plot() is not

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.