简体   繁体   中英

Stuck in pointers, printing the vector of class objects with vector of pointers to the output console

I am new to programming and stuck with an example.

  1. I have created a class ptr_generator that creates an array of pointers through its constructor. Here these pointers point to a vector of doubles.
  2. Then a vector myvec of class ptr_generator type is created.

I am having problems printing out the initial vector of doubles that the pointers are pointed to.

Can someone please help me in figuring this out?

/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>
#include <vector>
#include <typeinfo>
#include <array>
using namespace std;

struct ptr_generator 
{
    ptr_generator ( std::vector<double> v1 , std::vector<double> v2)
    {
        std::array<std::vector<double>* , 2> ptr_arr {{&v1, &v2}};
    }
};

//Myarr is a vector of ptr_generator class objects

using myvec = std::vector< ptr_generator> ;

myvec myfunction (std::vector<double> v1, std::vector<double> v2 )
{
    myvec MyVector;
    
    MyVector.push_back(ptr_generator(v1, v2));
    
    std::cout << MyVector[0]<< std::endl;
    
    return MyVector;
}


int main()
{
    
    std::vector<double> vec1 { 1.0, 2.0, 2.1, 3.1};
    std::vector<double> vec2 { 1.0, 4.0, 6.1, 2.1};
    
    myfunction (vec1, vec2);
  
    
    
    
    return 0;
}

Error:

main.cpp: In function ‘myvec myfunction(std::vector<double>, std::vector<double>)’:
main.cpp:33:15: error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream}’ and ‘__gnu_cxx::__alloc_traits >::value_type {aka ptr_generator}’)
     std::cout << MyVector[0]<< std::endl;

There's a lot going on here that's not going to work.

First off, myvec , and consequently MyVector is a std::vector<ptr_generator> , we can agree on that, yes?

So when you write std::cout << MyVector[0]; then what you're really doing is calling cout on a ptr_generator . But ptr_generator has no operator<< that can operate on it. You're going to have to write one.

When you do that, you'll find that your ptr_generator class needs a lot of work. First off, you'll want a member variable to store the data you passed in. But moreover, you cannot take and store the address of function arguments, as you do in {{&v1, &v2}} . You need to make copies of them if you want to store them.

You need to overload the operator<< function for ptr_generator .

The std::array is not stored outside of the constructor, so we'll add that as a member of the ptr_generator class. Then we will do two loops; One to loop over the array and one to loop over the vectors. As the array holds vector pointers, we need to dereference them before iterating them.

#include <iostream>
#include <vector>
#include <array>

using namespace std;

struct ptr_generator
{
    using vec_dbl = vector<double>;
    ptr_generator(vec_dbl& v1, vec_dbl& v2)
        : storage_({ &v1, &v2 })
    {}

    friend ostream& operator<<(ostream& os, const ptr_generator& pg)
    {
        for (auto v : pg.storage_)
        {
            for (auto d : *v)
            {
                os << d << ' ';
            }
            os << '\n';
        }
        return os;
    }
private:
    array<vec_dbl*, 2> storage_;
};

using myvec = vector<ptr_generator>;

myvec myfunction(ptr_generator::vec_dbl & v1, ptr_generator::vec_dbl & v2)
{
    myvec myvec;

    myvec.push_back(ptr_generator(v1, v2));

    cout << myvec[0] << endl;

    return myvec;
}

int main()
{
    vector<double> vec1{ 1.0, 2.0, 2.1, 3.1 };
    vector<double> vec2{ 1.0, 4.0, 6.1, 2.1 };

    myfunction(vec1, vec2);
}

Another thing to note, is that I changed the constructor and myfunction to take the vectors by reference. As they were, they were taken by value and so getting copied between functions. Then in the constructor when you took the addresses of the vectors, you were taking the addresses of the copied vectors not the originals. This is undefined behaviour as those vectors will go out of scope.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM