Why does p->getX() return all ones with no compiler error?

  • Topic: C/C++ 
  • Thread starter Thread starter Pattielli
  • Start date Start date
  • Tags Tags
    C++
Join the discussion
Registration is free. Start your own thread to ask a follow-up.
19 replies · 5K views
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,
 
Physics 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 ?
 
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:

count << "p(x,y) = (" ... << endl;
 
count is in the std namespace. You may either do:

using namespace std;
count << "Blah";

or

std::count << "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.
 
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:
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::count<<p->getX()<<p->getY()<<std::endl;

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

- Warren
 
Thank Chroot,
Why point (1,1) is output is what I really would like to know now..
 
Looks like the key is the const keyword here:

double Point::getX() const {

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

int m = p->getX;

won't compile. Apparently the count 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 count 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
 
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,
 
Yes, count 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
 
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,
 
Feel free to ask any questions you might have, Patielli, we're here to help.

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

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

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