2013-09-10 98 views
0

我是VC++的新手,我嘗試使用類。我的子程序工作得很好,因爲它是直接的代碼,但是當我嘗試在類中使用它時,我總是收到錯誤 以下是頭文件的代碼。使用類Visual C++ 2010

  [using namespace std; 
#ifndef Deck_h 
#define Deck_h 
class Deck 
{ 
     public: 
// Default constructor 
Deck(); 
//Destructor 
~Deck(); 
// access functions 
//function1 
// Member variables 

int InDeck[53]; 
int OutDeck[53]; 



    }; 

#endif 

下面是.cpp文件

  #include "StdAfx.h" 
    #include "Deck.h" 
    #include <stdlib.h> 
    #include <time.h> 
    &Deck::Deck() 

    { // start of Deck 
     int InDeck[53]; 
     int OutDeck[53]; 
     int icard; 
     int isuit=1; 

     for(int i = 1; i<=52; i++) 
      { // Begin For i 
      icard = i % 13; 
      if(icard == 0){icard=13;} 
      InDeck[i] = isuit * 1000 + icard; 
      OutDeck[i] = 0; 
      if(icard == 13){isuit ++;} 
     }// end of for i... 
     // Randomize InDeck into OutDeck 
     int t = 0; 
     srand(time_t(NULL)); 
     for(int j = 1; j<=52; j++) 
     { // begin for j 
      icard = rand() % 52 +1; 
      t = 0; 
      while (OutDeck[icard] >= 1000) 
      { // while 
       t++; 
       icard = rand() % 52 +1; 
       if(t > 10) 
       { // Don't take too long shuffling 
        for(int k=1; k<=52; k++) 
        { // put card in first empty slot 
         if(OutDeck[k] < 1000) { 
          icard = k; 
          t = 0; 
          break; 
         } // empty slot found 
        } //end of for k... 
        } // end of if t > 1000 

      } //end while 
      OutDeck[icard] = InDeck[j]; 

     } // end for j 
    } // end of Deck 

代碼這裏是使用類

  Deck mydeck; 

array<PictureBox ^, 1>^pix = gcnew array<PictureBox ^, 1>(10); 
pix[0] = this->pb1; 
pix[1] = this->pb2; 
pix[2] = this->pb3; 
pix[3] = this->pb4; 
pix[4] = this->pb5; 
pix[5] = this->pb6; 
pix[6] = this->pb7; 
pix[7] = this->pb8; 
pix[8] = this->pb9; 
pix[9] = this->pb10; 


for(int p = 1; p<= 10; p++) 
{pix[p-1]->Image = Bitmap::FromFile("c:\\users\\Bob K\\Documents\\pictures\\" +    System::Convert::ToString(mydeck.OutDeck[p]) + ".bmp"); 
} 

     } //End of Form1 load 

這些錯誤消息的代碼

1 > Deck.cpp
1>生成代碼...
1> CardsOne.obj:錯誤LNK2028:無法解析的標記(0A000013)「public:__clrcall Deck ::〜Deck(void)」(?? 1Deck @@ $$ FQAM @ XZ)在函數「private:void __clrcall CardsOne :: Form1 :: Form1_Load(class System :: Object ^,class System :: EventArgs ^)」(?Form1_Load @ Form1 @ CardsOne @@ $$ FA $ AAMXP $ AAVObject @ System @@ P> AAVEventArgs @ 4 @@ Z)
1> CardsOne.obj:error LNK2019:無法解析的外部符號「public:__clrcall Deck ::〜Deck(void)」(?? 1Deck @@ $$ FQAM @ XZ)函數「private:void __clrcall CardsOne :: Form1 :: Form1_Load(System System :: Object ^,class System :: EventArgs ^)」(?Form1_Load @ Form1 @ CardsOne @@ $$ FA $ AAMXP $ AAVObject @ System @@ P $ AAVEventArgs @ 4 @@ Z)
1> C:\ Users \ Bob K \ Documents \ Visual Studio 2010 \ Projects \ CardsOne \ Debug \ CardsOne.exe:致命錯誤LNK1120:2個未解析的外部設備
==========生成:0成功,1失敗,0上最新,0已跳過==========

我希望得到任何幫助誰能給我

+1

那不是C++,那是C++/CX或C++/CLI請正確標記。 – PeterT

+0

你的代碼中有很多奇怪的東西。爲什麼你的套牌有* 53 *卡?並且'&Deck :: Deck()'中的'&'是什麼?在構造函數的主體中,您將創建兩個局部變量'InDeck'和'OutDeck',它們將影響具有相同名稱的數據成員。你從[1,52]而不是[0,52]索引。最後,C++/CLI *不是* C++! – Praetorian

回答

1

定義你的析構函數,在頭可能,甚至不寫~Deck();如果你不使用它

1

連接程說:「你告訴我你的.h文件中,將有一個~Deck函數在某處,但我找不到它的定義。「

由於您沒有使用析構函數,只需從頭中刪除~Deck();即可。