By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,887 Members | 1,148 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,887 IT Pros & Developers. It's quick & easy.

How to use a vector data member when the class object is a vector?

P: n/a
Hi, First I'll state my over all goal = a text based game with several
rooms that have several exits as my first real program that I've done
without the help of a book.

I can make a normal vector object out of a custom class, I can even use
a single object of a class that has vector data members, but If my
vector object of a custom class has a vector data member my program
crashs not when I instaitant it, but if I try calling any of the member
functions, heres the code:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class CMine
{
private:
string m_yo;
vector<string> m_var;
public:
CMine(string yo = "Hello.") : m_yo(yo){m_var[0]=m_yo;}
void Show(){ cout << m_var[0] << endl; }
};
int main()
{
vector<CMine> test;

test[0].Show();

system("pause");
return 0;
}

very basic stuff, but i just dont get what i'm doing wrong or if this
is even possible.

Thanks in advace :)

Mar 9 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a

<ah*****@gmail.com> wrote in message
news:11**********************@e56g2000cwe.googlegr oups.com...
Hi, First I'll state my over all goal = a text based game with several
rooms that have several exits as my first real program that I've done
without the help of a book.

I can make a normal vector object out of a custom class, I can even use
a single object of a class that has vector data members, but If my
vector object of a custom class has a vector data member my program
crashs not when I instaitant it,
But that's where the trouble begins. You have 'undefined behavior'.
but if I try calling any of the member
functions, heres the code:
#include <iostream>
#include <string>
#include <vector>

using namespace std;

class CMine
{
private:
string m_yo;
vector<string> m_var;
public:
CMine(string yo = "Hello.") : m_yo(yo){m_var[0]=m_yo;}
'mvar[0]' does not yet exist, so you can't assign it a value.

Write:

m_var.push_back(m_yo);
void Show(){ cout << m_var[0] << endl; }
};
int main()
{
vector<CMine> test;

test[0].Show();

system("pause");
return 0;
}

very basic stuff, but i just dont get what i'm doing wrong or if this
is even possible.


You're forgetting to first make a place to store your data.
Review the documentation for the 'vector' type.

-Mike
Mar 9 '06 #2

P: n/a
TB
ah*****@gmail.com skrev:
Hi, First I'll state my over all goal = a text based game with several
rooms that have several exits as my first real program that I've done
without the help of a book.

I can make a normal vector object out of a custom class, I can even use
a single object of a class that has vector data members, but If my
vector object of a custom class has a vector data member my program
crashs not when I instaitant it, but if I try calling any of the member
functions, heres the code:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class CMine
{
private:
string m_yo;
vector<string> m_var;
public:
CMine(string yo = "Hello.") : m_yo(yo)
{m_var[0]=m_yo;}
m_var.push_back(m_yo);
void Show(){ cout << m_var[0] << endl; }
};


<snip>

--
TB @ SWEDEN
Mar 9 '06 #3

P: n/a
ah*****@gmail.com <ah*****@gmail.com> wrote:
Hi, First I'll state my over all goal = a text based game with several
rooms that have several exits as my first real program that I've done
without the help of a book.

I can make a normal vector object out of a custom class, I can even use
a single object of a class that has vector data members, but If my
vector object of a custom class has a vector data member my program
crashs not when I instaitant it, but if I try calling any of the member
functions, heres the code:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class CMine
{
private:
string m_yo;
vector<string> m_var;
public:
CMine(string yo = "Hello.") : m_yo(yo){m_var[0]=m_yo;}
At this point, m_var is a vector with no elements, yet you try and
access the 0th element. You need to do a m_var.push_back(m_yo); in
order to add the string to the vector.
void Show(){ cout << m_var[0] << endl; }
};
int main()
{
vector<CMine> test;

test[0].Show();
Same issue here. test is an empty vector, yet you try and access the
0th element.
system("pause");
return 0;
}

very basic stuff, but i just dont get what i'm doing wrong or if this
is even possible.

Thanks in advace :)


--
Marcus Kwok
Mar 9 '06 #4

P: n/a
Thanks guys, I did have to use m_var.push_back(m_yo) and I have to tell
the complier how many "test" vectors to make i.e. = vector<CMine>
test(1);, using .push_back for test wasn't working untill I did that.

Also, I get the warning "Warning W8026 13: Functions taking
class-by-value argument(s) are not expanded inline in finction
CMine::CMing(string)", what does this mean?

Mar 9 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.