tags:

views:

149

answers:

4
+1  Q: 

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();
        }
    }
+6  A: 

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
hhi james... can u send me the correct code? that wud be gr8 james
Julia Michelle
I don't think so, @Julia.
James McNellis
if i m replacing 3d_coord ...with some other name..its hwoing me 16 error
Julia Michelle
@Julia : It would be better if you go and read a good C++ book first and then ask your doubts/problems here.
Prasoon Saurav
its better if u help people who r stucked
Julia Michelle
@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
@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
@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
thnx for suggestion
Julia Michelle
@Ben Voigt : Yes my mistake. Problems/Questions/Doubt whatever. :P
Prasoon Saurav
@Ben Voigt: Even if you honestly did not understand what Prasoon meant, there is absolutely no call for condescension.
dreamlax
@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
@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
any more updates on my question? plz help
Julia Michelle
@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
@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
@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
Erros I am geting: on line 28 { expected Line 63 declaration termination incorrectly and line 63 declaration missing ;
Julia Michelle
A: 

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
Erros I am geting: on line 28 { expected
Julia Michelle
Line 63 declaration termination incorrectly and line 63 declaration missing ;
Julia Michelle
A: 

I can spot one:

void *ptr;
...
ptr-> plot(); // void::plot() is not
aaa
A: 
            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