Trouble with Strings

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
 
P

Philip

I change the code:
int main(){
myclass m;
....

Tested the code in vc6,Output is well:
Inside Test!
Outsize Test!
 
D

DaKoadMunky

int main(){
myclass m();
m.testString();
return 0;
}

myclass m(); is a forward declaration of a function that takes no arguments and
returns an object of type myclass.

m.testString(); should have resulted in a compiler error I would think.

Apparently it did not on your platform if you ended up with a program that
produced output.

Neither Comeau or Microsoft compiler accepted it.
 
J

John Harrison

DJ said:
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

This code doesn't compile because 'myclass m();' is a function prototype not
a variable declaration. Without the parens it should produce

Inside Test!
Outside Test!

as I'm sure you realise.

You should always cut and paste code directly from your editor. Whatever
problem you are having it could easily be hidden by some other typo you have
made. Post the real code.

john
 
D

DJ

OK i admit i didnt think about that when i entered it, i send the
constructor a string
i use the string as a path to the file i am openning, but that isnt part of
this problem so i left it out... sorry

still the problem is that im losing the string inside the object, and well i
had hoped it wasnt a problem with my compiler, but apparently it is. cause i
can code this stuff in VisualStudio 6 no problem.

Thank you for the help though, I bet it will all make sence when the pizza
and soda kicks in :)

this isnt the first thing the net compiler has failed to work with, my class
templates had to be thrown out too. #$%$#%
 
D

DJ

Ok I found my problem, too much java for my own good, you were all on the
money and helped me find the error in a round about way... I was calling my
default constructor from a overloaded constructor which is a no no in c++
but common practice in java. thus instantiating a second copy of myclass,
spitting out the inside line and then returnng to the first instance and
spitting out the second line.

Thanks again, this is a great resource.

David

ahhhh, i can sleep again.......zzzzzZZzzZzZZZzz zz z z z
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top