Używanie klasy w innej klasie.

0

Mam takie pytanie: Dlaczego jeżeli stworzę dwa pliki o nazwach Weight.h oraz Height.h i zadeklaruję w nich klasy Weight oraz Height, to później nie mogę ich użyć w trzecim pliku Car.h, w którym zadeklarowana jest klasa Car, gdyż pomimo dodania w Car.h nagłówków:

#include "Weight.h"
#include "Height.h"
<definicja klasy, akcesory itp...>
private:
	Weight w;
	Height h;

otrzymuję powiadomienie o błędzie, wg. którego używam niezadeklarowanych klas Weight oraz Height.
Jeżeli jednak klasy Weight oraz Height zadeklaruję w pliku Car.h ponad klasą Car, to wszystko działa poprawnie. Może nie jest to jakiś poważny błąd bo i bez tego wszystko działa, ale zaciekawiło mnie to, ponieważ np. C# pozwala mi na to. Czy jest ktoś w stanie odpowiedzieć mi dlaczego.

0

mozesz uzywac, cos robisz po prostu zle. Moze plikow nie masz dolaczonych do projektu skoro nie widzi tych plikow. Wklej kod, wklej komunikat bledu

0

OK, już nieważne. Przerzuciłem kod klas do pliku Car.h, a potem z powrotem do osobnych klas i zadziałało. Ale i tak wrzucam kod poniżej:
main.cpp:

#include <iostream>
#include <string>
#include <conio.h>
#include "Car.h"

using namespace std;

int main()
{
	Weight w(1500, "kg");

	Height h(2, "m");

	Car c(w, h);
}

Car.h:

#include <iostream>
#include <string>
#include <conio.h>
#include "Weight.h"
#include "Height.h"

using namespace std;

class  Car
{
	public:
		Car():CarWeight(Weight()), CarHeight(Height())
		{

		}

		Car(Weight NewWeight, Height NewHeight)
		{
			cout << "Creating a car\n";
			CarWeight = NewWeight;
			CarHeight = NewHeight;
			cout << "The car's weight is " << CarWeight.GetValue() << " " << CarWeight.GetUnit() << ".\n";
			cout << "The car's height is " << CarHeight.GetValue() << " " << CarHeight.GetUnit() << ".\n";
		}

		~ Car()
		{
			cout << "\nDestructing a car.\n";
			_getch();
		}

	private:
		Weight CarWeight;
		Height CarHeight;
};

Dimensions.h:

#include <iostream>
#include <string>

using namespace std;

class Weight
{
	public:
		Weight(): WeightValue(0), WeightUnit("g")
		{
			
		}

		Weight(int Value, string Unit)
		{
			WeightValue = Value;
			WeightUnit = Unit;
		}

		~Weight()
		{

		}

		void SetValue(int Value)
		{
			WeightValue = Value;
		}

		int GetValue()
		{
			return WeightValue;
		}

		void SetUnit(string Unit)
		{
			WeightUnit = Unit;
		}

		string GetUnit()
		{
			return WeightUnit;
		}

	private:
		int WeightValue;
		string WeightUnit;
};

class Height
{
	public:
		Height(): HeightValue(0), HeightUnit("m")
		{
			
		}

		Height(int Value, string Unit)
		{
			HeightValue = Value;
			HeightUnit = Unit;
		}

		~Height()
		{

		}

		void SetValue(int Value)
		{
			HeightValue = Value;
		}

		int GetValue()
		{
			return HeightValue;
		}

		void SetUnit(string Unit)
		{
			HeightUnit = Unit;
		}

		string GetUnit()
		{
			return HeightUnit;
		}

	private:
		int HeightValue;
		string HeightUnit;
};
0

Zgodnie z sugestią @furious programming przerzuciłem klasy Weight.h i Height.h do klasy Dimensions.h i znów nie działa. Jednak po przerzuceniu wszystkiego do Car.h znów działa. A oto błędy:

error C2079: 'Car::CarWeight' uses undefined class 'Weight'
error C2079: 'Car::CarHeight' uses undefined class 'Height'
error C2027: use of undefined type 'Weight'
error C2011: 'Weight' : 'class' type redefinition
error C2011: 'Height' : 'class' type redefinition
error C1903: unable to recover from previous error(s); stopping compilation
0

uzywasz klasy weight a jej nie ma. Bledy to jedno, przydalby sie kod. Wrzucaj na pastebin i wklej linka.

0

Hmm... Nie znając zbytnio komunikatów Twojego kompilatora, można śmiało wywnioskować, że:

error C2079: 'Car::CarWeight' uses undefined class 'Weight'
error C2079: 'Car::CarHeight' uses undefined class 'Height'

oba te pola czy właściwości używają niezdefiniowanych klas;

error C2027: use of undefined type 'Weight'

Tu podobnie - niezdefiniowany typ Weight;

error C2011: 'Weight' : 'class' type redefinition
error C2011: 'Height' : 'class' type redefinition

Redeklaracja typów sugeruje, że istnieją co najmniej dwa typy o takiej samej nazwie; Pewnie nie deincludowałeś starych modułów Weight.h i Height.h, lub po prostu zostały one zapamiętane - spróbuj przebudować projekt;

PS: Nie wiem czy wiesz, ale słowo weight oznacza wagę/ciężar, a nie szerokość; Domyślam się, że klasy mają określać szerokość i wysokość, więc powinieneś użyć słów width i height.

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