0

I am fairly new to C++ and am trying to write a little program where a string from a member function is stored into a public vector array.

I have written two functions where each stores a message into this vector, however when attempting to output the contents in main, nothing is printed.

classTest.h:

#include <iostream>
#include <vector>

class Test
{
public:
    //std::vector<std::string> buffer
    std::vector<std::string> buffer;

    void msg1();
    void msg2();
};

classTest.cpp:

#include <iostream>
#include <vector>
#include "classTest.h"

void Test::msg1()
{
    Test test;
    std::string message = "HELLO";
    test.buffer.push_back(message);
}

void Test::msg2()
{
    Test test;
    std::string message = "WORLD";
    test.buffer.push_back(message);

}

int main()
{
    Test *test = new Test;

    test->msg1();
    test->msg2();
    std::cout << test->buffer.size() << std::endl;
    //std::cout << test->buffer.at(1) << std::endl;
    //std::cout << test->buffer.at(2) << std::endl;

    return 1;
}

Size output is 0, and if I attempt test->buffer.at(0) I get an out of range error.

All I want to do is have the msg1 and msg2 functions write into the public buffer and have it output in console in main. Any help will be appreciated.

3 Answers3

0

You need to read a good book on C++, it appears.

Your member functions operate on a local variable named test which goes away when the functions end.

Here's the updated ones which hopefully will behave correctly.

void Test::msg1()
{
    std::string message = "HELLO";
    buffer.push_back(message);
}

void Test::msg2()
{
    std::string message = "WORLD";
    buffer.push_back(message);
}
Tanveer Badar
  • 5,438
  • 2
  • 27
  • 32
0

The issue is that constructer to your class, inside your class, and that just wouldn't work. Instead you go directly to your class.

void Test::msg1()
{
    std::string message = "HELLO";
    Test::buffer.push_back(message);
}

void Test::msg2()
{
    std::string message = "WORLD";
    Test::buffer.push_back(message);
}
appledoes
  • 113
  • 9
0

test instance in methods msg1() and msg2() are local to those methods only and go out-of-scope when those methods exit.

So, you would want to do this:

buffer.push_back( message );

In addition, an std::vector is a zero-based container so the first element would be at(0) and so on.

Another thing is that you're leaking memory. You allocate memory using new operator but do not delete it after use. So, you need to take care of that also. Better yet, use a smart pointer e.g. std::unique_ptr with std::make_unique for automatic memory management.

Here's a complete working example (live):

#include <iostream>
#include <vector>

class Test
{
public:
    std::vector<std::string> buffer;

    void msg1();
    void msg2();
};

void Test::msg1()
{
    std::string message = "HELLO";
    buffer.push_back(message);
}

void Test::msg2()
{
    std::string message = "WORLD";
    buffer.push_back(message);
}

int main()
{
    Test *test = new Test;  // memory allocation

    test->msg1();
    test->msg2();
    std::cout << test->buffer.size() << std::endl;
    std::cout << test->buffer.at(0) << std::endl;
    std::cout << test->buffer.at(1) << std::endl;

    delete test;            // memory deallocation
    return 0;
}

Output:

2    
HELLO    
WORLD

A relevant thread you might want to read: C++: "std::endl" vs "\n"

Azeem
  • 11,148
  • 4
  • 27
  • 40