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

Discussion in 'C++' started by ahrimen@gmail.com, Mar 9, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. Mike Wahler Guest

    <> wrote in message
    news:...
    > 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
    Mike Wahler, Mar 9, 2006
    #2
    1. Advertising

  3. TB Guest

    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
    TB, Mar 9, 2006
    #3
  4. Marcus Kwok Guest

    <> 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
    Marcus Kwok, Mar 9, 2006
    #4
  5. Guest

    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, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page