0

need some guide here

I got a code which is

struct Point
{
   int x;
   int y;
};

then at my class i got a function

class MyClass
{
   private:
      Point myPoint[4];
   public:
     void setPoint();
};

void MyClass::setPoint()
{
   int xData,yData;

   for (int i=0;i<4;i++)
   {
      cout << "Please enter x-ordinate:";
      cin >> xData;

      cout << "Please enter y-ordinate:";
      cin >> yData;

      //at this part the code throw a segmentation core dump.
      myPoint[i].x = xData;
      myPoint[i].y = yData;
   }

}

On first run nothing happen, but on 2nd loop, segmentation core dump occur. whats wrong with my code?

Additional code on main.cpp

#include "MyClass.h"

int main()
{
MyClass *mClass;

mclass->setPoint();
}

Thanks for helping.

user1777711
  • 1,604
  • 6
  • 22
  • 32
  • 1
    What does "run" mean to you? Execute `setPoint`? Create a `MyClass`? – Luchian Grigore Oct 26 '12 at 16:35
  • compile the code and execute, then i run the setPoint function at main class which then i key in the value, and on 2nd prompting of x-ordinate , got thrown a segmentation core dump error. – user1777711 Oct 26 '12 at 16:38
  • you probably never initialized your structs? – im so confused Oct 26 '12 at 16:40
  • Don't know if this will fix your problem, but you might want to validate that you were able to convert the user input successfully. `do { cout << "Please enter x-ordinate: "; cout.flush(); } while( cin >> xData );` And please post the code where you create an instance of `MyClass` and call `setPoint` – Praetorian Oct 26 '12 at 16:41
  • you should show us you 'main' function or whereever you create your instance of MyClass and call setPoint. – cellcortex Oct 26 '12 at 16:41
  • @AK4749 , what do you mean by Initialize my struct? – user1777711 Oct 26 '12 at 16:41
  • i'm just wondering if you created MyClass properly – im so confused Oct 26 '12 at 16:41
  • I'm hesitant to look beyond the unchecked input operations. Fix the input logic first; then we can see about the rest. – Kerrek SB Oct 26 '12 at 16:42
  • 2
    The code that you have shown us is not the culprit. If your program is failing, it is because of **some other part** of your code. Please reduce your original program to the shortest complete program that demonstrates the error and copy-paste that into the question. See http://sscce.org for more details. – Robᵩ Oct 26 '12 at 16:43
  • it start fail at myPoint[i].x = xData; – user1777711 Oct 26 '12 at 16:44
  • @user1777711 that doesn't mean it's the culprit. That could be the location of the error caused by a coding error in a different location. I'd take Rob's advice – im so confused Oct 26 '12 at 17:11

5 Answers5

4

You must allocate memory before use pointer:

int main()
{
    MyClass *mClass = new MyClass ();
    mclass->setPoint();
    return 0;
}
Denis Ermolin
  • 5,530
  • 6
  • 27
  • 44
0
MyClass *mClass;

mclass->setPoint();

There's your problem right there. You have never allocated an object, just a pointer.

int main () 
{
  MyClass mclass;
  mclass.setPoint();
}

As an alternative, if you require an object whose lifetime extends beyond the scope of your variable, you could allocate the object with new.

Robᵩ
  • 163,533
  • 20
  • 239
  • 308
0

The problem lies in creating the object. More precisely in your code you have not created a object.Its merely a pointer for which there is memory allocation happened.It should be like this:

MyClass *mClass = new MyClass();
mClass->setPoint();
0

you need memory allocation for your object

MyClass *obj = new MyClass();
                ^

new is a language construct that dynamically allocates memory

b3h3m0th
  • 1,330
  • 1
  • 11
  • 22
0

related to the construction of an object.

MyClass *mClass;

means you get a Myclass pointer, this pointer has ability to deal with a Myclass-type object, just has ability. but you haven't got an "real object", "real object" means you apply and get a block of memory on the system heap. then

mclass->setPoint();

this call means call the function of a Myclass-type object instead of call the function of a Myclass-class.

So, the code may be correct is:

MyClass *mClass = new MyClass ();
mclass->setPoint();

the new keyword means apply a block of memory for a Myclass-type object.

Joaquin
  • 11
  • 2