|
If you dont make a return of your "T x" and you declare only "T a" and "T b" in your main... how can the programm do the operation? Try...
template
T CProg::AddValues(T a, T b)
{
T x = a+b;
return (T) x;
}
and...
void main()
{
CProg prog;
prog.SetVal(9);
int a,b, c;
a = b =8;
c = prog.AddValues(a,b);
}
What does now the programm say?
EDIT:
I have just realiced that the error you told has not much to do with my answer (but anyways is something it could fail). This code is from Simon Huge's template to a smart CList.
template<class TYPE, class ARG_TYPE>
inline CMyList<TYPE, ARG_TYPE> operator + (const CMyList<TYPE, ARG_TYPE> &lhs, const CMyList<TYPE, ARG_TYPE> &rhs)
{ CMyList newList(lhs);
newList.AddTail(rhs);
return newList;
}
Maybe it gives you an idea
-- modified at 3:00 Tuesday 31st July, 2007
Greetings.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
|
|
|
|
|
Thanks for your replay Nelek < >
But code given by you give same compile-time error.
Is there any another solution?
|
|
|
|
|
I didn't understand what you want to say.
Will you elobarate this?
|
|
|
|
|
As you have written member function as inline, i change code as following,
<br />
class CProg<br />
{<br />
int val;<br />
public :<br />
void SetVal(int y);<br />
template<class T><br />
void AddValues(T a, T b)<br />
{<br />
T x = a+b;<br />
}<br />
};<br />
void CProg::SetVal(int y)<br />
{<br />
val = y;<br />
}<br />
<br />
int main(int argc, char* argv[])<br />
{<br />
CProg prog;<br />
prog.SetVal(9);<br />
int a,b;<br />
a = b =8;<br />
prog.AddValues(a,b);<br />
double x, y<br />
x = 12.1212;<br />
y = 12.446;<br />
prog.AddValues(x,y);<br />
return 0;<br />
}
Above code is working properly. Thanks for that.
But eventhough, is there any other way for non-inline memeber function for class to use as Function Template ?
|
|
|
|
|
If it works... cool
but actually... I edited the message because of the < class T >, I forgot the empty spaces so the web took them as format snippets.
Anyways... The inline statement is not needed. The only thing is that for so short functions is better because the compiler add the inline function wherever it is called, insteads of referencing and going to read the code everytime it is called.
It should work as well without the inline.
I'm glad helping
Greetings.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
|
|
|
|
|
Nelek wrote: The inline statement is not needed. [...] It should work as well without the inline.
Probably not. The Compiler must see the definition of the template at every point where a new template is instantiated.
And as tou almost newer include CPP-files, the template body must be in the header.
Failure is not an option - it's built right in.
|
|
|
|
|
I didn't say "put the template in the .cpp". I said that the same template function should work as well defined as inline as not defined as inline. It is already clear that all template functions have to go in headers.
In my article the functions I added to the SmartList are not inline and works perfectly.
Greetings.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
|
|
|
|
|
Try with...
template< class T >
class CProg
{
public :
int val;
void SetVal(int y);
void AddValues(T a, T b);
};
template< class T >
inline void CProg< class T >::SetVal(int y)
{
val = y;
}
template< class T >
inline T CProg< class T >::AddValues(T a, T b)
{
return (T) (a + b);
}
EDIT: Some corrections, like empty space between < > and class T, so the web doesn't take them as labels for format.
Greetings.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
|
|
|
|
|
I'm having a Cstring buffer that I'm accumulating for n iterations. At the end of each iteration I add \r\n to the end of buffer. After end of all the iterations I'm writing the CString buffer to the CFile object. I'm seeing hre two things.
1) The characters occupy double space may be two bytes. So the look i expanded rather than the normal.
2) Instead of \r\n the file contains some junk square characters. Also the file is not written with full contents.
The project is build in VS2005 with unicode. IS it mandatory to build a project with the default unicode support in VS2005.
|
|
|
|
|
1 is probably due to unicode. As for 2., post your writing code.
tom groezer wrote: IS it mandatory to build a project with the default unicode support in VS2005
Not that I know of.
|
|
|
|
|
tom groezer wrote: IS it mandatory to build a project with the default unicode support in VS2005
I think it is not mandatory, but VS2005 default to UNICODE settings.
|
|
|
|
|
Dear all,
Recently i have faced an interview .... so there the interviewer asked me.. have u knowledge of COM+ ... so what should i have to answer...
Now i am telling about me...
I am using the dlls(COM component) just for doing some logical operation from ASP page.or when i need the acess for the data base.. that time also from asp page only
so please tell me during interview what should i tell... about my work knowledge....
Tasu
Tasu
|
|
|
|
|
The more diplomatic answer is: I know the concepts but I have not used in realtime projects
|
|
|
|
|
Completely agree
Greetings.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
|
|
|
|
|
tasumisra wrote: so please tell me during interview what should i tell... about my work knowledge....
If you have to ask, then you don't know COM.
"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
|
|
|
|
|
Hi,
I use WinXP. I noticed that if we create bursh or pen object and destroy them, the GDI object count that is seen in the task manager is not decreasing.
What I did is,
I created 20 brushes in a button click. Then the GDI object count is increased from 28 to 48. Then destroyed the objects in another button click. Now the GDI object count is 38. But if click the first button again the count is getting increased to only 48, means the objects are already destroyed but the count is not updated correctly.
Why this behaviour?
The code sample:
HGDIOBJ hBr[20];<br />
void CMyDlg::OnButtonCreate() <br />
{<br />
for( int i = 0; i < 20; i++ ) hBr[i] = CreateSolidBrush( 0 );<br />
}<br />
<br />
void CMyDlg::OnButtonDestroy() <br />
{<br />
for( int i = 0; i < 20; i++ ) DeleteObject( hBr[i] );<br />
}<br />
I tried it for pen objects, then the result is 28 to 48 on creation and 48 to 36 on destruction. But the 48 is stable for each creation similar to the brush, if the destruction is done for each creation.
But for font objects it is working fine. That is 28 to 48 on creation and 48 to 28 on destruction.
- NS -
|
|
|
|
|
Where GDI object count is displayed in the task manager? I have not seen before
|
|
|
|
|
Please use menu to enable that. View > Select Columns...
- NS -
|
|
|
|
|
HFILE hFile = OpenFile( szPath , ofStruct , OF_READWRITE )
how to close this hFile pointer ?
thanks
|
|
|
|
|
It clearly mentioned in documentation, this API is for compatibility with 16bit OS.
Use CreateFile instead.
And CloseHandle to close it.
Prasad
MS MVP - VC++
|
|
|
|
|
i alswo want to test OF_EXIST
HFILE hFile = OpenFile( szPath , ofStruct , OF_EXIST )
and
ofStruct.fFixedDisk
so i dont want to use CreateFile.
|
|
|
|
|
Check value return of this file (hFile).
|
|
|
|
|
Hi,
Can we change the size of the default scrollbar for a particular window (control) only?
For example, I need to change the default scrollbar size of the edit box leaving all other controls' scrollbars unchanged.
Size means width of the vertical scrollbar and height of the horizontal scrollbar.
Thanks in advance.
- NS -
|
|
|
|
|
|
Thanks.
I already had look at this article. But it is just for listbox. It is done by hiding the default and place a new one.
Actually i need to change the default scrollbar size.
- NS -
|
|
|
|