2014-04-05 10 views
0

이것은 내 첫 번째 게시물이므로 다음과 같이 사용하여 첫 번째 객체 내에서 선언 된 다른 객체에 객체를 전달하려고합니다. 예어. 내가 두 번째 객체를 통과 한 첫 번째 객체에서 무언가를 액세스하려고 시도 할 때까지는 모두 잘 돌아갑니다. 다음 오류로 끝납니다 :전달 선언 오류없이이 키워드를 사용하여 한 객체를 다른 객체로 전달하는 방법

cac.cc: In member function ‘void Portfolio::check(Client*)’: 
cac.cc:8:9: error: invalid use of incomplete type ‘struct Client’ 
cac.cc:3:7: error: forward declaration of ‘struct Client’ 

아래 코드는 다음과 같습니다. 또한 실패한 줄로 좁혔습니다. 나는이 줄을 주석으로 경우 코드가 컴파일 :

#include <iostream> 

class Client; 

class Portfolio { 
    public: 
     void check(Client *client) { 
       client->buy("AAPL"); //<- If i comment our this line the program compiles 
     } 
}; 

class Client { 
    public: 
     Portfolio port; 

     void buy(std::string name) { 
       std::cout << "Sending order for " << name << "!\n"; 
     } 

     void shouldIBuy() { 
       port.check(this); 
     } 
}; 

int main() { 
     Client client; 
     client.shouldIBuy(); 
} 

나는 코드가 클라이언트 클래스 프로토 타입 된 경우에도 때문에 컴파일 실패 믿는다는 멤버 함수의 구매는하지 않은입니다. 나보다 더 많은 경험을 가진 사람이 이것을 확인해 줄 수 있습니까? 구조를 너무 많이 바꾸지 않고도이 문제를 해결할 수있는 방법은 없을까요?

감사합니다. Client 클래스가 정의 된 후까지 멤버 함수 정의와

+0

개체는 전달할 수 있지만 개체에 전달할 수는 없습니다. 컴파일러는이 클래스에 대해 아무 것도 모르기 때문에. 오류가 발생합니다. "client-> buy ("AAPL ");" – AdamF

답변

0

표준 접근법은 선언에서 구현을 분리하는 것이다. 이렇게하면 모든 구현 모듈에서 클래스 선언을 사용할 수 있습니다. 더 큰 프로그램에서 각 논리 세그먼트 (잠재적으로 각 클래스)는 하나의 컴파일 단위에 속하게됩니다. 다음은 더 자세한 세부 사항을 설명하는 몇 가지 주석이 포함 된 다중 단위 구현입니다.

// -- this be in "client.h" 
#include <string> 
#include "portfolio.h" 

class Client { 
public: 
    void buy(std::string const& name); 
    bool shouldIBuy(); 
private: 
    // since Portfolio is include by value (not a pointer), the 
    // compiler absolutely requires that it is a "complete type" 
    // so that it can calculate the correct byte size of a Client 
    // object 
    Portfolio port; 
}; 


// -- the following would be in "portfolio.h" 
// Client is only referenced in a parameter list so a complete 
// type is unnecessary. A forward declaration is the "least 
// coupled" solution. 
class Client; 

class Portfolio { 
public: 
    bool check(Client *client); 
}; 


// -- the following would be in "client.cpp" 
#include <iostream> 

#include "client.h" 
#include "portfolio.h" 

void Client::buy(std::string const& name) { 
    std::cout << "Sending order for " << name << "!\n"; 
} 

bool Client::shouldIBuy() { 
    return port.check(this); 
} 


// -- the following would be in "portfolio.cpp" 
#include "portfolio.h" 
#include "client.h" 

bool Portfolio::check(Client *client) { 
    // we need the complete type of Client at this point 
    client->buy("AAPL"); 
    return true; 
} 


// -- the following would be in "main.cpp" 
#include "client.h" 

int main() { 
    Client client; 
    client.shouldIBuy(); 
    return 0; 
} 
4

: 잠깐

class Client; 

class Portfolio { 
    public: 
     void check(Client *client); 
}; 

class Client { 
    public: 
     Portfolio port; 

     void buy(std::string name) { 
       std::cout << "Sending order for " << name << "!\n"; 
     } 

     void shouldIBuy() { 
       port.check(this); 
     } 
}; 

void Portfolio::check(Client *client) { 
    client->buy("AAPL"); 
}