jddipqd
Čuven
- Učlanjen(a)
- 17.10.2000
- Poruke
- 2,555
- Poena
- 725
Ovo je zvanično rešenje jednog ispitnog zadatka, ali ono ne može da se iskompajlira (MS VC++ 7)
Da li oni nas pogrešno uče ili sam ja lud?
Dobijam sledeće greške:
Da li oni nas pogrešno uče ili sam ja lud?
Kod:
#include <iostream>
using namespace std;
class Predmet {
static int ukId; int id;
virtual void pisi(ostream &d) const =0;
public:
Predmet () { id = ++ukId; }
Predmet (const Predmet &) { id = ++ukId; }
virtual ~Predmet () {}
Predmet &operator= (const Predmet &) { return *this);
int operator! () const { return id; }
virtual Predmet *kopija () const =0;
friend ostream &operator<< (ostream &d, const Predmet &p)
{ p.pisi (d); return d; }
};
int Predmet::ukId = 0;
class Celi : public Predmet {
int vr;
void pisi (ostream &d) const { d<<vr; }
public:
Celi (int v=0) { vr = v; }
int operator+ () const { return vr; }
Celi *kopija () const { return new Celi (*this); }
};
class GIndeks {};
class Zbirka {
public:
virtual ~Zbirka () {}
virtual const char *vrsta () const =0;
virtual int operator+ () const =0;
virtual Zbirka &operator+= (const Predmet &p) =0;
virtual Predmet *& operator[](int i)=0;
virtual const Predmet *operator[] (int i) const =0;
virtual Zbirka &operator~ () =0;
friend ostream &operator<< (ostream &d, const Zbirka &z);
};
ostream &operator<< (ostream &d, const Zbirka &z) {
d << z.vrsta() << '[';
for (int i=0; i<+z; i++) { d<<*z[i]; if (i!=+z-1) d<<','; }
return d << ']';
}
class Niz: public Zbirka {
Predmet **niz; int duz, kap;
void kopiraj (const Niz &n);
void brisi () { ~*this; delete[] niz; }
public:
Niz (int k=10) { niz = new Predmet* [kap = k]; duz = 0; }
Niz (const Niz &n) { kopiraj (n); }
~Niz () { brisi (); }
Niz & operator= (const Niz &n) {
if (this!=&n){ brisi(); kopiraj(n); }
return *this;
}
const char *vrsta () const { return "Niz"; }
int operator+ () const { return duz; }
Niz & operator+= (const Predmet &p);
Predmet *& operator[] (int i) {
if (i<0 || i>=duz) throw GIndeks ();
return niz[i];
}
const Predmet * operator[] (int i) const {
if (i<0 || i>=duz) throw GIndeks ();
return niz[i];
}
Niz & operator~ ();
};
void Niz::kopiraj (const Niz &n) {
niz = new Predmet* [kap=n.kap]; duz = n.duz;
for (int i=0; i<duz; i++) niz[i] = n.niz[i]->kopija();
}
Niz & Niz::operator+= (const Predmet &p){
if (duz == kap) {
Predmet **pom = new Predmet* [kap+=(kap<100?10:0.1*kap)];
for (int i=0; i<duz; i++) pom[i] = niz[i];
delete [] niz; niz = pom;
}
niz[duz++] = p.kopija();
return *this;
}
Niz & Niz::operator~ () {
for (int i=0; i<duz; delete niz[i++]);
duz = 0; return *this;
}
int main () {
Niz niz (20); cout << "Niz? ";
while (1) {
int k; cin >> k;
if (k == 9999) break;
niz += k;
}
cout << niz << endl;
while (1) {
int i; cout << "Indeks? "; cin >> i;
if (i == 9999) break;
try {
cout << "id=" << !*niz[i] << ", vr=" << *niz[i] << endl;
} catch (GIndeks) {
cout << "*** Neispravan indeks!\n";
}
}
return 0;
}
Dobijam sledeće greške:
Kod:
------ Build started: Project: nizovi, Configuration: Debug Win32 ------
Compiling...
nizovi.cpp
nizovi.cpp(5) : error C2864: 'ukId' : only const static integral data members can be initialized inside a class or struct
nizovi.cpp(20) : error C2504: 'Predmet' : base class undefined
nizovi.cpp(43) : error C2804: binary 'operator <<' has too many parameters
nizovi.cpp(43) : error C2333: 'Predmet::operator`<<'' : error in function declaration; skipping function body
nizovi.cpp(75) : error C3254: 'Predmet' : class contains explicit override 'kopiraj' but does not derive from an interface that contains the function declaration
nizovi.cpp(75) : error C2838: 'kopiraj' : illegal qualified name in member declaration
nizovi.cpp(80) : error C3254: 'Predmet' : class contains explicit override '+=' but does not derive from an interface that contains the function declaration
nizovi.cpp(80) : error C2838: '+=' : illegal qualified name in member declaration
nizovi.cpp(90) : error C3254: 'Predmet' : class contains explicit override '~' but does not derive from an interface that contains the function declaration
nizovi.cpp(90) : error C2838: '~' : illegal qualified name in member declaration
nizovi.cpp(114) : fatal error C1075: end of file found before the left brace '{' at 'nizovi.cpp(4)' was matched
nizovi - 11 error(s), 0 warning(s)
---------------------- Done ----------------------
Build: 0 succeeded, 1 failed, 0 skipped