[C++]Dziedziczenie (wsteczne?)

0

witam,
od razu przepraszam za temat, ale nie miałem pojęcia jak to zatytułować inaczej.
Problem tym razem mam w tym, że chcę napisać metodę dla klasy Plane która oblicza odległość od Point'a p , a deklaracja wygląda mniej więcej tak :

 float Plane::getDistanceFromPoint(Point& p) 

i wszystko gra i buczy, ALE chcę też zrobić opcję żeby obliczać odległość Point'a p od Plane'a pl i myslę, że wyglądało by to tak:

 float Point::getDistanceFromPlane(Plane& pl) 

problem w tym, że w klasie Plane mam deklarację

#include "vector.h" 

a w vector.h mam deklarację #include "point.h"


i jeżeli chciałbym dołączyć drugą opisaną metodę i napisać w pliku point.h coś takiego 
```cpp
 #include "plane.h"

to wszystko się sypie(mnóstwo błędów o nie wiadomo czymś) i nie wiem jak to obejść.
Nawet deklaracja klasy Plane przed

class Point { /* ... */   }; 

nic nie daje.

0
  1. W pliku *.h tylko deklaracje, definicje w pliku *.cpp
  2. Deklaracje zapowiadające tzn
//B.h
class A;
class B
{
  //
};

//A.h
class B;
class A
{
  //
};

Podkreślam ze w deklaracji zapowiadającej NIE MA żadnego działa klasy.

0

Ok, to rozumiem (albo przynajmniej tak myślę) ale cos wam pokażę :

point.h

#ifndef POINT_H
#define POINT_H

#include <stdlib.h>

class Plane;
		
class Quaternion;
class Point
{
public:
/* ... */
	Point rotateWithQuaternion(Quaternion& q);

	//float getDistanceFromPlane(Plane& p);       
/* ... */
};
 

to jest wersja bez błędów, przy czym funkcja z klasą Quaternion działa ,a w pliku quaternion.h mam

 #include "vector.h"

który ma #include "point.h"


a w pliku plane.h mam to samo 
```cpp
 #include "vector.h"

i jeżeli odkomentuję

 float getDistanceFromPlane(Plane& p);  

to już nie działa tylko wysypuje błędy ?? :

Error	1	error C2027: use of undefined type 'Plane'		67
Error	2	error C2228: left of '.A' must have class/struct/union		67
Error	3	error C2027: use of undefined type 'Plane'		67
Error	4	error C2228: left of '.B' must have class/struct/union		67
Error	5	error C2027: use of undefined type 'Plane'		67
Error	6	error C2228: left of '.C' must have class/struct/union		67
Error	7	error C2027: use of undefined type 'Plane'		67
Error	8	error C2228: left of '.D' must have class/struct/union		67
	24	IntelliSense: incomplete type is not allowed		67
	25	IntelliSense: incomplete type is not allowed		67
	26	IntelliSense: incomplete type is not allowed		67
	27	IntelliSense: incomplete type is not allowed		67
Error	9	error C2027: use of undefined type 'Plane'		70
Error	10	error C2228: left of '.A' must have class/struct/union		70
Error	11	error C2027: use of undefined type 'Plane'		70
Error	12	error C2228: left of '.A' must have class/struct/union		70
Error	13	error C2027: use of undefined type 'Plane'		70
Error	14	error C2228: left of '.B' must have class/struct/union		70
Error	15	error C2027: use of undefined type 'Plane'		70
Error	16	error C2228: left of '.B' must have class/struct/union		70
Error	17	error C2027: use of undefined type 'Plane'		70
Error	18	error C2228: left of '.C' must have class/struct/union		70
Error	19	error C2027: use of undefined type 'Plane'		70
Error	20	error C2228: left of '.C' must have class/struct/union		70
	28	IntelliSense: incomplete type is not allowed		70
	29	IntelliSense: incomplete type is not allowed		70
	30	IntelliSense: incomplete type is not allowed		70
	31	IntelliSense: incomplete type is not allowed		70
	32	IntelliSense: incomplete type is not allowed		70
	33	IntelliSense: incomplete type is not allowed		70
 

odnośnie kodu

 float Point::getDistanceFromPlane(Plane& p)
{
	float dist = p.A*(this->getX()) + p.B*(this->getY()) + p.C*(this->getZ()) + p.D;
	if(dist < 0)
		dist = (-1)*dist;
	return ( dist / sqrt(p.A*p.A + p.B*p.B + p.C*p.C) );
}
0

Nie nie nie. Nie korzystaj nigdy z faktu że jakiśtam nagłówek który includujesz includuje inny nagłówek. Jeśli korzystasz z klas Plane i Quaternion to includujesz pliki nagłówkowe z ich deklaracjami i kropka.

0

Ok , ale czy mógłbyś trochę to bardziej wytłumaczyć (ewentualnie na moim przykładzie) ?

bo ja robię coś takiego w pliku point.h :

#include "plane.h"
#include "quaternion.h"
class Quaternion;
class Plane; 

i :

	61	IntelliSense: identifier "Point" is undefined	d:\erasmus\301cr\porfolio\mymath\include\vector.h	37
	62	IntelliSense: identifier "Point" is undefined	d:\erasmus\301cr\porfolio\mymath\include\vector.h	37
	63	IntelliSense: identifier "Point" is undefined	d:\erasmus\301cr\porfolio\mymath\include\vector.h	76
	64	IntelliSense: identifier "Point" is undefined	d:\erasmus\301cr\porfolio\mymath\include\vector.h	77
	65	IntelliSense: identifier "Point" is undefined	d:\erasmus\301cr\porfolio\mymath\include\vector.h	79
	66	IntelliSense: identifier "Point" is undefined	d:\erasmus\301cr\porfolio\mymath\include\vector.h	79
	67	IntelliSense: identifier "Point" is undefined	d:\erasmus\301cr\porfolio\mymath\include\vector.h	80
	68	IntelliSense: identifier "Point" is undefined	d:\erasmus\301cr\porfolio\mymath\include\vector.h	81
	69	IntelliSense: identifier "Point" is undefined	d:\erasmus\301cr\porfolio\mymath\include\plane.h	15
Error	33	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	d:\erasmus\301cr\porfolio\mymath\include\angleaxis.h	16
Error	34	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	d:\erasmus\301cr\porfolio\mymath\include\angleaxis.h	16
Error	38	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	d:\erasmus\301cr\porfolio\mymath\include\angleaxis.h	22
Error	39	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	d:\erasmus\301cr\porfolio\mymath\include\angleaxis.h	22
Error	42	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	d:\erasmus\301cr\porfolio\mymath\include\quaternion.h	19
Error	43	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	d:\erasmus\301cr\porfolio\mymath\include\quaternion.h	19
Error	46	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	d:\erasmus\301cr\porfolio\mymath\include\quaternion.h	39
Error	47	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	d:\erasmus\301cr\porfolio\mymath\include\quaternion.h	39
Error	49	error C2614: 'Quaternion' : illegal member initialization: 'v' is not a base or member	d:\erasmus\301cr\porfolio\mymath\include\quaternion.h	16
Error	24	error C2535: 'Vector::Vector(void)' : member function already defined or declared	d:\erasmus\301cr\porfolio\mymath\include\vector.h	37
Error	29	error C2535: 'Vector Vector::vectorFromPoints(void)' : member function already defined or declared	d:\erasmus\301cr\porfolio\mymath\include\vector.h	80
Error	2	error C2535: 'Plane::Plane(void)' : member function already defined or declared	d:\erasmus\301cr\porfolio\mymath\include\plane.h	13
Error	13	error C2535: 'Plane::Plane(void)' : member function already defined or declared	d:\erasmus\301cr\porfolio\mymath\include\plane.h	13
Error	51	error C2535: 'Plane::Plane(void)' : member function already defined or declared	d:\erasmus\301cr\porfolio\mymath\include\plane.h	13
Error	9	error C2228: left of '.getZ' must have class/struct/union	d:\erasmus\301cr\porfolio\mymath\include\plane.h	13
Error	20	error C2228: left of '.getZ' must have class/struct/union	d:\erasmus\301cr\porfolio\mymath\include\plane.h	13
Error	58	error C2228: left of '.getZ' must have class/struct/union	d:\erasmus\301cr\porfolio\mymath\include\plane.h	13
Error	7	error C2228: left of '.getY' must have class/struct/union	d:\erasmus\301cr\porfolio\mymath\include\plane.h	13
Error	18	error C2228: left of '.getY' must have class/struct/union	d:\erasmus\301cr\porfolio\mymath\include\plane.h	13
Error	56	error C2228: left of '.getY' must have class/struct/union	d:\erasmus\301cr\porfolio\mymath\include\plane.h	13
Error	5	error C2228: left of '.getX' must have class/struct/union	d:\erasmus\301cr\porfolio\mymath\include\plane.h	13
Error	16	error C2228: left of '.getX' must have class/struct/union	d:\erasmus\301cr\porfolio\mymath\include\plane.h	13
Error	54	error C2228: left of '.getX' must have class/struct/union	d:\erasmus\301cr\porfolio\mymath\include\plane.h	13
Error	45	error C2146: syntax error : missing ';' before identifier 'v'	d:\erasmus\301cr\porfolio\mymath\include\quaternion.h	39
Error	41	error C2146: syntax error : missing ';' before identifier 'getV'	d:\erasmus\301cr\porfolio\mymath\include\quaternion.h	19
Error	32	error C2146: syntax error : missing ';' before identifier 'getAxis'	d:\erasmus\301cr\porfolio\mymath\include\angleaxis.h	16
Error	37	error C2146: syntax error : missing ';' before identifier 'axis'	d:\erasmus\301cr\porfolio\mymath\include\angleaxis.h	22
Error	48	error C2065: 'newV' : undeclared identifier	d:\erasmus\301cr\porfolio\mymath\include\quaternion.h	16
Error	4	error C2065: 'newNormal' : undeclared identifier	d:\erasmus\301cr\porfolio\mymath\include\plane.h	13
Error	6	error C2065: 'newNormal' : undeclared identifier	d:\erasmus\301cr\porfolio\mymath\include\plane.h	13
Error	8	error C2065: 'newNormal' : undeclared identifier	d:\erasmus\301cr\porfolio\mymath\include\plane.h	13
Error	15	error C2065: 'newNormal' : undeclared identifier	d:\erasmus\301cr\porfolio\mymath\include\plane.h	13
Error	17	error C2065: 'newNormal' : undeclared identifier	d:\erasmus\301cr\porfolio\mymath\include\plane.h	13
Error	19	error C2065: 'newNormal' : undeclared identifier	d:\erasmus\301cr\porfolio\mymath\include\plane.h	13
Error	53	error C2065: 'newNormal' : undeclared identifier	d:\erasmus\301cr\porfolio\mymath\include\plane.h	13
Error	55	error C2065: 'newNormal' : undeclared identifier	d:\erasmus\301cr\porfolio\mymath\include\plane.h	13
Error	57	error C2065: 'newNormal' : undeclared identifier	d:\erasmus\301cr\porfolio\mymath\include\plane.h	13
Error	10	error C2065: 'newD' : undeclared identifier	d:\erasmus\301cr\porfolio\mymath\include\plane.h	13
Error	21	error C2065: 'newD' : undeclared identifier	d:\erasmus\301cr\porfolio\mymath\include\plane.h	13
Error	59	error C2065: 'newD' : undeclared identifier	d:\erasmus\301cr\porfolio\mymath\include\plane.h	13
Error	1	error C2061: syntax error : identifier 'Vector'	d:\erasmus\301cr\porfolio\mymath\include\plane.h	13
Error	12	error C2061: syntax error : identifier 'Vector'	d:\erasmus\301cr\porfolio\mymath\include\plane.h	13
Error	36	error C2061: syntax error : identifier 'Vector'	d:\erasmus\301cr\porfolio\mymath\include\angleaxis.h	19
Error	40	error C2061: syntax error : identifier 'Vector'	d:\erasmus\301cr\porfolio\mymath\include\quaternion.h	16
Error	50	error C2061: syntax error : identifier 'Vector'	d:\erasmus\301cr\porfolio\mymath\include\plane.h	13
Error	3	error C2061: syntax error : identifier 'Point'	d:\erasmus\301cr\porfolio\mymath\include\plane.h	15
Error	14	error C2061: syntax error : identifier 'Point'	d:\erasmus\301cr\porfolio\mymath\include\plane.h	15
Error	23	error C2061: syntax error : identifier 'Point'	d:\erasmus\301cr\porfolio\mymath\include\vector.h	37
Error	25	error C2061: syntax error : identifier 'Point'	d:\erasmus\301cr\porfolio\mymath\include\vector.h	76
Error	26	error C2061: syntax error : identifier 'Point'	d:\erasmus\301cr\porfolio\mymath\include\vector.h	77
Error	27	error C2061: syntax error : identifier 'Point'	d:\erasmus\301cr\porfolio\mymath\include\vector.h	79
Error	28	error C2061: syntax error : identifier 'Point'	d:\erasmus\301cr\porfolio\mymath\include\vector.h	80
Error	30	error C2061: syntax error : identifier 'Point'	d:\erasmus\301cr\porfolio\mymath\include\vector.h	81
Error	31	error C2061: syntax error : identifier 'Point'	d:\erasmus\301cr\porfolio\mymath\include\plane.h	15
Error	52	error C2061: syntax error : identifier 'Point'	d:\erasmus\301cr\porfolio\mymath\include\plane.h	15
Error	11	error C1903: unable to recover from previous error(s); stopping compilation	d:\erasmus\301cr\porfolio\mymath\include\plane.h	13
Error	22	error C1903: unable to recover from previous error(s); stopping compilation	d:\erasmus\301cr\porfolio\mymath\include\plane.h	13
Error	60	error C1903: unable to recover from previous error(s); stopping compilation	d:\erasmus\301cr\porfolio\mymath\include\plane.h	13
 

a jeżeli zamiast tego kawałka kodu powyżej zrobię :

 class Plane;
class Quaternion;

to znowu mam takie błędy :/ :

 Error	11	error C2228: left of '.D' must have class/struct/union	d:\erasmus\301cr\porfolio\mymath\mymath\point.cpp	67
Error	9	error C2228: left of '.C' must have class/struct/union	d:\erasmus\301cr\porfolio\mymath\mymath\point.cpp	67
Error	21	error C2228: left of '.C' must have class/struct/union	d:\erasmus\301cr\porfolio\mymath\mymath\point.cpp	70
Error	23	error C2228: left of '.C' must have class/struct/union	d:\erasmus\301cr\porfolio\mymath\mymath\point.cpp	70
Error	7	error C2228: left of '.B' must have class/struct/union	d:\erasmus\301cr\porfolio\mymath\mymath\point.cpp	67
Error	17	error C2228: left of '.B' must have class/struct/union	d:\erasmus\301cr\porfolio\mymath\mymath\point.cpp	70
Error	19	error C2228: left of '.B' must have class/struct/union	d:\erasmus\301cr\porfolio\mymath\mymath\point.cpp	70
Error	5	error C2228: left of '.A' must have class/struct/union	d:\erasmus\301cr\porfolio\mymath\mymath\point.cpp	67
Error	13	error C2228: left of '.A' must have class/struct/union	d:\erasmus\301cr\porfolio\mymath\mymath\point.cpp	70
Error	15	error C2228: left of '.A' must have class/struct/union	d:\erasmus\301cr\porfolio\mymath\mymath\point.cpp	70
Error	4	error C2027: use of undefined type 'Plane'	d:\erasmus\301cr\porfolio\mymath\mymath\point.cpp	67
Error	6	error C2027: use of undefined type 'Plane'	d:\erasmus\301cr\porfolio\mymath\mymath\point.cpp	67
Error	8	error C2027: use of undefined type 'Plane'	d:\erasmus\301cr\porfolio\mymath\mymath\point.cpp	67
Error	10	error C2027: use of undefined type 'Plane'	d:\erasmus\301cr\porfolio\mymath\mymath\point.cpp	67
Error	12	error C2027: use of undefined type 'Plane'	d:\erasmus\301cr\porfolio\mymath\mymath\point.cpp	70
Error	14	error C2027: use of undefined type 'Plane'	d:\erasmus\301cr\porfolio\mymath\mymath\point.cpp	70
Error	16	error C2027: use of undefined type 'Plane'	d:\erasmus\301cr\porfolio\mymath\mymath\point.cpp	70
Error	18	error C2027: use of undefined type 'Plane'	d:\erasmus\301cr\porfolio\mymath\mymath\point.cpp	70
Error	20	error C2027: use of undefined type 'Plane'	d:\erasmus\301cr\porfolio\mymath\mymath\point.cpp	70
Error	22	error C2027: use of undefined type 'Plane'	d:\erasmus\301cr\porfolio\mymath\mymath\point.cpp	70

a znowu jak zrobie samo :

 class Quaternion;

i wykomentuje funkcje które używają plane to wszystko działa.

EDIT: jeszcze jest jedna opcja i to chyba to o co chodziło : jeżeli napiszę :

 #include "quaternion.h" 
class Quaternion;

to znowu wysypuje błędy (mniej więcej o point i vector - undefined identifier)
Nie rozumiem tego :/

0

Możesz gdzieś wrzucić te źródła? Bo nie bardzo z tych szczątków które pokazujesz widać co robisz. Bo te błędy wyglądają raczej tak że cos w samym kodzie klas jest skopane.

0

point.h http://wklej.to/BJPc
point.cpp http://wklej.to/hk3u
vector.h http://wklej.to/lBdy
vector.cpp http://wklej.to/5w6g
plane.h http://wklej.to/01fk
plane.cpp http://wklej.to/6pKv
quaternion.h http://wklej.to/HbuA
quaternion.cpp http://wklej.to/PTgw
angleaxis.h http://wklej.to/T7ej
angleaxis.cpp http://wklej.to/Gkvp
matrix.h http://wklej.to/DQlu

ale problem w tym że wszystko działa pięknie do momentu kiedy nie chcę dołączyć deklaracji class Plane w pliku point.h :/

0

Nie widze deklaracji zapowiadających klase Point w vector.h
Tak samo w Plane nie widzę ani zapowiedzi czym jest Point ani inlcudowania point.h
Nie widzę zapowiedzi klasy Vector w quaternion.h
Może jeszcze coś przeoczyłem...

0

dalej nie działa... nie mam pojęcia jak to zrobić i czy w ogóle da się tak zrobić :/

point.h http://wklej.to/QCnk

vector.h http://wklej.to/HwrZ

plane.h http://wklej.to/qOTl

quaternion.h http://wklej.to/PtDi

angleaxis.h http://wklej.to/tPLR

0

Ok trochę namieszałem wcześniej, powinno być tak:

  • w plikach *.h masz tylko (!) deklaracje zapowiadające oraz deklaracje klas (bez includowania deklaracji klas)
  • w plikach *.cpp masz includowane wszystkie(!) pliki nagłówkowe klas z których korzystasz
0

ok spróbowałem coś takiego i :
point.h http://wklej.to/AMBa
point.cpp http://wklej.to/XyIS

tak to ma wyglądać ? bo jezeli tak to mam więcej błędów niż poprzednio... :/

0

Omg. Następnym razem zwyczajnie zleje takie posty. Nie umiesz zamieścić całego projektu jak człowiek? Mam marnować 20min na poskładanie sobie tego?
Wiesz czym sie różni DEKLARACJA od DEFINICJI? Bo takie coś:

Plane(Vector& newNormal, float newD):A(newNormal.getX()), B(newNormal.getY()), C(newNormal.getZ()), D(newD) {;}

to definicja...
Masz takiego kwiatka także w kilku innych miejscach...

A nazywanie własnych klas tak samo jak klasy już istniejące w STL to proszenie się o guza, tym bardziej że używasz std::

0

Ok, przepraszam nie jestem geniuszem jeżeli chodzi o c++ i chyba myślę, że o to tutaj na forum chodzi (ale zanotowałem Twoje uwagi).

zrobiłem tak jak napisałeś i usunąłem wszelkie definicje z nagłówków, zmieniłem nazwę klasy Vector i dalej otrzymuję w miarę to samo, chce wrzucić projekt, ale nie chcę po raz wtóry się wygłupić. Na razie wrzuciłem na sendspace (pierwszy mi wpadł do głowy) jeżeli jest jakieś lepsze miejsce - specjalnie przeznaczone do projektów, plików .cpp czy .h to chętnie się o nim dowiem.
I zaznaczam że jest to projekt z VS 2010.

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