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

A

ahrimen

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 :)
 
M

Mike Wahler

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
 
T

TB

(e-mail address removed) 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>
 
M

Marcus Kwok

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.
 
A

ahrimen

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?
 

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
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top