Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Object Oriented Programming doubt (2)

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
0
0
Last time i was wrong. i´ve made real example which gives me the problem that i wanted to ask you. i will tell you again about my doubt.

i have two classes (for example X and Y) and i need a method in each one which let me obtain an object from the other class. I´ve tested the next:
(see my explanation below)

file x.hpp------------------
#include <iostream.h>
#include &quot;C:\__trabajo\prog\__cpp\clases\_pruebas\UTM\problema1\Y.hpp&quot;

class Y;

class X
{
private:
int i;
public:
void toY(Y &y)
{
y.setJ(function(i));
}
int function(int val)
{
cout << &quot;To Y&quot; << endl;
return val*2; //really a more complex transformation
}
int getI() { return i; }
void setI(int i) { this.i=i; }
};
end of file x.hpp------------------

file y.hpp------------------
#include <iostream.h>
#include &quot;C:\__trabajo\prog\__cpp\clases\_pruebas\UTM\problema1\X.hpp&quot;


class X;

class Y
{
private:
int j;
public:
void toX(X &x)
{
x.setI(function(i));
}
int function(int val)
{
cout << &quot;To X&quot; << endl;
return val*4; //really a more complex transformation
}
int getJ() { return j; }
void setJ(int j) { this.j=j; }
};
end of file y.hpp------------------

file main.cpp------------------
#include &quot;C:\__trabajo\prog\__cpp\clases\_pruebas\UTM\problema1\X.hpp&quot;
#include &quot;C:\__trabajo\prog\__cpp\clases\_pruebas\UTM\problema1\Y.hpp&quot;

void main(void)
{
X ox;
Y oy;

ox.toY(oy); //ox change to oy
}
end of file main.cpp------------------


but in each file i´m including the class (file) that i want to get, so i get the compiling error:

--------------------Configuration: main - Win32 Debug--------------------
Compiling...
main.cpp
c:\__trabajo\prog\__cpp\clases\_pruebas\utm\problema1\x.hpp(2) : warning C4182: #include nesting level is 363 deep; possible infinite recursion
c:\__trabajo\prog\__cpp\clases\_pruebas\utm\problema1\x.hpp(2) : fatal error C1076: compiler limit : internal heap limit reached; use /Zm to specify a higher limit
Error executing cl.exe.

main.obj - 1 error(s), 1 warning(s)


i´ve put the title &quot;OOP Probrem&quot; because i think that is not a c++ error.
How can i make this work? As you see i want to use the take the private data of one of the classes, change it with an algoritmn and put the result in the private data of the other class. And the same in the opposite way.
I want to transform one class to the other one.
Maybe the only way is to use &quot;friends&quot;. i ´d be very grateful if you can help me.
Anyway thank you for your patience.
bye
 
Don't include X.hpp in Y.hpp, and don't include Y.hpp in X.hpp. You don't need them there.
 
You should make each Your .hpp file in a header (*.h, for example) and implementation (*.cpp, for example). Then You can #include *.h - Files only and only in *.cpp - files, and in *.h - files You can use declarations only (I mean, class X; etc).
If You use class functions in Your file, You need include at least header of that class. Such construction
class Y
{
...
void toX(X &x)
{
x.setI(function(i));
}
...
};
needs a header included, and such:
class Y
{
...
void toX(X &x);
...
};
needs only class X; before (You must, of course, write in Your *.cpp File the next:

#include &quot;C:\__trabajo\prog\__cpp\clases\_pruebas\UTM\problema1\Y.h&quot;
#include &quot;C:\__trabajo\prog\__cpp\clases\_pruebas\UTM\problema1\X.h&quot;

void Y::toX(X &x)
{
x.setI(function(i));
}

Generally, make each header so:
#ifndef _HEADER_MY_FIRST //take a non-repeating string for each header!!!
#define _HEADER_MY_FIRST
//All of Your header
....
....
....

#endif

Such technique prevents headers from repeating by #include - that is Your problem.
You can try that by Your .hpp files too, but it works with *.hpp - files not allways.
 
clearly the problem is that to create an x object you need to create a y object, but to create an y object you need to create a x object, but to create an x object you need to create a y object.... an infinite loop, using all heap memory..... redo your program removing this recursion.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top