0

I'm new to this website and a bit new in programming. I'm using vector for the first time and I want to print content of it but I'm getting addresses instead of normal words. I don't know how to do that in other way than this.

My vector:

vector<Component*> vect;

and my code for printing:

void Frame::print() {
    for (int i = 0; i < vect.size(); i++)
        cout << vect[i] << endl;
}
Jarod42
  • 203,559
  • 14
  • 181
  • 302
hypr2
  • 43
  • 9
  • Your vector stores pointer of your class, so you need to dereference it before printing. – florgeng Apr 19 '17 at 20:35
  • If you really think it has something to do with the vector, try without it and see if anything changes. – juanchopanza Apr 19 '17 at 20:35
  • how do I do that dereference? – hypr2 Apr 19 '17 at 20:37
  • There we go. This link will work better: http://en.cppreference.com/w/c/language/operator_member_access#Dereference A more correct for C++ version is a bit harder to headwrap: http://en.cppreference.com/w/cpp/language/operator_member_access#Built-in_indirection_operator – user4581301 Apr 19 '17 at 20:42
  • @hypr2 it depends on how `Component` is suppose to be printed – Slava Apr 19 '17 at 20:43

2 Answers2

3

You are storing pointers in your vector. The value of a pointer is a memory address, therefore you are seeing addresses in your output. You need to dereference the pointers to access the actual Component objects:

void Frame::print()
{
    for (int i = 0; i < vect.size(); i++)
    {
        Component *component = vect[i];
        cout << *component << endl; // <-- not the extra '*'
    }
}

In order for this to work, an operator<< overload for Component is also required:

ostream& operator<<(ostream &out, const Component &comp)
{
    // print comp values to out as needed...
    return out;
}

Recommended reading:

Operator overloading

You also need to study up on pointers and references in general.

Community
  • 1
  • 1
Christopher Pisz
  • 3,757
  • 4
  • 29
  • 65
  • New minor nag: point the reader here: http://stackoverflow.com/questions/4421706/operator-overloading God knows what crap they might accidentally stumble into with a Google search. – user4581301 Apr 19 '17 at 20:51
1

vect is a vector (aka a lump of contiguous storage) of Component * (aka Component pointers) that is it's a chunk of memory with addresses of other chunks of memory which the compiler will treat as a Component class object. Printing those addresses via cout will just give you a big list of meaningless numbers.

What I suspect you want to do is probably not store a vector of Component pointers at all and just store a vector of Components. It's frowned upon in C++ these days to store raw pointers unless you know exactly what you are doing. If you really do want pointers you should use a vector of std::unique_ptr and std::make_unique.

Once you start trying to print Components rather than addresses of them you will most likely see that there is no << operator for Component. You will need to write one. Something like

std::ostream& operator<<(std::ostream &stream, const Component&component)
{
  stream << component.string_member;
  return stream;
}
1stCLord
  • 860
  • 5
  • 14
  • Who is frowning and what is their justification for their frowny faces? cache misses vs construction time? – Christopher Pisz Apr 19 '17 at 20:44
  • Yeah I knew as soon as I hit commit i'd messed up the operator overload, fixed now sorry. – 1stCLord Apr 19 '17 at 20:49
  • The main reason it's frowned upon to use raw pointers is it defies RAII, which means leaks are inevitable unless you know every single line of code between new and delete doesn't unwind the stack, which is very difficult to prove in real code. – 1stCLord Apr 19 '17 at 20:54
  • So, you'd turn your frown upside down if I had collections of shared_ptr to domain objects? – Christopher Pisz Apr 19 '17 at 20:56
  • Well shared_ptr's require synchronisation on their reference count so should be used sparingly and shared ownership *should* be a rare paradigm. – 1stCLord Apr 19 '17 at 21:03