Accessing variables in nested classes
-
I am doin the following...
Class A
{
public:
void out_func1();
class B
{
public:
string path;
in_func1(); //Assigning the value of path in func1
};
};How do I use the value of path in 'out_func1()'?? I tried ...
void A::out_func1()
{
B obj;
obj.path;
ofstream myfile;
myfile.open (obj.path.c_str(), ios::app);
...
}This causes problem during runtime and the program crashes Please let me know how to rectify this. THANKS.
-
I am doin the following...
Class A
{
public:
void out_func1();
class B
{
public:
string path;
in_func1(); //Assigning the value of path in func1
};
};How do I use the value of path in 'out_func1()'?? I tried ...
void A::out_func1()
{
B obj;
obj.path;
ofstream myfile;
myfile.open (obj.path.c_str(), ios::app);
...
}This causes problem during runtime and the program crashes Please let me know how to rectify this. THANKS.
It seems you are using it correctly except that you are not setting the file path so the IO to the file will fail. Are you sure program is crashing in this function? Have you actually debugged to see where is the crash? -Saurabh
-
I am doin the following...
Class A
{
public:
void out_func1();
class B
{
public:
string path;
in_func1(); //Assigning the value of path in func1
};
};How do I use the value of path in 'out_func1()'?? I tried ...
void A::out_func1()
{
B obj;
obj.path;
ofstream myfile;
myfile.open (obj.path.c_str(), ios::app);
...
}This causes problem during runtime and the program crashes Please let me know how to rectify this. THANKS.
Well, since you haven't initialized the
obj.path
variable, not wonder if the application doesn't behave correctly. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
I am doin the following...
Class A
{
public:
void out_func1();
class B
{
public:
string path;
in_func1(); //Assigning the value of path in func1
};
};How do I use the value of path in 'out_func1()'?? I tried ...
void A::out_func1()
{
B obj;
obj.path;
ofstream myfile;
myfile.open (obj.path.c_str(), ios::app);
...
}This causes problem during runtime and the program crashes Please let me know how to rectify this. THANKS.
I am sorry. This is how it is... My Class is defined in newclass.h as...
Class A
{
public:
void out_func1();
void out_func2();
class B
{
public:
string path;
in_func1();
};
};I then Get the path at intialize.cpp as follows....
void A::out_func1()
{
B obj;
obj.path="c:\folder\file1.txt";
}Then I try to open the file to write in write.cpp...
void A::out_func2()
{
B obj;
ofstream myfile;
myfile.open (obj.path.c_str(), ios::app);
...
}This crashes.I have included the header initialise.h too. Please let me know were I could have gone wrong. I think I am having problem with the scope of the varible. But I am not sure. THANKS.
-
I am sorry. This is how it is... My Class is defined in newclass.h as...
Class A
{
public:
void out_func1();
void out_func2();
class B
{
public:
string path;
in_func1();
};
};I then Get the path at intialize.cpp as follows....
void A::out_func1()
{
B obj;
obj.path="c:\folder\file1.txt";
}Then I try to open the file to write in write.cpp...
void A::out_func2()
{
B obj;
ofstream myfile;
myfile.open (obj.path.c_str(), ios::app);
...
}This crashes.I have included the header initialise.h too. Please let me know were I could have gone wrong. I think I am having problem with the scope of the varible. But I am not sure. THANKS.