noob-probleme vector und iterator
-
Bin neu in c++ und hab zu übungszwecken mal nen Vector und nen Vectoriterator (zumindest so halb) gescripted. Aber hab Probleme die Dateien zusammenzufügen bekomme ständig Fehlermeldungen, weil ja sowohl der Iterator eine Instanz des Vectors als member hat und umgekehrt ein Iterator zurückgeliefert wird.
Hier mal meine Dateien:
VectorIterator.h
class Vector; class VectorIterator { private: int index; Vector *target; public: VectorIterator (Vector *v); bool hasMoreElements (); };
VectorIterator.cpp
#include "VectorIterator.h" bool VectorIterator::hasMoreElements () { if (index < target.getLength()) { return true; } return false; } VectorIterator::VectorIterator (Vector *v) { index = -1; target = v; };
Vector.h
class VectorIterator; class Vector { private: int maxLength; int *content; int count; public: static const int EXTEND = 10; static const int DEFAULT_SIZE = 5; Vector (); Vector (int size); ~Vector (); void push (int num); int getElementAt (int pindex); void pop (); int getLength (); VectorIterator getIterator (); };
Vector.cpp
#include "Vector.h" Vector::Vector () { Vector(DEFAULT_SIZE); }; Vector::Vector (int size) { count=-1; content = new int[size]; maxLength = size; }; Vector::~Vector () { delete [] content; } void Vector::push (int num) { count++; if (count == maxLength) { maxLength += EXTEND; int *tmp = content; content = new int[maxLength]; for (int i=0; i<count;i++) { content[i] = tmp[i]; } //delete [] tmp; } content[count] = num; }; int Vector::getElementAt (int pindex) { if (pindex > -1 && pindex <= count) { return content[pindex]; } return -1; }; void Vector::pop () { content[count] = 0; count--; }; int Vector::getLength () { return count+1; }; VectorIterator Vector::getIterator () { VectorIterator v(this); return v; }; const int Vector::EXTEND; const int Vector::DEFAULT_SIZE;
main.cpp
#include <iostream> using namespace std; #include "VectorIterator.h" #include "Vector.h" int main(){ Vector v(6); VectorIterator vi = v.getIterator(); cout<<vi.hasMoreElements()<<endl; return 0; };
hier die Files zum Download:
http://www.lifted.de/files/test.rar
-
keiner ne Idee wo der Fehler ist??
-
so habe jetzt ein bisschen umgebaut (ob richtig oder nicht weiß ich nicht) bekomme immer folgenden Fehler:
../VectorIterator.cpp:13: request for member `getLength' in `this->VectorIterator::target', which is of non-aggregate type
Vector.h
#ifndef __VECTOR #define __VECTOR class VectorIterator; class Vector { private: int maxLength; int *content; int count; public: static const int EXTEND = 10; static const int DEFAULT_SIZE = 5; Vector (); Vector (int size); ~Vector (); void push (int num); int getElementAt (int pindex); void pop (); int getLength (); VectorIterator getIterator (); }; #endif
Vector.cpp
#include "Vector.h" #include "VectorIterator.h" Vector::Vector () { Vector(DEFAULT_SIZE); }; Vector::Vector (int size) { count=-1; content = new int[size]; maxLength = size; }; Vector::~Vector () { delete [] content; } void Vector::push (int num) { count++; if (count == maxLength) { maxLength += EXTEND; int *tmp = content; content = new int[maxLength]; for (int i=0; i<count;i++) { content[i] = tmp[i]; } //delete [] tmp; } content[count] = num; }; int Vector::getElementAt (int pindex) { if (pindex > -1 && pindex <= count) { return content[pindex]; } return -1; }; void Vector::pop () { content[count] = 0; count--; }; int Vector::getLength () { return count+1; }; VectorIterator Vector::getIterator () { VectorIterator v(*this); return v; }; const int Vector::EXTEND; const int Vector::DEFAULT_SIZE;
VectorIterator.h
#ifndef __VECTOR_ITERATOR #define __VECTOR_ITERATOR class Vector; class VectorIterator { private: int index; Vector *target; public: VectorIterator (Vector v); bool hasMoreElements (); }; #endif
VectorIterator.cpp
#include "VectorIterator.h" #include "Vector.h" VectorIterator::VectorIterator (Vector v) { index = -1; *target = v; }; bool VectorIterator::hasMoreElements () { if (index < target.getLength()) { // hier meldet der Compiler den Fehler return true; } return false; }
main.cpp
#include <iostream> using namespace std; #include "VectorIterator.h" #include "Vector.h" int main(){ Vector v(6); VectorIterator vi = v.getIterator(); cout<<vi.hasMoreElements()<<endl; return 0; };
Danke schonmal im vorraus.
-
target ist ein pointer auf Vector.
es muss also heissenif (index < target->getLength()) {
Kurt
-
vielen dank.