C/C++ C++ Program Flaw: Why p->getX() Returned All Ones?

  • Thread starter Thread starter Pattielli
  • Start date Start date
  • Tags Tags
    C++
AI Thread Summary
The discussion centers around a programming issue where the output of a program incorrectly displays values as (1,1) instead of the expected (12,36.3). This confusion arises from misusing function calls in C++. Specifically, the user mistakenly calls `p->getX` and `p->getY` without parentheses, which leads the compiler to treat them as pointers to the functions rather than invoking them. This results in the output of default values, likely due to the way the output stream handles such cases. The conversation highlights the importance of using parentheses for function calls and notes that the behavior may vary depending on the compiler and stream implementation. The participants also discuss the significance of understanding class definitions and the const keyword in member functions, emphasizing the learning curve for those new to object-oriented programming.
Pattielli
Messages
296
Reaction score
0
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:
	Point *p=new Point(12.0,36.3); 
	std::cout<<"p(x,y) = ("<<p->getX()<<","<<p->getY()<<")"<<std::endl;

Thank you,
 
Technology news on Phys.org
What is the Point class? Can we see its source code?

- Warren
 
I'll give you all, here it is...
Code:
#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 ?
 
Can anyone help me please ?
 
Your program works fine for me. Output was:

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

- Warren
 
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;
 
cout is in the std namespace. You may either do:

using namespace std;
cout << "Blah";

or

std::cout << "Blah";

They are equivalent.

- Warren
 
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 ?
 
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.
 
  • #10
Code:
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:
  • #11
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
 
  • #12
Thank Chroot,
Why point (1,1) is output is what I really would like to know now..
 
  • #13
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
 
  • #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,
 
  • #15
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
 
  • #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,
 
  • #17
Feel free to ask any questions you might have, Patielli, we're here to help.

- Warren
 
  • #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 !
 
  • #19
Patielli,

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

- Warren
 
  • #20
Okay, you Close this thread, and no more scatters, I got the answer anyway...
Thank you,
 
Last edited:
Back
Top