a string object for a class?

Ok, so I have a class, and one of its "variables" is a string object. The constructor for the class takes a string argument, and assigns it to that string object. However, it doesnt seem like it wants to do that, because when i try output it, nothing comes out...
Perhaps your code is wrong. It's very difficult to guess without seeing any of the code.
Last edited on
mk, well this is the basic structure:

class meow
{
public:
string bat;
void outputName();
}

meow::meow(string name):
bat(name)
{}

void meow::outputName()
{
cout << bat;
}

int main
{
meow dot("Harold");
dot.outputName();
return 0;
}
Last edited on
the program runs fine, it just puts a space where I want the string object to go
This code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include<string>
using namespace std;


class meow
{
public:
  meow(string);
string bat;
void outputName();
}

meow::meow(string name):
bat(name)
{}

void meow::outputName()
{
cout << bat;
}

int main()
{
meow dot("Harold");
dot.outputName();
return 0;
}


gives the following output:

Harold


What were you expecting it to output?
Slightly modified code (yours doesn't compile under g++, Suse) prints Harold.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<iostream>
#include<fstream>
#include<string>

using namespace std;

class meow
{
        public:
                meow(string name);
                string bat;
                void outputName();
};

meow::meow(string name):
        bat(name)
{}

void meow::outputName()
{
        cout << bat;
}

int main()
{
        meow dot("Harold");
        dot.outputName();
        return 0;
}
hmm.... would it make any diffference if it was

int main()
{
meow* pMeow;
pMeow = &meow("Harold");
pMeow->outputName();
return 0;
}
?

I didnt think it being a pointer would make a difference, but maybe so...
@tfityo: whats the difference between that and mine, except the inclusion of <fstream>? (and a few mistakes i made typing up the example >.>)
pMeow = &meow("Harold");

So exactly which object does pMeow point to here? A temporary object that ceases to exists as soon as this line of code has executed. It's nothing to do with the pointer- it's to do with the fact that temporary objects stop existing very quickly.
Last edited on
... wait what? I'm not too wonderful with pointers, so can you tell me how to fix it? DX
Make the object properly, so it exists.


1
2
3
4
5
6
7
8
int main()
{
meow* pMeow;
meow someObject("Harold");
pMeow = &someObject;
pMeow->outputName();
return 0;
}

well, the way it works, it goes into an if statement to assign the pointer based on user input, so if i do that, then wouldnt the object cease to exist after the scope ends?
It's up to you to make sure objects exist as long as you need them to.

This meow* pMeow = &meow("Harold"); creates a temporary that dies pretty much instantly, and leaves you with a pointer that points at useless memory. Don't do this.

This meow someObject("Harold"); creates one that lasts until the end of scope. If you need it above that scope, create it above that scope.

This meow* pMeow = new meow("Harold"); creates one that lasts until you delete it yourself.

Frequently, you can avoid using new by planning your code properly. When that's more trouble than it's worth, or you run out of room on the stack, you've got new.

Last edited on
Hmm... I'll try that
It worked!! :D Thank you for your help! :3
Topic archived. No new replies allowed.