|
Heh
Different opinions exist.
While I don't 100% follow their reasoning, the brightest minds on these issues recommend to:
* never use volatile (for threading purposes)
* for atomic data, use Interlocked functions
(If I understand everything correctly, here is where you could use volatile, too)
* otherwise, lock
The idea is (I think) that the Interlocked Functions more directly express what you intent to do, at a finer granularity.
However, volatile should work fine as well.
PLEASE keep in mind that in all the discussion I am on thin ice hear already. There are a few things that I need to read up again. It is all to the best of my knowledge and from memory, but still could be full of errors.
|
|
|
|
|
|
I've got a main cpp file that is including a header file (MyAlgorithms.h) where I'm playing around with doing algorithms the STL way with templates and iterators.
If I put the actual code for my template algorithm in the header file everything compiles and works fine. If I define it in my header file, yet put the code in its own cpp file (MyAlgorithms.cpp) I get a compile error.
I can also put the code for the template function in the main cpp file and then it compiles/runs fine.
What gives?
I can even put the code for a non-template normal old function in the MyAlgorithms.cpp file and define in the header and it's fine.
undefined reference to `int ajo::roffeltemplate<int>(int)' CppPlayground.cpp
In main:
<br />
#include <iostream><br />
#include <string><br />
#include <vector><br />
#include <algorithm><br />
#include <br />
<br />
#include "Util.h"<br />
#include "PracticalSocket.h"<br />
#include "Protocol.h"<br />
#include "MyAlgorithms.h"<br />
<br />
...<br />
<br />
<br />
int blah = ajo::roffeltemplate(1);
MyAlgorithms.h
#ifndef MYALGORITHMS_<br />
#define MYALGORITHMS_<br />
<br />
#include <string><br />
#include <vector><br />
#include <algorithm><br />
<br />
namespace ajo<br />
{<br />
<br />
#define ROFFEL 1<br />
<br />
void printroffel();<br />
<br />
template <typename t=""><br />
T roffeltemplate(T val);<br />
<br />
template <typename iter,="" typename="" t=""><br />
T sumsquares(Iter beg, Iter end, T init);<br />
<br />
<br />
}<br />
<br />
<br />
<br />
#endif /*MYALGORITHMS_*/<br />
</typename></typename></algorithm></vector></string>
MyAlgorithms.cpp
<br />
#include "MyAlgorithms.h"<br />
<br />
#include <iostream><br />
#include <string><br />
<br />
using namespace ajo;<br />
<br />
template <typename t=""><br />
T roffeltemplate(T val)<br />
{<br />
return val;<br />
}<br />
<br />
template <typename iter,="" typename="" t=""><br />
T sumsquares(Iter beg, Iter end, T init) <br />
{<br />
T result = init;<br />
for(;beg != end; ++beg) <br />
{<br />
result += (*beg) * (*beg);<br />
}<br />
return result;<br />
}<br />
<br />
void ajo::printroffel() <br />
{<br />
printf("roffel");<br />
}<br />
<br />
<br />
</typename></typename></string></iostream>
I don't think it's a make issue, although I am experimenting with Eclipse's auto-make. I could try writing the make myself like I usually do. I can force a compiler error in the MyAlgorithms files so I know they're being compiled.
|
|
|
|
|
Great... the forum ate everything in the code with angle brackets.
Also, this is compiled in linux with gnu compiler.
|
|
|
|
|
Please reformat you post.
ArmchairAthlete wrote: Great... the forum ate everything in the code with angle brackets.
Use <pre> tags to surround code, i.e.:
<pre>
code here
</pre>
and, for template, escape the < operator ( with < ).
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
ArmchairAthlete wrote: Great... the forum ate everything in the code with angle brackets.
So why not just fix your post, rather than create another one?
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
I want to write C program about CPU Scheduling simmulator
1. use mutilevel queue for this program
2. mix batch and interactive process
3. input process from manual keyboard write
4. Show output to dos screen
Thank you
|
|
|
|
|
whats stopping you ? you dont expect US to write it for you do you ?
There's plenty of examples here and on google that deal with such things - how about you do some research, play with some code, and come back and ask specific questions
oh, and please read the note at the top of the forum - this one :- http://www.codeproject.com/script/Forums/View.aspx?fid=1647&msg=2371639[^]
Your question also 'smells like' homework - we dont do people's homework for them - most of us got where we are by solid work and application, not by cheating, so we tend to look a bit disdainfully on this sort of thing - we will help you learn, but you have to do some research and apply yourself a bit first.
'G'
|
|
|
|
|
hi everyone...
am having a system tray icon.
if i right click the icon i have to get four option.
1.about
2.minimise
3.maximise
4.exit
upto here is over...
now the problem is if i clicked minimise that should be unvisible... like if i click maximise that should be unvisible...
can help anyone?
please tell somewhat brief... because am the beginner...
thanks in advance...
|
|
|
|
|
You can use ShowWindow with SW_HIDE and SW_SHOW
|
|
|
|
|
Hi all,
I have a template function inside a class a shown below. But while compiling I am getting an error "error C2893: Failed to specialize function template 'void __thiscall Test::someFunction(T)' With the following template arguments: 'int' ". Anybody knows whats wrong with the code?
class Test<br />
{<br />
public:<br />
template<class T> void someFunction( T obj );<br />
};<br />
<br />
template<class T> void Test::someFunction( T obj )<br />
{<br />
<br />
}<br />
<br />
int main(int argc, char* argv[])<br />
{ <br />
int n = 0;<br />
Test obj;<br />
obj.someFunction( n );
return 0;<br />
}
Also if I wrote the implementation of the template function inside the class itself, then there is no error. Like..
class Test<br />
{<br />
public:<br />
template<class T> void someFunction( T obj )
{<br />
<br />
}<br />
};
|
|
|
|
|
It should be:
template <class T>
class Test
{
void function(T);
};
Maxwell Chen
modified on Friday, December 28, 2007 4:42:31 AM
|
|
|
|
|
No no thats not possible. I only need to call the function with different types.
I think the problem is with the vc6. In vc8, it is working fine.
|
|
|
|
|
Naveen,
It's a bug of Microsoft Visual C++ (6.0) compiler. There's no workaround available.
They have corrected it in the newer versions.
Please check KB article[^]
-Sarath.
"Great hopes make everything great possible" - Benjamin Franklin
|
|
|
|
|
Oh I misunderstood your question. Sorry!
Maxwell Chen
|
|
|
|
|
Hi Naveen ,
You are right the code given by you is working on VC7 and onwards
But if you want to make it possible on VC6 then please refere the code given below and make the changes accordingly. Hope this will help you.
template<class T><br />
class Test<br />
{<br />
public:<br />
void someFunction( T obj );<br />
};<br />
<br />
template<class T> <br />
void Test<T>::someFunction( T obj )<br />
{<br />
<br />
}<br />
<br />
int main(int argc, char* argv[])<br />
{ <br />
int n = 0;<br />
Test<int> obj;<br />
obj.someFunction( n );
return 0;<br />
}
The secret of life is not enjoyment
but education through experience.
- Swami Vivekananda.
|
|
|
|
|
Hi Mahesh ,
The problem with your sugeestion is that, I cannot call the obj.someFunction() with both int and long at the same time like..
int n = 0;<br />
long l = 0;<br />
obj.someFunction( n ); <br />
obj.someFunction( l );
|
|
|
|
|
I think that Naveen wants (member) function templates of a class, not class templates .
If it is a class template, the argument type is fixed when the class object is declared. So we can not invoke member functions with different input types (something like function overloading).
Maxwell Chen
|
|
|
|
|
HI Naveen and MaxWell
I am really sorry ..It was my misunderstanding.
later I worked for wht Naveen exactly want, on VC6
But it was not working then i googled,
I found the Following link
http://forums.devx.com/archive/index.php/t-82949.html[^]
The secret of life is not enjoyment
but education through experience.
- Swami Vivekananda.
|
|
|
|
|
To tell the truth, I have been 7 years not coding template stuffs.
I just tried hard to recall everything about templates (both member function template and class template), and tried with VC++2008. It works well finally.
Maxwell Chen
|
|
|
|
|
How to resize bitmap which in CStatic
<code>
CStatic* myStatic;
myStatic = new CStatic();
myStatic->Create(_T("my static"), WS_CHILD|WS_VISIBLE|SS_BITMAP|SS_CENTERIMAGE, CRect(50,80, 150, 150), this);
myStatic->SetBitmap(LoadBitmap(AfxGetApp()->m_hInstance,MAKEINTRESOURCE(IDB_BITMAP_default)));
</code>
these code set bitmap in CStatic control, but the picture is not whole displaied, because its area more than CRect(50,80, 150, 150),
how to resize the bitmap area that to dispaly full onto the CStatic control?
thanks!
|
|
|
|
|
This control[^] does all the dirty work.
This article[^] shows what you can do with the static control. Not settng the SS_CENTERIMAGE or SS_REALSIZEIMAGE style supposedly makes the image fit the static control.
Note that the actual size of the static control in pixels, and even its aspect ratio depends on the dialog font, so it may be different when moving to another PC.
|
|
|
|
|
|
I define a class CArray2D,inside it I imeplement a "proxy" class CArray1D, so that if I create a object of CArray2D like CArray2D array(2,3), I can output the its element like cout<<array[0][0].
But when I run it, I met with serials of strange questions like error address.
Can anyone tell me why?
Many thanks
The code is as follows
class CArray2D
{
public:
void Print()
{
CArray1D *pHead = pArray1D;
for(int i=0; i<size2D; i++)
{
cout<<"[ "<<i<<" }";
pHead->Print1D();
pHead = pHead+i*sizeof(CArray1D);
cout<<endl;
}
}
CArray2D(int n2DWidth, int n1DWidth):size2D(n2DWidth)
{
pArray1D = new CArray1D[size2D];
};
~CArray2D()
{
delete[] pArray1D;
};
class CArray1D
{
public:
CArray1D(int n1DWidth=3):size1D(n1DWidth)
{
pArray = new int[size1D];
for(int i=0; i<size1D; i++)
{
*(pArray+i*sizeof(int)) = 0;
}
};
~CArray1D()
{
delete[] pArray;
};
const int& operator[](int nIndex) const
{
return *(pArray+nIndex*sizeof(int));
}
int& operator[](int nIndex)
{
return *(pArray+nIndex*sizeof(int));
}
void Print1D()
{
for(int i=0; i<size1D; i++)
{
cout<<"["<<i<<"]="<<*(pArray+i*sizeof(int))<<" ";
}
}
private:
int size1D;
int *pArray;
};
const CArray1D& operator[](int nIndex) const
{
return *(pArray1D+nIndex*sizeof(CArray1D));
};
CArray1D& operator[](int nIndex)
{
return *(pArray1D+nIndex*sizeof(CArray1D));
};
public:
int size2D;
CArray1D *pArray1D;
};
int main()
{
CArray2D array(2,3);
array.Print();
array[0][0] = 3;
array.Print();
cout<<array[0][0]<<endl;
return 0;
}
}
Tomorrow is another day
|
|
|
|
|
Hi,
I'm doing a CPU-intensive program right now (C++ WinAPI, no MFC but I could use it if I had to) that uses some graphics, frequently calling a FillRect function to clear its back-buffered HDC to white. Is there a more efficient way to do this? Maybe by using bitwise operators somehow?
Thanks!
KR
|
|
|
|