-3

I have a class and a vector of elements of that class. I need to insert new objects in a specific position of my vector, but I can't even access the positions that I want. I tried to print my iterator but it's not working... I tested iterators with a vector of ints and it's working okay, I just can't make it work with my own class... What am I missing?

ToDo individualTask;
vector<ToDo> myToDoList;
vector<ToDo>::iterator it;

myToDoList.push_back(individualTask);
cout << myToDoList.size() << endl;

myToDoList.resize(10);

for (it = myToDoList.begin(); it != myToDoList.end(); ++it) {
    cout << *it << endl; // not working
}

I tried *it->toString() and it says that my class doesn't have the method toString

inessadl
  • 87
  • 9
  • You need to create toString for your class it doesnt exist by default (unlike in Java or C#) same for operator <<. It would help if you pasted the exact error message from your compiler. – Borgleader Sep 25 '14 at 21:14
  • Can you `cout` a `ToDo` object? Also, `resize` may invalidate iterators. – juanchopanza Sep 25 '14 at 21:14
  • it's my class. I want to create a vector of "ToDo" objects and access this elements, I'm just trying to print the iterator to check, my goal is insert in a specific position of the vector. – inessadl Sep 25 '14 at 21:18
  • @inessadl - If your `ToDo` class doesn't have an `operator <<`, that code cannot compile. – PaulMcKenzie Sep 25 '14 at 21:21
  • First of, you're not printing the `iterator` but rather the `ToDo` object in `myToDoList` vector that `it` points to. Secondly, if `ToDo` class doesn't have an overloaded `operator<<` that shows to the compiler how to print a `ToDo` object then the error you are getting is justified (i.e., Compiler doesn't know how to print a `ToDo` object). – 101010 Sep 25 '14 at 21:23
  • @inessadl - How do you "print" a `ToDo` object? Whatever you tell us, that is the same information you need to give to the compiler in the form of an overloaded `operator <<`. Otherwise, the compiler has no idea how to "print" this object. – PaulMcKenzie Sep 25 '14 at 21:25
  • 1
    You have to overload `operator<<` for your class. Check out [operator overloading](http://stackoverflow.com/questions/4421706/operator-overloading) for more information. – jliv902 Sep 25 '14 at 21:25

1 Answers1

1

The reason your code is not working is that what your iterator is pointing to (A ToDo object) has no output operator defined.

If you want to print out a ToDo object using operator<<() then you need to define one for your class.

Here is a rough example to give you the idea:

#include <string>
#include <ctime>
#include <iostream>

// Example ToDo class
class ToDo
{
    // declare friend to allow << to access your private members
    friend std::ostream& operator<<(std::ostream& os, const ToDo& todo);
private:
    std::time_t when; // unix time
    std::string what; // task description

public:
    ToDo(std::time_t when, const std::string& what): when(when), what(what) {}
    // etc...
};

// This should go in a .cpp file. It defines how to print
// Your ToDo class objects
std::ostream& operator<<(std::ostream& os, const ToDo& todo)
{
    // todo.when needs formatting into a human readable form
    // using library functions
    os << "{" << todo.when << ", " << todo.what << "}";
    return os;
}

// Now you should be able to output ToDo class objects with `<<`:

int main()
{
    ToDo todo(std::time(0), "Some stuff");

    std::cout << todo << '\n';
}
Galik
  • 47,303
  • 4
  • 80
  • 117