[ Pharos @ 15.06.2006. 11:03 ] @
Imam 2 klase A i B.
Klasa A poziva klasu B, a klasa B poziva klasu A.
Znači A.h bih trebalo da imam nešto kao:
Code:

#include "B.h"
class A { ... };


A u B.h nešto kao

Code:

#include A.h"
class B { ... };


Pošto ne mogu da se rade include-ovi u krug, u B.h sam stavio ovako

Code:

class A;
class B { ... };


Na ovaj način metode klase B koje koriste klasu A neće moći da se definišu u samoj klasi (.h datoteci) već će morati da se definišu u .cpp datoteci.

Kako da ih definišem tamo?

Hvala!
[ #Ninja# @ 15.06.2006. 11:23 ] @
Code:
// a.h
class B; // declaration

class A
{
   public:
   A();
   A(B& data);
   int var;
};

// b.h
#include "a.h"

class B
{
   public:
   B();
   B(A& data);
   int var;
};

This will work. I always put a

#pragma once

in my header files, but not all compilers support that.



http://www.thescripts.com/forum/thread60222.html
[ Pharos @ 15.06.2006. 11:49 ] @
Ok je to za deklaraciju. To sam i ja uradio.
Ali kako da odradim metode koje koriste onu deklarisanu klasu?
To mora u cpp datoteci, a ne kapiram baš najbolje kako to.

edit:

Kad se metode koje koriste onu deklarisanu klasu nalaze u samoj klasi (.h datoteci), prevodilac prijavi sledeću grešku:

Code:
error C2514: 'A' : class has no constructors


U klasi A je uredno napravljen konstruktor samo što ga klasa B ne vidi :(
[ tosa @ 15.06.2006. 12:19 ] @
Pa stavi metode u cpp fajl. Jedini problem može da bude ako su ti performanse bitne a želeo si inline metode.
U svakom slučaju bih ti savetovao da promeniš dizajn koda tako da taj problem iščezne sa njim.
[ #Ninja# @ 15.06.2006. 12:56 ] @
Nemam ba� iskustva s kreiranjem klasa, ali evo �ta sam prona�ao:

Code:
// a.h
class B; // This file needs to know that B is a class and that's all it
needs
         // to know. Think of this declaration as "previews of coming
         // attractions", like at the movies.

class A
{
  // other stuff for class A members

    B* pB;

  // other stuff
};


Code:
// b.h
class A; // This file needs to know that A is a class and that's all it
needs
         // to know. Think of this declaration as "previews of coming
         // attractions", like at the movies.

class B
{

  // other stuff for class B members

    A* getA();

  // other stuff
};


Code:
//main.cpp

#include <iostream>

#include "a.h"
#include "b.h"

using std::cout;
using std::endl;

int main()
{

    // create objects from Class A and Class B
    // etc.

    cout << "Included both" << endl;
    return 0;
}


http://www.gidforums.com/t-9782.html
[ Pharos @ 15.06.2006. 12:57 ] @
Al kako se metoda stavlja u .cpp fajl, a da se pri tom ne deklariše u samoj klasi?
To je ono što ne znam.

Dakle da pojasnim.
Ovde je domaći koji trebam uraditi.

Klasa figura treba da se postavi na zadato mesto na zadatoj tabli. Tabla sadrži dinamičku matricu pokazivača na figuru.
Nešto kao Figura*** polje;

E sad, za figura.h trebam da imam
Code:


Figura& postavi(const Tabla* t, int xx, int yy)
    {
        t->polje[xx][yy]=this;
        return *this;
    }


To mi je ona klasa A;

A za klasu Tabla, opet imam neku metodu postavi.
Mislio sam nekako ovako:
Code:

Tabla& postavi(char v, int xx, int yy)
    {
        *polje[xx][yy] = new Figura (xx,yy,v);
        return *this;
    }


I nema šanse da proradi.
[ Časlav Ilić @ 16.06.2006. 14:38 ] @
Ako metodima Figure prosleđuješ samo pokazivače na Tablu, onda možeš da uradiš ovako:

Code:
// figura.h
#ifndef FIGURA_H
#define FIGURA_H

class Tabla; // dovoljno za pokazivač

class Figura
    {
        ...
        Figura& postavi(const Tabla* t, int xx, int yy);
    };

#endif

Code:
// figura.cpp
#include <figura.h>
#include <tabla.h>

...

Figura& Figura::postavi(const Tabla* t, int xx, int yy)
    {
        t->polje[xx][yy]=this;
        return *this;
    }


Zatim možeš u tabla.h slobodno da uključiš figura.h, ako je potrebno.
[ Pharos @ 16.06.2006. 16:58 ] @
Da li to uopšte može da se uradi u Visual Studiu?
Uradim sve tako i neće!
[ Časlav Ilić @ 16.06.2006. 17:16 ] @
Kako tačno neće? Daj greške koje ti izbaci.
[ nikoladsp @ 03.09.2006. 00:55 ] @
sigurno ti je vec kasno za domaci,ali evo kako se takve stvari rade,da znas za ubuduce :
imas klasu A koja sadrzi B(tacnije pointer na objekat klase B,koji se inicijalizuje u konstruktoru od A i unistava u destruktoru od A).
B ima pointer na IA interfejs (koga implementira klasa A), pa onda sve metode koje si deklarisao u IA kao pure virtual, mozes da pozoves iz B... samo vodi racuna:
1. sve metode koji su pure virtual,moras da implementiras u klasi koja nasledjuje taj interfejs
2. treba u nekom trenutku da prosledis klasi B pointer na konkretan objekat koji je drzi i cije ces metode pozivati(u ovom slucaju SomeMethod() iz A), i najbolje mesto da to uradis je u konstruktoru B klase,jer inace postoji verovatnoca da ces to negde zaboraviti da inicijalizujes,a onda neces imati pojma gde si pogresio
Code:

//A.h
#ifndef __A_H__
#define __A_H__

#include <iostream>
#include "b.h"

class A : public IA {

private:

    B* m_pB;

public:

    A();
    virtual ~A();

    void FooA();
    void SomeMethod();
};

#endif // __A_H__

Code:

//A.cpp
#include "a.h"

using namespace std;

A::A() {

    m_pB = new B(this);
}

A::~A() {

    if (m_pB != NULL) delete m_pB;
}

void A::FooA() {
    
    cout << "Foo A..." << endl;

    m_pB->FooB();
}

void A::SomeMethod() {

    cout << "member of type B called method form A..." << endl;
}

Code:

//B.h
#ifndef __B_H__
#define __B_H__

#include <iostream>
#include "IA.h"

class B {

private:

    IA* m_pIA;

public:

    B(IA* pIA);
    virtual ~B();

    void FooB();
};

#endif // __B_H__

Code:

//B.cpp
#include "b.h"

using namespace std;

B::B(IA* pIA) {

    m_pIA = pIA;
}

B::~B() {

}

void B::FooB() {
    
    cout << "Foo B..." << endl;

    m_pIA->SomeMethod();
}

Code:

//IA.h
#ifndef __IA_H__
#define __IA_H__

class IA {

public:

    virtual void SomeMethod() = 0L;
};

#endif // __IA_H__

Code:

//main.cpp
#include "A.h"

int main(int nArgc, char** pszArgs) {

    A a;
    a.FooA();

    return 0;
}


PS. NIKADA ne koristi #pragma once, ti makroi ne rade uvek-znam iz licnog iskustva, uvek pravi ovako
Code:

#ifndef __imefajla_h__
#define __imefajla_h__

.... kod ovde...

#endif 
[ Pharos @ 11.09.2006. 20:54 ] @
Problem je rešen, zaboravio sam da javim.
Code:

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

Code:

// B.h.
#include "A.h"
class B { ... };

Onda u A.cpp datoteci napišem
Code:

// A.cpp
#include "A.h"
#include "B.h"
...

U A.cpp datoteci nisam include-ovao B.h pa nisam mogao da prevedem kod.

@nikoladsp
Nije to ono što sam tražio ali hvala na trudu.