Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

My C++ program's flaw

  1. Jun 1, 2004 #1
    Can you tell me why the values cout-ed were all ones (1) if I misused the p->getX() and p->getX ???
    I don't understand how the compiler will check such statements since there are no errors but only warnings of integral size mismatch instead...
    Code (Text):
        Point *p=new Point(12.0,36.3);
        std::cout<<"p(x,y) = ("<<p->getX()<<","<<p->getY()<<")"<<std::endl;
    Thank you,
     
  2. jcsd
  3. Jun 1, 2004 #2

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    What is the Point class? Can we see its source code?

    - Warren
     
  4. Jun 1, 2004 #3
    I'll give you all, here it is...
    Code (Text):
    #ifndef UnitTest1_H
    #define UnitTest1_H
    #include<iostream>

    class Point{
    public:
        Point(double x, double y);
        double getX() const;
        double getY() const;

    private:
        double x_;
        double y_;
    };

    Point::Point(double x,double y):x_(x),y_(y){}

    double Point::getX()const{
        return x_;
    }

    double Point::getY() const{
        return y_;
    }

    #endif
    Could you help me ?
     
  5. Jun 1, 2004 #4
    Can anyone help me please ???
     
  6. Jun 1, 2004 #5

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Your program works fine for me. Output was:

    p(x,y) = (12,36.3)
    Press any key to continue

    - Warren
     
  7. Jun 1, 2004 #6

    AKG

    User Avatar
    Science Advisor
    Homework Helper

    Integral size mismatch?

    The only thing I might suggest is putting p->getX() in brackets, like this:

    ... << (p->getX()) << ", " << (p->getY()) ...

    Also, I'm not familiar with the "std::" but maybe that's giving you problems. Why not just use:

    cout << "p(x,y) = (" .... << endl;
     
  8. Jun 1, 2004 #7

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    cout is in the std namespace. You may either do:

    using namespace std;
    cout << "Blah";

    or

    std::cout << "Blah";

    They are equivalent.

    - Warren
     
  9. Jun 1, 2004 #8
    Thank you very much,
    But you weren't answering my question...:(
    I meant p->getX() and p->getX.
    All compiled fine but the latter stimulated compiler to emit warnings and the output is (1,1). And I would like to know why that happened ?
    I know that is a very stupid mistake made when coding but i have found out that students who are new to OOP will oftenly make this kind of mistake and they (me inclusive) keep asking this thing repeatedly. I am new and I made that mistake, I am wondering what compilers will then do after they read that line ?
     
  10. Jun 1, 2004 #9

    AKG

    User Avatar
    Science Advisor
    Homework Helper

    Oh, I didn't even notice that part of your question (which was your whole question)! I just thought you were having general problems. Anyways, getX() performs a call to a function. If you're ever going to call to a function, you need to put the name of the function and the arguments. Even if it has no arguments to pass, you still need to write the empty brackets. Why are you getting ones? I don't know. Look in your book for how to call to functions, and see what it says, and if it says anything as to why you need to put the brackets.
     
  11. Jun 1, 2004 #10
    Code (Text):
    int main(){
            Point *p=new Point(12.0,36.3);
        std::cout<<p->getX<<p->getY<<std::endl;
            delete p;
            return 0;
    }
    I then got 11 which i expected to be 12.036.3
    I put them in brackets to make it look clearer than it used to, just like (9,3)...
     
    Last edited: Jun 1, 2004
  12. Jun 1, 2004 #11

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    The last code that you posted (post #10) cannot compile or run, because getX and getY are not members of the Point class -- the methods getX() and getY() are, with the parentheses included. The line should say std::cout<<p->getX()<<p->getY()<<std::endl;

    Your original program ran fine. I have no idea what you're asking.

    - Warren
     
  13. Jun 1, 2004 #12
    Thank Chroot,
    Why point (1,1) is output is what I really would like to know now..
     
  14. Jun 1, 2004 #13

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Looks like the key is the const keyword here:

    double Point::getX() const {

    It also has something to do with the way the cout stream operates. Note that code like

    int m = p->getX;

    won't compile. Apparently the cout stream has some kind of overloaded operator that's able to take a const member function as an argument -- though I'm frankly not too sure which operator is taking it. Somewhere along the way, probably in the internals of the cout stream itself, a variable is being initialized to 1, and then probably the assignment of that variable is failing, leaving 1 as the output.

    I frankly wouldn't worry about it -- it may be specific to the actual stream implementation being used.

    - Warren
     
  15. Jun 2, 2004 #14
    Uhm...Okay, you might mean ostream, I guess
    And I also think this also depends on compilers being used, i am using MSC++6.0, which works nicer than DevC++, Watcom, comeau, these only give me a bunch of irritative errors...(i mean this program only, and in this case only)./.

    Thanks a lot for your help,
     
  16. Jun 2, 2004 #15

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Yes, cout is of class ostream. It probably does vary both with the compiler and with the stream implementation -- keep in mind a given stream library only needs to expose the ANSI interface -- how it works inside is irrelevant.

    - Warren
     
  17. Jun 2, 2004 #16
    okay, i see it, this is what i have been told many a time too, but never i could do as what I was told...
    Digging deeper into Iostream is also one of my interest, it is really hard to understand the whole points in Angelika's book...I am working on it too,
    I have tried to read that book to grasp as much as i could but honestly failed....I admit I am just really new to this computer field and I am sure i made a big mistake, a very basic one about class definition and its meaning...
    Thank Chroot for your help and suggestions,
     
  18. Jun 2, 2004 #17

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Feel free to ask any questions you might have, Patielli, we're here to help.

    - Warren
     
  19. Jun 2, 2004 #18
    Okay, sure !
    Can you help me with that driver's questions ?

    I know graduate students will have to take a course about Eastern and Western culture, and when you talked about Brazilian culture, I thought you might have taken it from the course...And that WAS why I WENT ON WITH THAT JOKE !!!!
    I really HATE people who look down on others and those whose attitudes are AGGRESSIVE !!!!
     
  20. Jun 2, 2004 #19

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Patielli,

    I would be really helpful for you to keep replies about a thread in that thread, rather than scattered around like this.

    - Warren
     
  21. Jun 2, 2004 #20
    Okay, you Close this thread, and no more scatters, I got the answer anyway...
    Thank you,
     
    Last edited: Jun 2, 2004
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: My C++ program's flaw
  1. C++ program help (Replies: 2)

Loading...