0

I have this code:

#include <iostream>
#include <memory>
#include <vector>

class Test {
private:
    int data;
public:
    Test() : data{} { std::cout << "\tctor Test(" << data << ")\n"; }
    Test(int data) : data{ data } {
        std::cout << "\tctor Test(" << data << ")\n";
    }
    int get_data() const { return data; }
    ~Test() { std::cout << "\tdtor Test(" << data << ")\n"; }
};


std::unique_ptr<std::vector<std::shared_ptr<Test>>> make() {

    std::unique_ptr<std::vector<std::shared_ptr<Test>>> ptr = std::make_unique<std::vector<std::shared_ptr<Test>>>();

    return ptr;
}

void fill(std::vector<std::shared_ptr<Test>>& vec, int num) {

    for (int i = 1; i <= num; i++) {

        std::cout << "Number " << i << ": " <<std::endl;
        int a;
        std::cin >> a;

        std::shared_ptr<Test> ptr(new Test(a));
        vec.push_back(std::move(ptr));
    }
}

void display(const std::vector<std::shared_ptr<Test>>& vec) {

    
    for (unsigned i = 0; i < vec.size(); i++)
    {
        
        std::cout << vec[i] << std::endl;
    }
}

int main()
{
    std::unique_ptr<std::vector<std::shared_ptr<Test>>> vec_ptr;
    vec_ptr = make();
    std::cout << "How many data points: ";
    int num;
    std::cin >> num;
    fill(*vec_ptr, num);
    display(*vec_ptr);
    return 0;

}

I want to print out my vector with the display function, but I only get the adress. I only got something like this:

How many data points: 3
 Number 1:
 1
         ctor Test(1)
 Number 2:
 2
         ctor Test(2)
 Number 3:
 3
         ctor Test(3)
 014A5820
 014A5850
 014A9460
         dtor Test(1)
         dtor Test(2)
         dtor Test(3)

How do i get the actual numbers? Can someone please tell me what I do wrong.

Botje
  • 26,269
  • 3
  • 31
  • 41
patro98
  • 5
  • 1

2 Answers2

1

Your Test class has the get_data() function to get the number, so use that.

Changing the line

std::cout << vec[i] << std::endl;

to

std::cout << vec[i] << " : " << vec[i]->get_data() << std::endl;

will give you something like this:

0x18f4190 : 1
0x18f41f0 : 2
0x18f41d0 : 3
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
0

Your vector contains pointers. Whether they're shared_ptr, unique_ptr, or plain dumb C-style pointers makes no difference. To get the value at the pointer you need the * operator to dereference the pointer.

std::cout << *vec[i] << std::endl;

In this case you haven't defined an operator<< for the class the pointer points at, so that won't work. But you can do as MikeCAT suggests and call a function on the pointer.

std::cout << vec[i]->get_data() << std::endl;
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • This will lead to compilation error because how to print `Test` via `ostream` is not defined. How to define: [c++ - How can I use cout << myclass - Stack Overflow](https://stackoverflow.com/questions/2981836/how-can-i-use-cout-myclass) – MikeCAT Jun 22 '21 at 14:00
  • @MikeCAT yes I realized that and was correcting just as you made your comment. Thanks for the additional link. – Mark Ransom Jun 22 '21 at 14:03