Question

I'm having this weird problem where the compiler is highlighting "=" and "!=" as errors claiming that there are no matching operands but I have no idea how. Here's my code:

#pragma once
#include "Console.h"
#include "RandomNumber.h"
#include "Element.h"
#include "Flotsam.h"
#include "vector"
#include <list>
#include <iostream>

using namespace std;

#define NUMBER 10

int main()
{
    Console console;
    RandomNumber rnd;


    vector<Element*> flotsam;


    for(int i = 0; i < NUMBER; i++)
    {
        flotsam.push_back(new Element(rnd, console));
    }

    vector<Element>::iterator ptr;

    ptr = flotsam.begin();

    while(ptr!=flotsam.end())
    {
        ptr->printAt();
        ptr++; 
    }
    Sleep(1000);
    console.clear();

}
Was it helpful?

Solution

Your vector has a different type, the iterator should be

vector<Element*>::iterator ptr;
//            ^

OTHER TIPS

flotsam is an std::vector<Element*>, so you need

vector<Element*>::iterator ptr;

You will also need to de-reference the pointer when accessing it via the iterator:

(*ptr)->printAt();

Alternatively, you can greatly simplify your code by using a vector of Element objects:

vector<Element> flotsam;

Maybe even better solution would be (C++11):

auto ptr = flotsam.begin();

It will be stable against vector element type.

And yes, looking at iterator usage you probably should have:

vector<Element> flotsam;

Since the iterator type iterates over a vector<Element>, flotsam should also be a vector<Element>. There's no apparent need for a container of pointers here; a container of objects seems appropriate. To add elements, just use pushback(Element(rnd, console)); no new needed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top