Hi,

I have class X and class Y such as the following;

class X
{
      ...
      void foo();
      ...
}
-----------------------------------------------
#include "X.h"

class Y
{

      void hoo()
     {
             X myNewX;
             myNewX.foo();
     }
}

int main()
{
      X newX = new X();
}

When I do this, it gives error at runtime saying no object found or something like that. Sorry I do not have a compiler now.

King Regards

Post again, when you've got actual code and actual error messages.

>> X newX = new X();
wrong syntax. should be X* newX = new X;

commented: Great Minds +10

you are using "new" right? So, it's dynamically allocating memory right? Where is your pointer to the new object in memory?

int main()
{
      X *newX = new X();
}

EDIT: *Snaps His Fingers* Beat me to it!

make the constructor public, don't put new because newX is not a pointer, and semicolon at the end of class declarations.

Worked for me.

Hi,

Let me tell you help you to visualize;

class X is my main User Interface class.
class Y is my another component of the UI which is created indirectly via class X with some call chains. i.e. Class X >> Class Z >> Class Y

I want to call the method foo() in the method hoo() in class Y...

By the way, I couldn't post the code now since I haven't it.

I don' t think this is about "X *newX = new X();" thing, what's wrong with doing this without a pointer?


King Regards.

if you don't want a pointer, use "X newX = X();"

you only new pointers.

Pardon me but is it supposed give a syntax error when we do

X newX = new X(); ????

If yes, it works for me. Anyway you know more than me so I agree with you but I can't edit my code right now.:(

Moreover, do you think that this is the reason for why my code crashes? Could this be something different?


King Regards.

I recreated your code in Visual Studio, I posted the 3 errors I got.

1. I had to make sure the functions I used were public
2. I had to put ; after every class declaration } (so class X {}; )
3. I had to remove the new keyword.

VS reported all those errors, maybe your compiler just doesn't create the object or something, I don't know.

Here is my code:
ct.h:

class X {
public:
  X() {};
  void foo();
};

ct.cpp:

#include "ct.h"

class Y {
public:
  Y() {}
  void foo() {
    X myNewX;
    myNewX.foo();
  }
};

int main () {
  X newX = X();
  return 0;
}

compiled and ran fine

By the way I just want your opinion; isn't it a problem to have a class Y ,in which we create object of class X, already created in class X?


King Regards.

yeah, I noticed I never used X inside Y and fixed it (see fixed code). The new code ran fine.

also, I'm not sure what you mean by "isn't it a problem to have a class Y already created in class X?"

By the way I just want your opinion; isn't it a problem to have a class Y already created in class X?


King Regards.

No, its a common occurence -- programmers do that frequently.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.