2011-03-20 4 views
1

내 스레드 외부의 벡터에 액세스하려고 시도했을 때 링크 오류가 발생했습니다. 내 디자인은 클라이언트로부터 메시지를 받으면 추가 작업을 위해 데이터 큐에 마사지 또는 데이터를 넣기를 원합니다. 그러나 나는 그 (것)들을 일하게 할 수 없다.문제점 : 스레드 내에서 벡터에 액세스

.cpp 파일 :

// Inside the recv thread 
start: 
    err = recvfrom(RecvSocket, lpData->RecvBuf, BufLen, 0, (SOCKADDR *)&lpData->SenderAddr, &SenderAddrSize); 
    //lpData is used to access Recv Structure which stores the attributes of the server and client. 

    switch(lpData->port) 
    { 
     case 6516: 
     { 
      BuffStack1.push_back(lpData->RecvBuf); 
      break; 
     } 

     case 6517: 
     { 
      break; 
     } 

     case 6518: 
     { 
      break; 
     } 
    } 

goto start; 

.H 파일 : 여기 내 코드입니다

class CUdpSocket 
{ 
public: 
    CUdpSocket(void); 
    ~CUdpSocket(void); 
    void ServerRecv(int port); 
    void ClientSend(const char *ip, int port, const char *buff); 
    unsigned static __stdcall ServerRecvThread(void *arguments); 
    unsigned static __stdcall ClientSendThread(void *arguments); 
    CString static Itoa(int data); 
    void Mix(); 

private: 
    RecvStruct *pRecvData; 
    SendStruct *pSendData; 
    vector<HANDLE>threadStl; 
    static vector<char*>BuffStack1; // Here is my stack vector 
    static vector<char*>BuffStack2; 
    static vector<char*>BuffStack3; 
    HANDLE hThread; 
    unsigned threadID; 
    static BufferData *ptrBufferData; 
}; 

내가 정적 여부를 넣을 필요가 있는지 조금 혼란 스러워요. 그리고 정적 둘 때, 오류는 다음과 같습니다

error LNK2001: unresolved external symbol "private: static class std::vector<char *,class std::allocator<char *> > CUdpSocket::BuffStack1" ([email protected]@@[email protected][email protected]@[email protected]@@[email protected]@A) 

을 그리고 정적 문을 넣어하지 않은 경우, 오류가 말한다 :

error C2228: left of '.push_back' must have class/struct/union 

이 도와주세요.

감사합니다.

답변

3

저는 C++에서는 static 데이터 멤버를 사용하는 것이 2 단계 프로세스라고 생각합니다. 먼저 이고 변수 이름이이고 변수가 이고static 변수를 정의해야 변수가 하나의 번역 단위에 저장 공간이됩니다. 내 추측으로는 첫 번째 단계는 완료했지만 두 번째 단계는 완료하지 않았다는 것입니다.

는 .cpp 파일에 이와 같은 정의를 말하자면 static 변수를 정의하려면 : 당신이 여기 하지 반복 static 키워드를 할

vector<char*> CUdpSocket::BuffStack1; 

참고. 대신, static 데이터 멤버의 형식 및 정규화 된 이름을 제공하면됩니다. static 데이터 멤버에 대해 기본이 아닌 생성자를 사용하려는 경우 여기에서도 수행 할 수 있습니다.

희망이 도움이됩니다.

추신. 레이블을 사용하지 말고 goto을 사용하여 루프를 구현하십시오 ... 대신 while(true) 루프를 사용하십시오! :-)

편집 :vector을 스레드에서 액세스하려는 경우 적절한 동기화를 수행해야합니다. 하나의 스레드가 언제든지 수정할 수 있다는 보장없이 복수 스레드에서 vector을 읽고 쓰는 것은 안전하지 않습니다.

+0

나는 작품을 만들 수 없습니다. 어떻게 보여 주시겠습니까? –

+0

좀 더 구체적으로 말씀해 주시겠습니까? 어떤 오류가 발생하고 있습니까? 특히 작동하지 않는 것은 무엇입니까? – templatetypedef

+0

신경 쓰지 마십시오. 나는 이미 그것을 알아 냈다. 도와 줘서 고맙다. –

관련 문제