abulka
asked on
cast to int + return a LPSIZE
There are 2 errors in the following function. I am attempting to cast Text.length to an integer - how? Also I want to correctly return a LPSIZE - how?
LPSIZE CNodeView1::TextExt(HDC dc, string Text)
{
LPSIZE res;
GetTextExtentPoint32(dc, Text.c_str(), (integer) Text.length, res);
return res;
}
Note that this is the official definition of the GetTextExtentPoint32 function:
BOOL GetTextExtentPoint32(
HDC hdc, // handle to device context
LPCTSTR lpString, // pointer to text string
int cbString, // number of characters in string
LPSIZE lpSize // pointer to structure for string size
);
Finally - should I be passing in char * to make my life easier?
thanks
LPSIZE CNodeView1::TextExt(HDC dc, string Text)
{
LPSIZE res;
GetTextExtentPoint32(dc, Text.c_str(), (integer) Text.length, res);
return res;
}
Note that this is the official definition of the GetTextExtentPoint32 function:
BOOL GetTextExtentPoint32(
HDC hdc, // handle to device context
LPCTSTR lpString, // pointer to text string
int cbString, // number of characters in string
LPSIZE lpSize // pointer to structure for string size
);
Finally - should I be passing in char * to make my life easier?
thanks
First, casting the length should be - (int)
Second, your variable - res - needs to be defined as:
SIZE res;
Your function call becomes ...
GetTextExtentPoint32(dc, Text.c_str(), (int) Text.length, &res);
Give this a try.
Second, your variable - res - needs to be defined as:
SIZE res;
Your function call becomes ...
GetTextExtentPoint32(dc, Text.c_str(), (int) Text.length, &res);
Give this a try.
or
SIZE size;
LPSIZE res;
LPSIZE CNodeView1::TextExt(HDC dc, string Text)
{
GetTextExtentPoint32(dc, Text.c_str (),Text.length, &size);
return &res;
}
SIZE size;
LPSIZE res;
LPSIZE CNodeView1::TextExt(HDC dc, string Text)
{
GetTextExtentPoint32(dc, Text.c_str (),Text.length, &size);
return &res;
}
Ignore last comment,I miswrite.
I mean:
private:
SIZE size;
LPSIZE res;
.............
LPSIZE CNodeView1::TextExt(HDC dc, string Text)
{
GetTextExtentPoint32(dc, Text.c_str (),Text.length, &size);
return &size;
}
...............
res=TextExt(...);
I mean:
private:
SIZE size;
LPSIZE res;
.............
LPSIZE CNodeView1::TextExt(HDC dc, string Text)
{
GetTextExtentPoint32(dc, Text.c_str (),Text.length, &size);
return &size;
}
...............
res=TextExt(...);
ASKER
Oh yeah - I want to use the above function like this:
long LineHeight = TextExt(mydc, "Aj").cy;
long LineHeight = TextExt(mydc, "Aj").cy;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Wyn - looks like I actually don't need to declare
LPSIZE res;
since the function is not using it?
Also, when I typed in
LPSIZE CNodeView1::TextExt(HDC dc, string Text)
{
GetTextExtentPoint32(dc, Text.c_str(), (int) Text.length, &size);
return &size;
}
and compiled it I found that I had to includ an integer cast - cos I got a compile error. Even so, I am still getting an error re that attempted cast!!
CNodeView1.cpp(553) : error C2440: 'type cast' : cannot convert from 'unsigned int (__thiscall std::basic_string<char,str uct std::char_traits<char>,cla ss std::allocator<char> >::*' to 'int'
Conversion is a valid standard conversion, which can be performed implicitly or by use of static_cast, C-style cast or function-style cast
LPSIZE res;
since the function is not using it?
Also, when I typed in
LPSIZE CNodeView1::TextExt(HDC dc, string Text)
{
GetTextExtentPoint32(dc, Text.c_str(), (int) Text.length, &size);
return &size;
}
and compiled it I found that I had to includ an integer cast - cos I got a compile error. Even so, I am still getting an error re that attempted cast!!
CNodeView1.cpp(553) : error C2440: 'type cast' : cannot convert from 'unsigned int (__thiscall std::basic_string<char,str
Conversion is a valid standard conversion, which can be performed implicitly or by use of static_cast, C-style cast or function-style cast
ASKER
alexo - your code also fails on the line with the integer cast thus:
D:\My Documents\aa Vc\ts1\CNodeView1.cpp(561) : error C2065: 'integer' : undeclared identifier
D:\My Documents\aa Vc\ts1\CNodeView1.cpp(561) : error C2146: syntax error : missing ')' before identifier 'Text'
D:\My Documents\aa Vc\ts1\CNodeView1.cpp(561) : error C2059: syntax error : ')'
D:\My Documents\aa Vc\ts1\CNodeView1.cpp(561)
D:\My Documents\aa Vc\ts1\CNodeView1.cpp(561)
D:\My Documents\aa Vc\ts1\CNodeView1.cpp(561)
->Wyn, don't EVER return an address of an automatic (local on the stack) variable!
========================== ========
Yes,I mean res and I rewrite one later just for this. But you'v got to admit at the very point the function returns,the value is still there on stack,although become tragedy later.
==========================
Yes,I mean res and I rewrite one later just for this. But you'v got to admit at the very point the function returns,the value is still there on stack,although become tragedy later.
Yeah, we all missed on the (int) cast
should be (int) Text.length()
should be (int) Text.length()
wylliker,I think in your first comment You already have the code,(int)Text.length().Yo u dont miss it:)
->looks like I actually don't need to declare
LPSIZE res;
since the function is not using it?
=================
Doesnt matter,just sample.
LPSIZE res;
since the function is not using it?
=================
Doesnt matter,just sample.
Wyn,
No, I did miss the () after length. As it is a member function - every example had incorrectly ...
(int) Text.length
instead of
(int) Text.length()
I also forgot about the return value being a pointer to a SIZE structure.
No, I did miss the () after length. As it is a member function - every example had incorrectly ...
(int) Text.length
instead of
(int) Text.length()
I also forgot about the return value being a pointer to a SIZE structure.
O,:)
I even dont realize the () after the length until I read your last comment:-)
I concentrate myself to other parts of code.:0
Yes,it's a function:)
Sorry,wylliker .....:)
I even dont realize the () after the length until I read your last comment:-)
I concentrate myself to other parts of code.:0
Yes,it's a function:)
Sorry,wylliker .....:)
Why return a LPSIZE??????
It's more logical to return a SIZE! If you return a pointer, you need a variable to hold the value, as a member in you're class or as a static...
Not neccesairy, the way to go is returning a SIZE
SIZE CNodeView1::TextExt(HDC dc, string Text)
{
SIZE res;
GetTextExtentPoint32(dc, Text.c_str(),Text.length, &res);
return res;
}
It's more logical to return a SIZE! If you return a pointer, you need a variable to hold the value, as a member in you're class or as a static...
Not neccesairy, the way to go is returning a SIZE
SIZE CNodeView1::TextExt(HDC dc, string Text)
{
SIZE res;
GetTextExtentPoint32(dc, Text.c_str(),Text.length, &res);
return res;
}
yup ,it's also my first way to go...The second I use &res.
ASKER
alexo - your code also fails on the line with the integer cast thus:
D:\My Documents\aa Vc\ts1\CNodeView1.cpp(561) : error C2065: 'integer' : undeclared identifier
D:\My Documents\aa Vc\ts1\CNodeView1.cpp(561) : error C2146: syntax error : missing ')' before identifier 'Text'
D:\My Documents\aa Vc\ts1\CNodeView1.cpp(561) : error C2059: syntax error : ')'
D:\My Documents\aa Vc\ts1\CNodeView1.cpp(561)
D:\My Documents\aa Vc\ts1\CNodeView1.cpp(561)
D:\My Documents\aa Vc\ts1\CNodeView1.cpp(561)
ASKER
Seems that
SIZE CNodeView1::TextExt(HDC dc, string Text)
{
SIZE res;
GetTextExtentPoint32(dc, Text.c_str(), (int) Text.length(), &res);
return res;
}
compiles correctly. I assume the
return res
is not returning the actual local variable (which will disappear off the call stack as soon as the function exits), but a copy of res. I hope so :-)
SIZE CNodeView1::TextExt(HDC dc, string Text)
{
SIZE res;
GetTextExtentPoint32(dc, Text.c_str(), (int) Text.length(), &res);
return res;
}
compiles correctly. I assume the
return res
is not returning the actual local variable (which will disappear off the call stack as soon as the function exits), but a copy of res. I hope so :-)
ASKER
Seems that
SIZE CNodeView1::TextExt(HDC dc, string Text)
{
SIZE res;
GetTextExtentPoint32(dc, Text.c_str(), (int) Text.length(), &res);
return res;
}
compiles correctly. I assume the
return res
is not returning the actual local variable (which will disappear off the call stack as soon as the function exits), but a copy of res. I hope so :-)
SIZE CNodeView1::TextExt(HDC dc, string Text)
{
SIZE res;
GetTextExtentPoint32(dc, Text.c_str(), (int) Text.length(), &res);
return res;
}
compiles correctly. I assume the
return res
is not returning the actual local variable (which will disappear off the call stack as soon as the function exits), but a copy of res. I hope so :-)
abulka, the (integer) cast was in the original code so I assumed it was correct (maybe a typedef to int?). It should really be (int).
Returning a local variable is no problem since you return BY VALUE. Tha is - a copy of the local variable. On the other hand, returning a POINTER to a local variable (or a REFERENCE) to it usually makes your program go BOOM!
Returning a local variable is no problem since you return BY VALUE. Tha is - a copy of the local variable. On the other hand, returning a POINTER to a local variable (or a REFERENCE) to it usually makes your program go BOOM!
ASKER
alexo seems to be the first one to get the closest to the answer. And of course, the correct typecast is (int) rather than (integer)
SIZE CNodeView1::TextExt(HDC dc, string Text)
{
SIZE res;
GetTextExtentPoint32(dc, Text.c_str(), (int) Text.length(), &res);
return res;
}
SIZE CNodeView1::TextExt(HDC dc, string Text)
{
SIZE res;
GetTextExtentPoint32(dc,
return res;
}
LPSIZE CNodeView1::TextExt(HDC dc, string Text)
{
SIZE res;
GetTextExtentPoint32(dc, Text.c_str(),Text.length, &res);
return &res;
}