D
DJ
I am currently having trouble with strings. I have a private string variable
in my class and i assign a value to it in the creation function as follows.
#include <string>
using std::string;
#include <iostream>
using std::cout;
using std::endl;
class myclass{
private:
string myString;
public:
myclass();
void testString();
};
myclass::myclass()
{
myString = "Test";
cout << "Inside " << myString << "!" << endl;
}
void myclass::testString()
{
cout << "Outside " << myString << "!" << endl;
}
int main(){
myclass m();
m.testString();
return 0;
}
Output looks like this.
Inside Test!
Outside !
Why am I losing the string outside of the scope of the initial function even
though it is a class member?
Thank you for any help,
David
in my class and i assign a value to it in the creation function as follows.
#include <string>
using std::string;
#include <iostream>
using std::cout;
using std::endl;
class myclass{
private:
string myString;
public:
myclass();
void testString();
};
myclass::myclass()
{
myString = "Test";
cout << "Inside " << myString << "!" << endl;
}
void myclass::testString()
{
cout << "Outside " << myString << "!" << endl;
}
int main(){
myclass m();
m.testString();
return 0;
}
Output looks like this.
Inside Test!
Outside !
Why am I losing the string outside of the scope of the initial function even
though it is a class member?
Thank you for any help,
David