|
Revised Post
Hi,
I am facing a peculiar problem with vectors. I define a vector in my program with a size given by another variable. Works fine.
But the same thing, I try inside a structure gives me a copiler error of C2061: sytax error : identifier size
Code:
#include
void main()
{
int size =4;
vector <int > vect(size);
vect[0] = 3;
vect[1] = 2;
vect[2] = 1;
display(vect);
}
This works fine But below code doesnt
int gsize =3;
struct VectorStruct
{
vector <int > vect2(gsize); //error C2061: syntax error
// : identifier 'gsize'
};
and even if i try to give a vlaue instead of variable it fails
struct VectorStruct
{
vector <int > vect2(2); //error C2059: syntax error : 'constant'
};
Can someone help me out. I cant figure out what going wrong
Thanks Peter for the suggestion, it works. Thanks a ton
Vinod
|
|
|
|
|
If I want the same thing to be in constructor of that structure what is the way
I tried..
int gsize =3;
struct VectorStruct
{
int size;
vector <int> vect2;
//VectorStruct():vect2(i){}; //this works
VectorStruct()
{ //constructor
size=gsize;
vect2(size);
}
}
This gives an compiler error,(error C2064: term does not evaluate to a function taking 1 arguments) howto give size in constructor? Any pointers..
|
|
|
|
|
To pass a parameter to the constructor of a member, you need to pass that parameter to the constructor of the class, e.g.
struct VectorStruct
{
vector <int> vect2;
VectorStruct(int size):vect2(size){}
}
use class:
int gsize =3;
VectorStruct vs(gsize);
VectorStruct vs1(6);
Peter
"Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."
|
|
|
|
|
I am having trouble printing the value I just keyed in. However I am able to get the ascii value corresponding to it. Now how do I change that to text?
The code below does not work...
case WM_CHAR: {<br />
LPSTR s; s = (LPSTR) wParam; MessageBox(hwnd,s,"hi",MB_OK);<br />
}<br />
break;
|
|
|
|
|
// Try something like this...
case WM_CHAR: {
char s[2]={0};
s[0]=wParam;
::MessageBox(hwnd,s,"hi",MB_OK);
}
break;
|
|
|
|
|
Now how can I keep compounding all those keystrokes into a string variable...? I've tried strcat() - does not work!
|
|
|
|
|
Since s[0] = wParam will always add to the first position in the array, replace the 0 with an actual variable.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Are you using MFC? If so, using a CString to concatenate is the simplest approach. Not the most efficient but the easiest to implement.
It's += operator handles TCHAR and LPCTSTR as well as other CString references.
CString is really the only reason I ever started using MFC in the first place.
|
|
|
|
|
Hello everyone,
There is error message when executing my program,
Unhandled exception at 0x00411a49 in test_entern.exe: 0xC0000005: Access violation reading location 0x00000002.
It is very simple, does anyone know what is wrong with the program?
I have tested that when changing from extern int* p_int to extern int p_int[16], my program is ok. But I think the two statements should be the same, right?
foo.c
<br />
int p_int [16] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17};<br />
goo.c
<br />
extern int* p_int;<br />
<br />
int main (int argc, char** argv)<br />
{<br />
int i;<br />
int sum = 0;<br />
for (i = 0; i < 16; i++)<br />
{<br />
sum += p_int [i];
}<br />
<br />
return 0;<br />
}<br />
thanks in advance,
George
|
|
|
|
|
What is the address of p_int (in main()) for both the cases.
|
|
|
|
|
Both are 0x00427b40.
regards,
George
|
|
|
|
|
George_George wrote: I have tested that when changing from extern int* p_int to extern int p_int[16], my program is ok. But I think the two statements should be the same, right?
Not quite. The compiler can't make the connection that the extern *p_int in goo.c is the same variable as the int p_int[16] in foo.c. It can't reconcile the integer pointer and the interger array. Just use extern int p_int[] in goo.c - you don't need to specify the size but you do need to tell the compiler the external variable is an array.
Judy
|
|
|
|
|
Thanks for your advice, Judy!
regards,
George
|
|
|
|
|
Hi all. I have a TCP program that crashes when a connection isnt made. One example is when i open it and dont allow the connection through a firewall it crashes in Windows. How would i go about making it try to reconnect every so often if a connection isnt established without crashing? Thanx in advance!
|
|
|
|
|
dellthinker wrote: How would i go about making it try to reconnect every so often if a connection isnt established without crashing?
Probably the first step is to use the debugger to determine exactly why it is crashing. When it crashes, look at the call stack window, see how it got there, inspect the variables.
|
|
|
|
|
I ran the debugger and it pointed at this line:
<br />
structSock.sin_addr.s_addr = *((unsigned long*)ServerAddr->h_addr_list[0]);<br />
Do i need to loop this every so often to make sure the program doesnt crash when trying to connect?
This was the error message that VC++ gave me.
<quote>
Unhandled exception in Program.exe: 0xC0000005: Access Violation.
P.S. I set a breakpoint at that line and yes it is the problem. How can i resolve this?
Anyone care to shed some light on what that means?
|
|
|
|
|
Hello,
I am currently working on a notepad-like application and I am having trouble with the CFileDialog class. In my code there is a save function, and when the user selects save as from the menu, the function is run and the "Save File As" dialog comes up. When the user enters the filename and presses the save button, my program cannot seem to get the file extension...As an example, if the user entered foo.txt for the filename, I can use GetFileExt() to get the file extension, but if the user just enters foo and selects .txt extension from the dropdown, I cannot seem to get the extension
Currently I'm using a VC++ 6.0 compiler. Here is the code:
void CMFCTestDlg::OnFileSaveas()
{
CFileDialog filesave(FALSE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"Text files (*.txt)|*.txt|All Files|*||");
if (filesave.DoModal()==IDOK){
filename=filesave.GetFileTitle();
filename+=".";
filename+=filesave.GetFileExt();
CFile theFile;
theFile.Open(filename,CFile::modeWrite | CFile::modeCreate);
int filesize=m_editbox.GetWindowTextLength();
char* buffer=new char [filesize+1];
m_editbox.GetWindowText(buffer,filesize+1);
theFile.Write(buffer,filesize+1);
delete [] buffer;
theFile.Close();
m_editbox.SetModify(FALSE);
}
}
BTW, m_editbox is my textbox. The function saves the text in m_editbox to a file.
Thanks !
|
|
|
|
|
The nFilterIndex member in the OPENFILENAME structure indicates the currently selected index in the "save as type" combobox. So can get the extesnion using this index
CString csExt = dlg.GetFileExt();
if( csExt.IsEmpty())
{
int n = filesave.m_ofn.nFilterIndex
// now if n == 1, the item is "Text files (*.txt)"
// if n == 2, the items is "All Files"
}
BTW I would like to know what will you put extesnion when user selects "All files".
FYI :- Normally there will not be "All files" options in the save as dialog box.
|
|
|
|
|
LOL, thanks for pointing that out! I just copied the save stuff over from the open function, so I didn't notice...;P
And now I have a new problem...how do you associate a file extension with a particular program?!
|
|
|
|
|
I hope ur using an sdi/mdi application. You can find the option in the appwizard itself. How ever you can set the file associate later by following the below steps.
Suppose in the case of SDI application if you take the string table, you can find one entry with ID IDR_MAINFRAME.If my application name is MyApp, the string may look like
MyApp\n\nMyApp\n\n\nMyApp.Document\nMyApp Document. If you want to associate the file type say ".xyz", modify the string as follows
MyApp\n\nMyApp\nMyApp Files (*.xyz)\n.MyApp\nMyApp.Document\nMyApp Document. Also call the function RegisterShellFileTypes(TRUE); in the Initinstance just after the creation of the template.
|
|
|
|
|
hxhl95 wrote: ...but if the user just enters foo and selects .txt extension from the dropdown, I cannot seem to get the extension
Because NULL was used as the second argument to the CFileDialog constructor.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
I am not using MFC; just pure win32 at the moment. Is there something like a font dialog box? If there is then how to I call it and set my font after disposing the dialog...?
|
|
|
|
|
Use the ChooseFont() API to show the common font chooser dialog.
|
|
|
|
|
Font Dialog Box[^]
CreateFontIndirect() can be used to create a font from the LOGFONT obtained with the dialog.
For setting a control's font there's WM_SETFONT.
SelectObject() can select a font into a device context.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
hi, i wrote a mfc dialog that's running a worker thread i want to terminate it from outside.
in CMyTestDlg: public CDialog{
static bool STOP;//a shared variable to signal the thread function
static UINT __cdecl Start();
CWinThread* pThread
....
in the cpp file, i have a OnBnClickedButtonStart() and OnBnClickedButtonStop()function to start and stop the thread
bool CMyTestDlg::Stop=false;
void CMyTestDlg::OnBnClickedButtonStart(){
pThread=AfxBeginThread(Start,this,-1,0,CREATE_SUSPENDED);
pThread->m_bAutoDelete=FALSE;
pThread->ResumeThread();
}
void CMyTestDlg::OnBnClickedButtonStop()
{
STOP=true;
if(::WaitForSingleObject(pThread->m_hThread,2000)==WAIT_OBJECT_0){
delete pThread;
m_ListBox_MSG.AddString(_T("Thread terminated succesfully"));
//message showed in a list box
}
else{
::TerminateThread(pThread->m_hThread,0);
m_ListBox_MSG.AddString(_T("Thread is not normally terminated!"));
//message showed in a list box
}
}
UINT __cdecl CMyTestDlg::Start(LPVOID pParam){
CMyTestDlg* me=(CMyTestDlg*)pParam;
while(STOP==false){
//looping here...
}
return 0;
}
when i stop the thread, from the message showed in the listbox, i found that the thread is always terminated "dangeroursly" which means the ::TerminateThread()function ran.
What could be the possible cause?
|
|
|
|