lundi 4 janvier 2021

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 ;
    }
};

//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;

Aucun commentaire:

Enregistrer un commentaire