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

  • C/C++
  • Thread starter Pattielli
  • Start date
  • Tags
    C++
In summary, the compiler generated a warning when I used point() with no arguments. The warning indicated that there was an integral size mismatch.
  • #1
Pattielli
296
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
  • #2
What is the Point class? Can we see its source code?

- Warren
 
  • #3
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 ?
 
  • #4
Can anyone help me please ?
 
  • #5
Your program works fine for me. Output was:

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

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

using namespace std;
cout << "Blah";

or

std::cout << "Blah";

They are equivalent.

- Warren
 
  • #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 ?
 
  • #9
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:

1. Why is my C++ program returning all ones when I use p->getX()?

This issue can occur due to a flaw in the program logic, specifically in the implementation of the getX() function. It is possible that the function is not properly accessing or returning the value of the variable it is supposed to retrieve.

2. How can I fix the problem of p->getX() returning all ones?

To fix this issue, you will need to carefully review the code for the getX() function and ensure that it is properly accessing and returning the desired variable. It may also be helpful to use debugging tools or print statements to track the flow of the program and identify any errors.

3. Can other factors contribute to p->getX() returning all ones?

Yes, there may be other factors at play, such as memory allocation errors, incorrect data types, or even issues with the input data. It is important to thoroughly check all aspects of the program and its inputs to identify and address any potential contributing factors.

4. Why does p->getX() only return all ones in certain situations?

This could be due to specific conditions or data values triggering the flaw in the program. It is important to carefully analyze the code and program flow to determine what may be causing the issue in those particular situations.

5. Is there a way to prevent this type of flaw in a C++ program?

While it is not possible to completely eliminate the possibility of flaws in a program, there are steps that can be taken to minimize the likelihood of this type of issue. These include thorough testing and debugging, using good programming practices and techniques, and continuously reviewing and improving the code.

Similar threads

  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
3
Replies
75
Views
4K
Replies
10
Views
951
  • Programming and Computer Science
3
Replies
73
Views
4K
  • Programming and Computer Science
2
Replies
36
Views
2K
  • Programming and Computer Science
Replies
11
Views
995
  • Programming and Computer Science
Replies
6
Views
897
  • Programming and Computer Science
Replies
20
Views
1K
Back
Top