Błędy przy inkludowaniu, deklaracje zapowiadające

0

Hej. Mam kilka plików: A.h, A.cpp, B.h, B.cpp i main.cpp.

Plik A.h

#include "B.h"
class string;
using std::string;
class vector;
using std::vector;
class ostream;

class A{
   private:
           long a; 
           string b,c,d,e; 
           vector<B> wektor;
   //...
};

W pliku B.h:

#include "A.h"
class string;
using std::string;
class vector;
using std::vector;
class ostream;

class B{
   private:
           string b,c,d; 
           A a;
           vector<int> e;
   //...
};

W plik main.cpp chcę używać klas A i B. Ale dostaję błędy:

/home/unix/Pulpit/main.cpp|11|error: ‘std::string’ has not been declared|
/home/unix/Pulpit/main.cpp|13|error: ‘std::vector’ has not been declared|
/home/unix/Pulpit/main.cpp|25|error: ‘ostream’ does not name a type|
/home/unix/Pulpit/main.cpp|34|error: field ‘a’ has incomplete type|
/home/unix/Pulpit/main.cpp|36|error: field ‘b’ has incomplete type|
/home/unix/Pulpit/main.cpp|38|error: field ‘c’ has incomplete type|
/home/unix/Pulpit/main.cpp|40|error: field ‘d’ has incomplete type|
/home/unix/Pulpit/main.cpp|42|error: field ‘e’ has incomplete type|
/home/unix/Pulpit/main.cpp|44|error: ‘vector’ is not a template|
/home/unix/Pulpit/main.cpp|44|error: field ‘groups’ has incomplete type|
/home/unix/Pulpit/main.cpp|70|error: ‘vector’ is not a template|
/home/unix/Pulpit/main.cpp|71|error: ‘vector’ is not a template|
/home/unix/Pulpit/main.cpp|9|error: ‘ostream’ does not name a type|
/home/unix/Pulpit/main.cpp||In function ‘bool operator==(const A&, const A&)’:|
/home/unix/Pulpit/main.cpp|30|error: ‘const class A’ has no member named ‘a’|
/home/unix/Pulpit/main.cpp|30|error: ‘const class A’ has no member named ‘a’|
/home/unix/Pulpit/main.cpp|31|error: ‘const class A’ has no member named ‘b’|
/home/unix/Pulpit/main.cpp|31|error: ‘const class A’ has no member named ‘b’|
/home/unix/Pulpit/main.cpp|32|error: ‘const class A’ has no member named ‘c’|
/home/unix/Pulpit/main.cpp|32|error: ‘const class A’ has no member named ‘c’|
/home/unix/Pulpit/main.cpp|33|error: ‘const class A’ has no member named ‘d’|
/home/unix/Pulpit/main.cpp|33|error: ‘const class A’ has no member named ‘d’|
/home/unix/Pulpit/main.cpp|34|error: ‘const class A’ has no member named ‘e’|
/home/unix/Pulpit/main.cpp|34|error: ‘const class A’ has no member named ‘e’|
/home/unix/Pulpit/main.cpp||In member function ‘A& A::operator=(const A&)’:|
/home/unix/Pulpit/main.cpp|40|error: ‘class A’ has no member named ‘a’|
/home/unix/Pulpit/main.cpp|40|error: ‘const class A’ has no member named ‘a’|
/home/unix/Pulpit/main.cpp|41|error: ‘class A’ has no member named ‘b’|
/home/unix/Pulpit/main.cpp|41|error: ‘const class A’ has no member named ‘b’|
/home/unix/Pulpit/main.cpp|42|error: ‘class A’ has no member named ‘c’|
/home/unix/Pulpit/main.cpp|42|error: ‘const class A’ has no member named ‘c’|
/home/unix/Pulpit/main.cpp|43|error: ‘class A’ has no member named ‘d’|
/home/unix/Pulpit/main.cpp|43|error: ‘const class A’ has no member named ‘d’|
/home/unix/Pulpit/main.cpp|44|error: ‘class A’ has no member named ‘e’|
/home/unix/Pulpit/main.cpp|44|error: ‘const class A’ has no member named ‘e’|
/home/unix/Pulpit/main.cpp|45|error: ‘class A’ has no member named ‘groups’|
/home/unix/Pulpit/main.cpp|45|error: ‘const class A’ has no member named ‘groups’|
/home/unix/Pulpit/main.cpp||In constructor ‘A::A()’:|
/home/unix/Pulpit/main.cpp|52|error: ‘class A’ has no member named ‘a’|
/home/unix/Pulpit/main.cpp|53|error: ‘class A’ has no member named ‘b’|
/home/unix/Pulpit/main.cpp|54|error: ‘class A’ has no member named ‘c’|
/home/unix/Pulpit/main.cpp|55|error: ‘class A’ has no member named ‘d’|
/home/unix/Pulpit/main.cpp|56|error: ‘class A’ has no member named ‘e’|
/home/unix/Pulpit/main.cpp|57|error: ‘class A’ has no member named ‘groups’|
/home/unix/Pulpit/main.cpp|61|error: ‘vector’ is not a template|
/home/unix/Pulpit/main.cpp|61|error: prototype for ‘A::A(long int, string, string, string, string, string, vector)’ does not match any in class ‘A’|
/home/unix/Pulpit/main.cpp|55|error: candidates are: A::A(const A&)|
/home/unix/Pulpit/main.cpp|53|error: A::A(long int, string, string, string, string, string)|
/home/unix/Pulpit/main.cpp|50|error: A::A()|
/home/unix/Pulpit/main.cpp|74|error: ‘class A’ has no member named ‘a’|
/home/unix/Pulpit/main.cpp|74|error: ‘const class A’ has no member named ‘a’|
/home/unix/Pulpit/main.cpp|75|error: ‘class A’ has no member named ‘b’|
/home/unix/Pulpit/main.cpp|75|error: ‘const class A’ has no member named ‘b’|
||More errors follow but not being shown.|
||Edit the max errors limit in compiler options...|
||=== Build finished: 50 errors, 0 warnings ===|

Czy to znaczy, że jeśli nie mam wskaźników/referencji nie mogę używać deklaracji zapowiadających?

0

Masz mylne wyobrażenie o deklaracjach zapowiadających. Tak, umożliwiają wzajemne użycie klas, ale nie w taki sposób. Jeżeli jakaś klasa jest bezpośrednio polem drugiej klasy to zawarta jest ona w niej w całości. Jeżeli ma 100 bajtów to tyle właśnie będzie zajmować w tej drugiej klasie. Ale jak kompilator może obliczyć jej wielkość skoro jedna i druga zawierają się w sobie?
Możesz za to użyć wskaźnika do takiej klasy. I wtedy właśnie w jednym z tych plików używasz deklaracji zapowiadającej tej klasy (a nie klasy z biblioteki standardowej, to jakiś żart chyba).

0

No dobrze - czyli jeśli się "nawzajem" inkludują, to nie mogę dać deklaracji zapowiadającej.

Nie zrozumiałem Cię do końca; - nie mogę stosować deklaracji zapowiadających do klas z biblioteki standardowej?

0

Nic nie zrozumiałeś. Jeśli polem klasy jest OBIEKT innej klasy to DEFINICJA tej klasy musi być znana wcześniej, bo kompilator musi wiedzieć ILE bajtów zajmuje ta klasa. W efekcie NIE WYSTARCZY deklaracja zapowiadająca. Gdyby jednak polem klasy był WSKAŹNIK to taka deklaracja wystarczy bo rozmiar wskaźnika jest znany.

0

prościej: jeżeli w A zawiera się B, a w B zawiera się A w którym znów zawiera się B i tak w nieskończoność to klasa ta musiałaby zajmować nieskończoną ilość miejsca w pamięci...

co innego jeżeli masz wskaźnik i wskazujesz na istniejący już obiekt

a co deklaracji zapowiadających do klas z biblioteki standardowej to WTF
po co chcesz to w ogóle robić? :| po prostu je zaincluduj...

1 użytkowników online, w tym zalogowanych: 0, gości: 1