简体   繁体   English

卡在指针中,打印 class 对象的向量以及指向 output 控制台的指针向量

[英]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.我创建了一个 class ptr_generator,它通过其构造函数创建一个指针数组。 Here these pointers point to a vector of doubles.这里这些指针指向一个双精度向量。
  2. Then a vector myvec of class ptr_generator type is created.然后创建一个 class ptr_generator 类型的向量 myvec。

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?首先, myvec ,因此MyVector是一个std::vector<ptr_generator> ,我们可以同意,是吗?

So when you write std::cout << MyVector[0];所以当你写std::cout << MyVector[0]; then what you're really doing is calling cout on a ptr_generator .那么你真正要做的是在ptr_generator上调用cout But ptr_generator has no operator<< that can operate on it.但是ptr_generator没有operator<<可以对其进行操作。 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.当你这样做时,你会发现你的ptr_generator class 需要做很多工作。 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}} .首先,您需要一个成员变量来存储您传入的数据。但是,您不能像在{{&v1, &v2}}中那样获取和存储 function arguments 的地址。 You need to make copies of them if you want to store them.如果要存储它们,则需要制作它们的副本。

You need to overload the operator<< function for ptr_generator .您需要为 ptr_generator 重载operator<< 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. std::array不存储在构造函数之外,因此我们将其添加为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.另一件需要注意的事情是,我更改了构造函数和myfunction以通过引用获取向量。 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.这是未定义的行为,因为这些向量将从 scope 中取出 go。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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