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.