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();
        }
    }
pmg
  • 106,608
  • 13
  • 126
  • 198
  • 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! – Kevin Vermeer Oct 01 '10 at 03: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. – James McNellis Oct 01 '10 at 03:25
  • I am geting following errors: – Julia Michelle Oct 01 '10 at 03:28

4 Answers4

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

James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • hhi james... can u send me the correct code? that wud be gr8 james – Julia Michelle Oct 01 '10 at 03:37
  • if i m replacing 3d_coord ...with some other name..its hwoing me 16 error – Julia Michelle Oct 01 '10 at 03:43
  • 4
    @Julia : It would be better if you go and read a good C++ book first and then ask your doubts/problems here. – Prasoon Saurav Oct 01 '10 at 03:44
  • its better if u help people who r stucked – Julia Michelle Oct 01 '10 at 03:50
  • @James: In order to have a virtual `plot` function, both classes need to at least inherit from a common base. But I agree that's superior to having a 3-D coordinate derive from a 2-D coordinate. – Ben Voigt Oct 01 '10 at 03:52
  • 8
    @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 Oct 01 '10 at 03:53
  • @Ben: Yeah; a common `Plottable` interface (or something like that) would be better. @Julia: As Ben said, we aren't here to write code for you (though if you have a really interesting problem, people might be interested in [writing a lot of code](http://stackoverflow.com/questions/3818277/3825181#3825181) to help solve it). If you want to learn C++, you need to learn to debug problems and use the language correctly. If you don't have [a good introductory C++ book](http://stackoverflow.com/questions/388242/the-definitive-c++-book-guide-and-list), you should get one. It will help a lot. – James McNellis Oct 01 '10 at 03:57
  • @Ben Voigt : Yes my mistake. Problems/Questions/Doubt whatever. :P – Prasoon Saurav Oct 01 '10 at 04:07
  • 6
    @Ben Voigt: Even if you honestly did not understand what Prasoon meant, there is absolutely no call for condescension. – dreamlax Oct 01 '10 at 04:18
  • @dreamlax: I don't mean for that to be interpreted in a derogatory way. Not toward Prasoon, anyway. On the other hand his former English teacher, who has taught at least a few hundred unsuspecting Indian schoolchildren to misuse the word "doubt" (and I'm exaggerating only slightly when I say I've seen that many different Indians misuse the word in online forums), is deserving of great contempt. It just illustrates the proverb "Those who can, do. Those who can't, teach." – Ben Voigt Oct 01 '10 at 04:45
  • @Ben: And those who can't teach cause Stack Overflow to be flooded with students who have learned bad programming habits from them :( Oh well. – James McNellis Oct 01 '10 at 04:49
  • @Julia: Were you able to fix the errors James already explained? Use the "edit" link below your question and add your modified code along with the new list of errors. – Ben Voigt Oct 01 '10 at 05:02
  • @Julia: You still haven't asked a question. You posted a large block of code and said there were three errors in it. You haven't said what those errors were. You need to go read what the compiler tells you is wrong, look at the source where the compiler tells you the errors are, and read a good introductory C++ book to learn how to correctly write code in C++. – James McNellis Oct 01 '10 at 05:02
  • @Ben Voigt : Please do not make unnecessary assumptions. I have already said that the mistake was mine and so fighting over such silly thing doesn't make much sense. `:)`. – Prasoon Saurav Oct 01 '10 at 05:13
  • Erros I am geting: on line 28 { expected Line 63 declaration termination incorrectly and line 63 declaration missing ; – Julia Michelle Oct 01 '10 at 06:00
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.

Alexander Rafferty
  • 6,134
  • 4
  • 33
  • 55
0

I can spot one:

void *ptr;
...
ptr-> plot(); // void::plot() is not
Anycorn
  • 50,217
  • 42
  • 167
  • 261
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...

Preet Sangha
  • 64,563
  • 18
  • 145
  • 216