2010-08-20 56 views
1

好吧我試圖在兩個不同的向量中插入兩個值時,它將無法工作,第一個將工作,但第二個不會設置。C++不能在向量中插入值

// Xstrike.cpp : Defines the entry point for the application. 
// 

#include "stdafx.h" 
#include "Xstrike.h" 
#include <vector> 

#define MAX_LOADSTRING 100 

// Global Variables: 
HINSTANCE hInst;        // current instance 
TCHAR szTitle[MAX_LOADSTRING];     // The title bar text 
TCHAR szWindowClass[MAX_LOADSTRING];   // the main window class name 
RECT *rect; 
const UINT_PTR EVERYTHING_ID=0x1; 
const UINT_PTR LBUTTONDOWN_ID=0x3; 
const UINT_PTR TDENEMIE1_ID=0x4; 
const UINT_PTR TAENEMIE1_ID=0x5; 
int conno=2; 
int side=0; 
int cEnemie1=0; 
int dEnemie1=1; 
int aEnemie1=0; 
int sEnemie1=1; 
bool e1=true; 
time_t now; 
time_t tEnemie1; 

// Forward declarations of functions included in this code module: 
ATOM    MyRegisterClass(HINSTANCE hInstance); 
BOOL    InitInstance(HINSTANCE, int); 
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); 
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM); 

vector <POINT> vRegularShots; 
vector <POINT> vS1Enemie1; 
vector <POINT> vS2Enemie1; 
VOID Paint(HDC hdc, HWND hWnd) 
{ 
    hdc=GetDC(hWnd); 
    HDC memDC=CreateCompatibleDC(hdc); 
    HBITMAP hMemMap=CreateCompatibleBitmap(hdc, 225, 350); 
    HBITMAP hOldMap=(HBITMAP)SelectObject(memDC, hMemMap); 
    Graphics draw(memDC); 

    // Drawing 
    Image bg(L"bg.jpg"); 
    draw.DrawImage(&bg, 0, 0); 

    // Regular shots 
    Image shot(L"RegularShots.png"); 
    long s=vRegularShots.size(); 

    // Draw shots 
    for(long index=0; index < (long)vRegularShots.size(); ++index) 
    { 
     draw.DrawImage(&shot, vRegularShots[index].x, vRegularShots[index].y); 
    } 

    // Update the shots 
    for(long index=0; index < (long)vRegularShots.size(); ++index) 
    { 
     vRegularShots[index].y--; 
    } 

    // Create Enemies 
    if(dEnemie1==0) 
    { 
     if(cEnemie1<2) 
     { 
      if(aEnemie1==0) 
      { 
       SetTimer(hWnd, TAENEMIE1_ID, 550, NULL); 
      } 
      aEnemie1=1; 
      cEnemie1++; 
     } 
     else 
     { 
      KillTimer(hWnd, TDENEMIE1_ID); 
     } 
     dEnemie1=1; 
    } 

    // Draw enemies 
    for(long index=0; index < (long)vS1Enemie1.size(); ++index) 
    { 
     Image iEnemie1(L"Enemie1.png"); 
     draw.DrawImage(&iEnemie1, vS1Enemie1[index].x, vS1Enemie1[index].y); 
    } 
    for(long index=0; index < (long)vS2Enemie1.size(); ++index) 
    { 
     Image iEnemie1(L"Enemie1.png"); 
     draw.DrawImage(&iEnemie1, vS2Enemie1[index].x, vS2Enemie1[index].y); 
    } 

    // Update enemies 
    for(long index=0; index < (long)vS1Enemie1.size(); index++) 
    { 
     vS1Enemie1[index].x++; 
     vS1Enemie1[index].y++; 
    } 
    for(long index=0; index < (long)vS2Enemie1.size(); index++) 
    { 
     vS2Enemie1[index].x--; 
     vS2Enemie1[index].y++; 
    } 

    // Delete enemies 
    for(long index=0; index < (long)vS1Enemie1.size(); index++) 
    { 
     if(vS1Enemie1[index].x>225) 
     { 
      vS1Enemie1.erase(vS1Enemie1.begin()+index); 
     } 
    } 
    for(long index=0; index < (long)vS2Enemie1.size(); index++) 
    { 
     if(vS2Enemie1[index].x>225) 
     { 
      vS2Enemie1.erase(vS2Enemie1.begin()+index); 
     } 
    } 

    BitBlt(hdc, 0, 0, 225, 350, memDC, 0, 0, SRCCOPY); 
    ReleaseDC(hWnd, hdc); 
    SelectObject(memDC, hOldMap); 
    DeleteObject(hMemMap); 
    DeleteDC(memDC); 
} 

VOID CheckDead() 
{ 
    for(long index=0; index < (long)vRegularShots.size(); ++index) 
    { 
     vRegularShots[index].x, vRegularShots[index].y; 
    } 
} 

int APIENTRY _tWinMain(HINSTANCE hInstance, 
        HINSTANCE hPrevInstance, 
        LPTSTR lpCmdLine, 
        int  nCmdShow) 
{ 
    UNREFERENCED_PARAMETER(hPrevInstance); 
    UNREFERENCED_PARAMETER(lpCmdLine); 
    GdiplusStartupInput gdiplusStartupInput; 
    ULONG_PTR   gdiplusToken; 

    // TODO: Place code here. 
    MSG msg; 
    HACCEL hAccelTable; 
    // Initialize GDI+. 
    GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL); 


    // Initialize global strings 
    LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); 
    LoadString(hInstance, IDC_XSTRIKE, szWindowClass, MAX_LOADSTRING); 
    MyRegisterClass(hInstance); 

    // Perform application initialization: 
    if (!InitInstance (hInstance, nCmdShow)) 
    { 
     return FALSE; 
    } 

    hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_XSTRIKE)); 

    // Main message loop: 
    while (GetMessage(&msg, NULL, 0, 0)) 
    { 
     if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
     { 
      TranslateMessage(&msg); 
      DispatchMessage(&msg); 
     } 
    } 

    GdiplusShutdown(gdiplusToken); 
    return (int) msg.wParam; 
} 



// 
// FUNCTION: MyRegisterClass() 
// 
// PURPOSE: Registers the window class. 
// 
// COMMENTS: 
// 
// This function and its usage are only necessary if you want this code 
// to be compatible with Win32 systems prior to the 'RegisterClassEx' 
// function that was added to Windows 95. It is important to call this function 
// so that the application will get 'well formed' small icons associated 
// with it. 
// 
ATOM MyRegisterClass(HINSTANCE hInstance) 
{ 
    WNDCLASSEX wcex; 

    wcex.cbSize = sizeof(WNDCLASSEX); 

    wcex.style   = CS_HREDRAW | CS_VREDRAW; 
    wcex.lpfnWndProc = WndProc; 
    wcex.cbClsExtra  = 0; 
    wcex.cbWndExtra  = 0; 
    wcex.hInstance  = hInstance; 
    wcex.hIcon   = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_XSTRIKE)); 
    wcex.hCursor  = LoadCursor(hInstance, MAKEINTRESOURCE(IDC_CURSOR)); 
    wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); 
    wcex.lpszMenuName = MAKEINTRESOURCE(IDC_XSTRIKE); 
    wcex.lpszClassName = szWindowClass; 
    wcex.hIconSm  = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); 

    return RegisterClassEx(&wcex); 
} 

// 
// FUNCTION: InitInstance(HINSTANCE, int) 
// 
// PURPOSE: Saves instance handle and creates main window 
// 
// COMMENTS: 
// 
//  In this function, we save the instance handle in a global variable and 
//  create and display the main program window. 
// 
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) 
{ 
    HWND hWnd; 

    hInst = hInstance; // Store instance handle in our global variable 

    hWnd = CreateWindow(szWindowClass, szTitle, (WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX), 
     CW_USEDEFAULT, 0, 225, 350, NULL, NULL, hInstance, NULL); 

    if (!hWnd) 
    { 
     return FALSE; 
    } 

    ShowWindow(hWnd, nCmdShow); 
    UpdateWindow(hWnd); 

    return TRUE; 
} 

// 
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM) 
// 
// PURPOSE: Processes messages for the main window. 
// 
// WM_COMMAND - process the application menu 
// WM_PAINT - Paint the main window 
// WM_DESTROY - post a quit message and return 
// 
// 
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 
{ 
    int wmId, wmEvent; 
    PAINTSTRUCT ps; 
    HDC hdc; 
    POINT pt; 
    POINT pts; 

    switch (message) 
    { 
    case WM_CREATE: 
     SetTimer(hWnd, EVERYTHING_ID, 1, NULL); 
     break; 
    case WM_COMMAND: 
     wmId = LOWORD(wParam); 
     wmEvent = HIWORD(wParam); 
     // Parse the menu selections: 
     switch (wmId) 
     { 
     case IDM_ABOUT: 
      DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About); 
      break; 
     case IDM_EXIT: 
      DestroyWindow(hWnd); 
      break; 
     default: 
      return DefWindowProc(hWnd, message, wParam, lParam); 
     } 
     break; 
    case WM_PAINT: 
     hdc = BeginPaint(hWnd, &ps); 
     Paint(hdc, hWnd); 
     EndPaint(hWnd, &ps); 
     break; 
    case WM_LBUTTONDOWN: 
     GetCursorPos(&pt); 
     ScreenToClient(hWnd, &pt); 
     if(conno==1) 
     { 
      conno++; 
     } 
     else 
     { 
      pt.x+=18; 
      conno--; 
     } 
     vRegularShots.push_back(pt); 
     SetTimer(hWnd, LBUTTONDOWN_ID, 350, NULL); 
     InvalidateRect(hWnd, rect, false); 
     break; 
    case WM_LBUTTONUP: 
     KillTimer(hWnd, LBUTTONDOWN_ID); 
     break; 
    case WM_TIMER: 
     switch(wParam) 
     { 
     case EVERYTHING_ID: 
      if(e1==true) 
      { 
       now=time(NULL); 
       tEnemie1=now+1; 
       e1=false; 
      } 
      now=time(NULL); 
      if(now==tEnemie1) 
      { 
       SetTimer(hWnd, TDENEMIE1_ID, 550, NULL); 
      } 
      InvalidateRect(hWnd, rect, false); 
      break; 
     case LBUTTONDOWN_ID: 
      GetCursorPos(&pt); 
      ScreenToClient(hWnd, &pt); 
      if(conno==1) 
      { 
       conno++; 
      } 
      else 
      { 
       pt.x+=18; 
       conno--; 
      } 
      vRegularShots.push_back(pt); 
      break; 
     case TDENEMIE1_ID: 
      pt.y=5; 
      pt.x=-26; 
      vS1Enemie1.push_back(pt); 
      pt.y=52; 
      pt.x=251; 
      vS2Enemie1.push_back(pt); 
      dEnemie1=0; 
      InvalidateRect(hWnd, rect, false); 
      break; 
     case TAENEMIE1_ID: 
      InvalidateRect(hWnd, rect, false); 
      break; 
     } 
     break; 
    case WM_DESTROY: 
     PostQuitMessage(0); 
     break; 
    default: 
     return DefWindowProc(hWnd, message, wParam, lParam); 
    } 
    return 0; 
} 

// Message handler for about box. 
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) 
{ 
    UNREFERENCED_PARAMETER(lParam); 
    switch (message) 
    { 
    case WM_INITDIALOG: 
     return (INT_PTR)TRUE; 

    case WM_COMMAND: 
     if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
     { 
      EndDialog(hDlg, LOWORD(wParam)); 
      return (INT_PTR)TRUE; 
     } 
     break; 
    } 
    return (INT_PTR)FALSE; 
} 

中的值將在vS1Enemie1但不是在vS2Enemie1插入。
有什麼想法?

+1

你能告訴我們pt的聲明和pt的類型的定義嗎?還有2個向量的聲明? – George 2010-08-20 21:26:34

+3

您發佈的部分代碼沒有任何問題。請發佈一個小的,代表性的,可編譯的例子,以便您可以指出它並說「這是我期望看到的,但是這是發生了什麼」。 – 2010-08-20 21:26:53

+0

@George看看,我更新了帖子 – Ramilol 2010-08-20 21:29:32

回答

2

當您在Visual Studio中的特定代碼行上放置斷點,然後命中該斷點時,放置斷點的代碼行尚未實際執行。

例如,讓我們讓「*」表示您在Visual Studio中放置斷點的位置。

int a = 0; 
    int b = 0; 
    a = 3; 
* b = 4; 

當你命中該斷點,它是A和B被宣佈後,過了已被設置爲3,但前行的「B = 4;」有機會執行。大多數調試器都這樣做,所以你有機會進入你放置斷點的代碼行。例如,我可能有「myComplicatedFunction(a,b);」而不是「b = 4」。通過在該行代碼有機會執行之前停止,調試器給你一個機會來檢查數據或者進入一個函數,看看在發生什麼事情之前發生的事情,然後纔會發生一切錯誤。