+ 2

why is this giving me 'no output'?

#include <iostream> #include <vector> #include <iterator> #include <set> #include <algorithm> namespace VECTOR1{ // going to figure out this hiding information stuff :/ class Vector1{ public: Vector1(){ }; ~Vector1(){ std::cout<<"vector 1 destroyed.";}; enum vect{ x,y,z,w }; std::vector<int> getV(){ return m_v; }; private: //put container in private visibility std::vector<int> m_v; }; } int main() { using namespace VECTOR1; Vector1 v1; std::vector<int> vect1; vect1=v1.getV(); vect1[0]=v1.x; vect1[1]=v1.y; vect1[2]=v1.z; vect1[3]=v1.w; std::cout<<"hello World!"; return 0; }

5th Nov 2018, 2:44 AM
Brandon Autry
Brandon Autry - avatar
3 Antworten
+ 3
You are getting an exception trying to access vect1[0] as vect1 has no elements. Either allocate 4 elements initially or add each one individually. For more info, check this out: https://mobile.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c4027/C-Tutorial-A-Beginners-Guide-to-stdvector-Part-1.htm
5th Nov 2018, 8:18 PM
John Wells
John Wells - avatar
+ 3
I've only used vector once and it couldn't do what I need so I did something else. I'd try: vect1.push_back(v1.x);
5th Nov 2018, 8:29 PM
John Wells
John Wells - avatar
+ 1
so if I added arguments to getV() to initialize the enum values... would that work I wonder?
5th Nov 2018, 8:26 PM
Brandon Autry
Brandon Autry - avatar