0

Im having an issue where i cannot manage to print the set of coords which are held within list 'vertex' vertices.. To explain, 'Vertex' is a class which basically holds two coordinates that make a vertex (where two lines of a shape meet). The issue is within the second for loop that is meant to print out the coordinates held within the list.. Can anyone help me in trying to get this printed out ? ive tried numerous ways and errors have always given me a problem so far.. Code is below, Thanks for the help!

INT MAIN

int main(){
//obj
Console con;
RandomNumber rand;
Vertex vertex;

//Declarations
list<Vertex>vertices; 
//list<Vertex>::iterator ii;


//set Vertex coords and push into list
for (int i = 0; i <= 8;  i++){
vertices.push_back(Vertex(rand.random(0, 10), rand.random(0, 10)));
}

//iterate through the list outputting a char at each vertex (coords)
for (list<Vertex>::iterator ii = vertices.begin(); ii != vertices.end(); ii++){
    cout << vertices[ii];
}


system("pause");

}

VERTEX CLASS

#pragma once
class Vertex
{
    int x;
    int y;
public:
    Vertex(int x = 10, int y = 10);
    ~Vertex();
    int getX() const;
    int getY() const;
    void setX(unsigned x);
    void setY(unsigned y);
    bool operator== (const Vertex &p2) const;
};

    #include "Vertex.h"
Vertex::Vertex(int x, int y)
{
    this->x = x;
    this->y = y;
}
Vertex::~Vertex(){}
int Vertex::getX() const {
    return x;
}
int Vertex::getY() const {
    return y;
}
void Vertex::setX(unsigned x) {
    this->x = x;
}
void Vertex::setY(unsigned y) {
    this->y = y;
}
bool Vertex::operator== (const Vertex &point) const {
    return (this->x == point.getX()) && (this->y == point.getY());
}
Satain
  • 9
  • 1
  • 7

3 Answers3

0
//iterate through the list outputting a char at each vertex (coords)
for (list<Vertex>::iterator ii = vertices.begin(); ii != vertices.end(); ii++){
    cout << vertices[ii];
}

This is essentially wrong. You need to use the iterator as if it was a pointer to the ii-th object in the list.

for (list<Vertex>::iterator ii = vertices.begin(); ii != vertices.end(); ii++){
    cout << ii->getX() << ',' << ii->getY(); 
    // if you have an overload for 'operator <<(std::ostream&, Vertex)' you can also do 
    cout << *ii 
}
TheHube
  • 752
  • 1
  • 10
  • 23
0

The << operator doesn't work for custom defined types. You need to specify how the << operator behaves when confronted with your Vertex class.

You'll need to generate a signature that understands your vertex class. Take the following function:

std::ostream& operator<<(std::ostream &os, const Vertex &vertex) {
    os << "Node: [" << vertex.getX() << ", " << vertex.getY() << "]";
    return os;
}

This will allow you to use the code that you have written.

Tyler Jandreau
  • 4,245
  • 1
  • 22
  • 47
0

You need to add a function to you class:

declaration:

friend std::ostream& operator <<(std::ostream &os,const Vertex &obj);

and implementation:

std::ostream& operator<<( std::ostream &os,const Vertex &obj )
{
    os << << obj.getX() << ',' << obj.getY();
    return os;
}

this will declare a overloading of << operator as seen here.

You must also change the iteration to this:

for (list<Vertex>::iterator ii = vertices.begin(); ii != vertices.end(); ii++){
    cout << *ii;
}
Community
  • 1
  • 1
Lucian
  • 3,407
  • 2
  • 21
  • 18