#### Archived

This topic is now archived and is closed to further replies.

# Coding Help...

This topic is 5585 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm getting a couple of compiling errors and need some help wondering why it won't work. General.cpp

#include <string>
#include "General.h"
#include "resource.h"

GENERAL::GENERAL()
{
hMainApp = 0;
hDlg = 0;
Num_of_Players = 0;
intCurrent = 0;
}
void GENERAL::Get_Players_Names()

{
char* szPNum;
HWND hName;
int intCount;
szPNum = new char[20];
for(intCount= 0; intCount < Num_of_Players; intCount++)
{
sprintf(szPNum,"Enter Player %i Name:",intCount);
hName = CreateDialog(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DLG_NAMES), NULL, (DLGPROC)NameProc);
if(hName != NULL)
{
SetDlgItemText(hName,IDC_NAME_LBL,szPNum);
ShowWindow(hName, SW_SHOW);
intCurrent = intCount;
}
}
delete[] szPNum;
}

GENERAL::~GENERAL()
{

}

LONG CALLBACK NameProc(HWND hwnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
switch(Msg)
{
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDC_NAMES_OK:
{
char* szPlayerName;
szPlayerName = new char[15];
GetDlgItemText(hwnd, IDC_PNAME,szPlayerName,FALSE);
vPlayers.assign(intCurrent,szPlayerName);
}
}
case WM_INITDIALOG:
{
return TRUE;
break;
}
case WM_CLOSE:
DestroyWindow(hwnd);
return TRUE;
break;
case WM_DESTROY:
//(hwnd);

DestroyWindow(hwnd);
return TRUE;
}

}

General.h

#ifndef GENERAL_H

#define GENERAL_H

#include <windows.h>

class GENERAL
{

public:

GENERAL(); // Default constructor

void Get_Players_Names();

HINSTANCE hMainApp;
HWND hDlg;
int Num_of_Players;
~GENERAL();//deconstrutor

private:
int intCurrent;
LONG CALLBACK NameProc(HWND hwnd, UINT Mesg, WPARAM wParam, LPARAM lParam);

};
#endif

In the main.cpp I have declared vPlayers as a class vector of Players so I can have an array of Players based on the number selected by the user. After I get the number of players from user I destroy the dialog and then call Get_Players_Names. This function will loop as many times as the number of players inserting each player name into the name property. I have a couple of compiling errors that I'm not sure why I'm getting. c:\GDev\Cards\Ride2\General.cpp(23) : error C2440: 'type cast' : cannot convert from 'overloaded-function' to 'DLGPROC' None of the functions with this name in scope match the target type c:\GDev\Cards\Ride2\General.cpp(51) : error C2065: 'vPlayers' : undeclared identifier c:\GDev\Cards\Ride2\General.cpp(51) : error C2228: left of '.assign' must have class/struct/union type c:\GDev\Cards\Ride2\General.cpp(51) : error C2065: 'intCurrent' : undeclared identifier Everything seems to be declared correctly. Any ideas? TIA Triston [/source] All the world's a stage...and I seem to fall off quite a bit. [edited by - Triston on September 3, 2002 9:56:31 AM]

##### Share on other sites
You need the callback function to be a C-compatible function, not a C++ member function.
You can get around this by making the function static, but you won''t get a this pointer or be able to access any members.

The other error is because you haven''t declared the variable as extern anywhere. Put the extern declaration in the header for the class which exports the variable. Look up extern if you don''t know what I''m talking about.

##### Share on other sites
Okay the static keyword worked, thanks. I think I''m going to research a bit more on static functions, but I think that should serve my purpose for now.

In my main.cpp I have declared this:

vector vPlayers;

This gives me a vector of a class, so how can I make a vector extern? I understand what extern does and means. I guess I figured if I declared it as a global var in main.cpp it would be able to use globally, but now I understand why it wouldn''t. But what would be the syntax to declare vPlayers?

TIA
Triston