Problem with variables
-
I had posted this earlier but no reply. Sorry for posting it again 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 had posted this earlier but no reply. Sorry for posting it again 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.
out_func1() and out_func2() uses different instance of the class B. You should use same instance of class B in both functions.
nave [OpenedFileFinder]
-
I had posted this earlier but no reply. Sorry for posting it again 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.
Actually you got replies. I repeat myself: no wonder your application crashes, you're passing a uninitialized path to
myfile.open
. Hint: usage of locals where class member (or method parameter) should be used. :)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 had posted this earlier but no reply. Sorry for posting it again 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.
Where do you initialize the variable obj? That kept aside you don't call out_func1 that is initializign the obj variable. Initialize and use the same variables.
Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_
-
I had posted this earlier but no reply. Sorry for posting it again 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.
pl_kode wrote:
obj.path="c:\folder\file1.txt";
Use below code,
obj.path="c:\\folder\\file1.txt";
Regards, Paresh. -
I had posted this earlier but no reply. Sorry for posting it again 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.
pl_kode wrote:
myfile.open (obj.path.c_str(), ios::app);
obj
has not been initialized."Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne