Public Group

Simple Error Help

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

Recommended Posts

Does anyone know how I can fix this?
	int Length = GetWindowTextLength(nWind);
wchar_t Buffer[Length];
GetWindowText(nWind, Buffer, sizeof(Buffer)/sizeof(Buffer[0]));

I keep getting these errors:
1>c:\users\matt\documents\visual studio 2008\projects\ustest\ustest\main.cpp(13) : error C2057: expected constant expression
1>c:\users\matt\documents\visual studio 2008\projects\ustest\ustest\main.cpp(13) : error C2466: cannot allocate an array of constant size 0
1>c:\users\matt\documents\visual studio 2008\projects\ustest\ustest\main.cpp(13) : error C2133: 'Buffer' : unknown size
1>c:\users\matt\documents\visual studio 2008\projects\ustest\ustest\main.cpp(14) : error C2070: 'wchar_t []': illegal sizeof operand


Share on other sites
The array size must be a compile time constant and cannot be evaluated at run time. Use a dynamic vector instead.
int Length = GetWindowTextLength(nWind);std::vector<wchar_t> Buffer(Length);GetWindowText(nWind, &Buffer[0], Length);

Share on other sites
Now this error:

1>c:\users\matt\documents\visual studio 2008\projects\ustest\ustest\main.cpp(17) : error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::vector<_Ty>' (or there is no acceptable conversion)

With this code:

	std::wofstream File;	File.open("Test.lua");	File << Buffer;	File.close();

Share on other sites
int Length = GetWindowTextLength(nWind);wchar_t Buffer = new wchar_t[Length];GetWindowText(nWind, Buffer, Length);

Remember to delete or use a smart_ptr.

Or using Brother Bob's example you might use
File << &Buffer[0];

Share on other sites
I got it to work:

	int Length = GetWindowTextLength(nWind);	wchar_t *Buffer = new wchar_t[Length+1];	GetWindowText(nWind, Buffer, Length+1);	std::wofstream File;	File.open("Test.lua");	File << Buffer;	File.close();	delete Buffer;

Any suggestions for anything I am doing sloppy? Thanks!

Share on other sites
You're using the wrong delete operator. It's delete [] Buffer;

Share on other sites
Stick with Bob's vector code if you can. Otherwise, remember to use delete [] Buffer (as you've allocated an array, not just a single object).

Share on other sites
Quote:
 Any suggestions for anything I am doing sloppy? Thanks!
Here's a slightly revised version of your code (not compiled or tested):

std::vector<wchar_t> buffer(GetWindowTextLength(nWind) + 1);GetWindowText(nWind, &buffer.front(), buffer.size());std::wofstream file("Test.lua");file << &buffer.front();

The main differences are:

1. Use of std::vector makes the code (more) exception safe, and obviates the need for manual cleanup and for 'remembering' the length of the buffer.

2. Opening the file using the wofstream constructor tightens the code up a bit.

3. The (most likely) unnecessary call to close() has been removed (I'm assuming that this code resides in a function or some other scoped block, and that the file object will therefore go out of scope soon after this code finishes executing).

There are probably some opportunities for error-checking there as well, but I didn't address them in my example.

[Edit: Some of these improvements were already suggested in earlier posts.]

Share on other sites
Thanks, and it did work.

1. 1
2. 2
3. 3
Rutin
21
4. 4
5. 5

• 14
• 30
• 13
• 11
• 11
• Forum Statistics

• Total Topics
631778
• Total Posts
3002308
×