Problem mit Speicherzuordnung
-
HI, bin ganz neu hier und hab gleich mal ne Frage.
Habe ein Problem bei dem Programm zur Laufzeitmessung.
Da ich mich mit c++ noch net so gut auskenne (habs im Studium erst seit 3 wochen) weis ich auch nicht was mir der Compiler damit sagen will.hier mal der Code:
Vector.h
#ifndef _VECTOR_H #define _VECTOR_H #include <iostream.h> class Vector { public: // Verwaltungsmethoden Vector(const int=10); // Konstruktor Vector(const Vector& n); // Kopierkonstruktor Vector(int n, int a[]); // Konstruktor Vector& operator=(const Vector& v); // Zuweisung ~Vector() {delete[] digits;} // Destruktor // Nur-Lese Methoden int upper() const; // Vectorl�ge void print(ostream&) const; // Ausgabe Vector operator+(const Vector& v) const; // Additionsoperator // �Aenderungsmethoden int& operator[](const int i); // Zuweisung private: // Datenelemente und interne Methoden int* digits; // Zeiger auf das Ziffernfeld unsigned ndigits; // Anzahl der Ziffern }; ostream& operator<< (ostream&, const Vector&); #endifVector.cpp
#include <iostream.h> #include "Vector.h" Vector::Vector(const Vector& n) // Standartkopierkonstruktor { ndigits=n.ndigits; digits= new int [ndigits]; for (unsigned i=0 ; i< ndigits ; i++) digits[i]=n.digits[i]; } Vector::Vector(const int n)// Konstruktor (Objekt Feld bereitstellen) { ndigits = n; digits = new int[ndigits];// Speicher reservieren for (unsigned i=0 ; i<ndigits ; i++) digits[i]=0; } int& Vector::operator[](const int i)// Methode fuer Benutzung von [] { if (2147483647 < i > -2147483648) { cout << "Wertebereich ueberschritten...Wert auf 10 gesetzt" << endl; return (digits[10]); } return (digits[i]);// Referenz auf int Wert des angeforderten Elementes } int Vector::upper() const // bestimmt die Vectorlaenge { return ndigits; } void Vector::print(ostream& strm) const // Ausgabemethode fuer alle Objekte { // vom Typ der KLasse Vector for (unsigned i=0 ; i<ndigits ; i++) strm << digits[i] << ' '; strm << endl; } Vector::Vector(int n, int a[]) // Konstruktor (Laenge und Feld) { digits = new int[ndigits = n]; for (unsigned i=0 ; i<ndigits ; i++) digits[i] = a[i]; } Vector& Vector::operator=(const Vector& n) // Zuweisungsoperator { if (&n != this) // c = c wenn &n == this { delete[] digits; // Loeschen im Zielobjekt unsigned i = n.ndigits; digits = new int[ndigits=i];// dyn.Speicherplatz fuer int* p = digits; // Zeiger im Zielobjekt const int* q = n.digits; // Zeiger im Quellobjekt while (i--) *p++ = *q++; // Kopieren von } return *this; } Vector Vector::operator+(const Vector& v) const// Additionskonstuktor {digits = new int[ndigits = n]; for (unsigned i=0 ; i<ndigits ; i++) digits[i] = a[i]; } int s; if (ndigits!=v.ndigits) cout << "Die zu addierenden Vektoren sind unterschiedlich lang" << endl; if (ndigits < v.ndigits) s= ndigits; else s= v.ndigits; Vector sum(s); // sum enthaelt die addierten Vektoren for (int i = 0; i < s; ++i) sum.digits[i] = digits[i] + v.digits[i]; return (sum); } ostream& operator<< (ostream& strm, const Vector& v) { v.print(strm); return strm; }Hier hab ich ein problem, das programm läuft wenn ich
digits = n.digits;schreibe, damit hat mein prof aber ein problem, und wenn ich es ins kundenprogramm schreibe auch mein speicher

ClientTime.cpp
/
*ClientTime.cpp*/ #include "Vector.h" #include <time.h> #include <iostream.h> int main() { int runs = 4; //Anzahl Durchlaufe int multi = 10; // Multiplikatorwert fuer nexte Zuweisungen unsigned long zahl= 100000 ; // Anfagswert der Zuweisungen double duration(unsigned long); cout << "Vectorzuweisung ohne Referenzzaehler:" << endl; //Standardausgabe cout << "Anzahl der Messungen: " << runs << endl << endl; //Standardausgabe while (runs--) { cout << zahl << " Zuweisungen = "<< duration(zahl) << " Sekunden"; cout << endl; zahl = zahl * multi; } return 0; } double duration(unsigned long MAX) { clock_t start, finish; Vector a(10), b(5); start = clock(); for (unsigned long i=0 ; i < MAX ; i++) b = a; finish = clock(); return (double)(finish-start)/CLOCKS_PER_SEC; }mir fällt echt nix ein, denn wenn ich es so laufen lasse, dann....
Vector.cpp:24: warning: this decimal constant is unsigned only in ISO C90 Vector.cpp:94:6: warning: no newline at end of file Vector.cpp: In member function ‘Vector Vector::operator+(const Vector&) const’: Vector.cpp:68: error: ‘n’ was not declared in this scope Vector.cpp:68: error: assignment of data-member ‘Vector::digits’ in read-only structure Vector.cpp:70: error: ‘a’ was not declared in this scope Vector.cpp: At global scope: Vector.cpp:73: error: expected unqualified-id before ‘if’ Vector.cpp:75: error: expected unqualified-id before ‘if’ Vector.cpp:77: error: expected unqualified-id before ‘else’ Vector.cpp:82: error: expected unqualified-id before ‘for’ Vector.cpp:82: error: expected constructor, destructor, or type conversion before ‘<’ token Vector.cpp:82: error: expected unqualified-id before ‘++’ token Vector.cpp:85: error: expected unqualified-id before ‘return’ Vector.cpp:86: error: expected declaration before ‘}’ token make: *** [Vector.o] Fehler 1Kann mir da jemand weiterhelfen ?
-
Hoppala aus welcher Sprache kommst du denn? Das kann man so nicht in C++ machen ^^
Naja also nen Großteil der Fehlermeldungen resultiert aus der folgenden Stelle:
Vector Vector::operator+(const Vector& v) const// Additionskonstuktor {digits = new int[ndigits = n]; for (unsigned i=0 ; i<ndigits ; i++) digits[i] = a[i]; }... du schließt den Scope ... das ist aber an der Stelle falsch
Also muss die letzte '}' da weg!
-
Aja und:
#include "Vector.h" #include <ctime> #include <iostream> double duration(unsigned long); // In den global-Scope :P Lokale Funktionsdeklarationen sind nicht erlaubt :P int main() { const unsigned int multi(10); unsigned long zahl(100000L); std::cout << "Vectorzuweisung ohne Referenzzaehler:" << std::endl; for (unsigned int rum(4); rum; --run) { std::cout << zahl << " Zuweisungen = "<< duration(zahl) << " Sekunden" << std::endl; zahl *= multi; } } double duration(unsigned long MAX) { const std::clock_t start(std::clock()) Vector a(10), b(5); for (unsigned long i(0); i < MAX; ++i) b = a; return static_cast<double>((std::clock() - start) / CLOCKS_PER_SEC); }... wobei das so nicht wirklich soo viel Sinn hat, was du da machst

-
(D)Evil schrieb:
Lokale Funktionsdeklarationen sind nicht erlaubt
Unsinn.