|
ns ami wrote: Wrong. vector is a template.
I forgot. Corrected now
Thanks for pointing
|
|
|
|
|
I assume you mean std::vector . If so then try something like this:
template <typename T>
inline void SomeFunction(std::vector<T> &vec)
{
}
Steve
|
|
|
|
|
Awesome this works just as intended !!!!!!!
Many thanks Stephen!
|
|
|
|
|
I am trying to do the same for a list, but cant get the iterator pointer to use
the generic type T:
<br />
template<class t=""> inline void showListSizeAndContents(list<t> lst) {
cout << "Size = " << lst.size() << endl;<br />
cout << "List Contents: ";<br />
list<t>::iterator p = lst.begin();
while(p != lst.end()) {<br />
cout << *p;<br />
p++;<br />
}<br />
<br />
return;<br />
}<br />
<br />
<br />
</t></t></class>
|
|
|
|
|
Your post is malformed. For example, I can't see and angle brackets after the template keyword. I'll have a go anyway but please revise your posts and make sure they are readable. Try something like this:
template <typename T>
inline void showListSizeAndContents(const list<T> &lst)
{
cout << "Size = " << lst.size() << endl;
cout << "List Contents: ";
list<T>::const_iterator e = lst.end();
for (list<T>::const_iterator i=lst.begin(); i!=e; ++i)
{
cout << *i << " ";
}
cout << endl;
}
Steve
|
|
|
|
|
Thanks Steve that works perfectly!!
gvanto
|
|
|
|
|
You may need to derive from vector for flexibility. And can write the code for "showVectorSizeAndContents" inside that.
- ns ami -
|
|
|
|
|
Hi All
How can i copy folder?I have a folde A whic have 9 file and 4 folder.And detination folder is B.I want to copy folder A in B.Plz help me
|
|
|
|
|
I haven't tried it myself, but I'm sure SHFileOperation will do this for you.
«_Superman_»
|
|
|
|
|
|
Davitor wrote: can you give example?
Since he's given you the name of the function to use, why not just search for an example?
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
|
|
|
|
|
|
Try this code
bool CMainFrame::CopyFun(LPCTSTR lpctszSrc_i,LPCTSTR lpctszDetn_i)
{
int nSrcLen = _tcslen(lpctszSrc_i);
int nDetnLen = _tcslen(lpctszDetn_i);
TCHAR *pszFrom = new TCHAR[nSrcLen];
TCHAR *pszTo = new TCHAR[nDetnLen];
_tcscpy(pszFrom, lpctszSrc_i);
_tcscpy(pszTo, lpctszDetn_i);
pszFrom[nSrcLen] = 0;
pszFrom[nSrcLen+1] = 0;
pszTo[nDetnLen] = 0;
pszTo[nDetnLen+1] = 0;
SHFILEOPSTRUCT stFileop;
if("Copy" == m_csType )
{
stFileop.wFunc = FO_COPY;
}
else
{
stFileop.wFunc = FO_MOVE;
}
stFileop.pFrom = pszFrom;
stFileop.pTo = pszTo;
stFileop.fAnyOperationsAborted = FALSE;
stFileop.lpszProgressTitle = NULL;
stFileop.hNameMappings = NULL;
int ret = SHFileOperation(&stFileop);
return(ret == 0);
}
Here lpctszSrc_i & lpctszDetn_i are the src & detn full path.
|
|
|
|
|
I'm having a problem with getting a template class's code split into a header and source file. The following three files below simply refuse to compile, error (using Eclipse Ganymede on Kubuntu Hardy Heron) below.
W.r.t. the 3 files below, when I include the GenArray.cpp 's code INTO the GenArray.h file or include both GenArray.h and .cpp 's contents into the TestDemo, it works
just fine! Any help on how to use this header file correctly would be greatly appreciated !
gvanto
[CODE]
Compiler output:
make all
Building file: ../src/GenArray.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/GenArray.d" -MT"src/GenArray.d" -o"src/GenArray.o" "../src/GenArray.cpp"
Finished building: ../src/GenArray.cpp
Building file: ../src/TestDemo.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/TestDemo.d" -MT"src/TestDemo.d" -o"src/TestDemo.o" "../src/TestDemo.cpp"
Finished building: ../src/TestDemo.cpp
Building target: STLTest
Invoking: GCC C++ Linker
g++ -o"STLTest" ./src/GenArray.o ./src/TemplateClassDemo.o ./src/TemplateFunctionDemo.o ./src/TestDemo.o ./src/VirtualClassDemo.o ./src/rectangle.o
./src/TestDemo.o: In function `main':
/home/pacific/workspace/STLTest/Debug/../src/TestDemo.cpp:21: undefined reference to `GenArray<int, 10>::operator[](int)'
/home/pacific/workspace/STLTest/Debug/../src/TestDemo.cpp:22: undefined reference to `GenArray<int, 10>::operator[](int)'
/home/pacific/workspace/STLTest/Debug/../src/TestDemo.cpp:25: undefined reference to `GenArray<double, 15>::operator[](int)'
/home/pacific/workspace/STLTest/Debug/../src/TestDemo.cpp:26: undefined reference to `GenArray<double, 15>::operator[](int)'
collect2: ld returned 1 exit status
[/CODE]
[CODE]
/*
* TestDemo.cpp
*
* Created on: 1/02/2009
* Author: pacific
*/
#include <iostream>
#include <cstdlib>
#include "GenArray.h"
using namespace std;
int main() {
GenArray<int, 10> intob; //int array of size 10
GenArray<double, 15> doubleob; //double array of size 15
int i;
cout << "Integer array: ";
for(i=0; i<10; i++) intob[i] = i;
for(i=0; i<10; i++) cout << intob[i] << " ";
cout << '\n';
cout << "Double array: ";
for(i=0; i<15; i++) doubleob[i] = (double) i/3;
for(i=0; i<15; i++) cout << doubleob[i] << " ";
return 0;
}
[/CODE]
[CODE]
/*
* GenArray.h
*
* Created on: 1/02/2009
* Author: pacific
*/
//A Generic Safe-Array
#include <iostream>
#include <cstdlib>
using namespace std;
template <class AType, int size> class GenArray {
AType a[size]; //length of array is passed in size
public:
GenArray() {
register int i;
for(i = 0; i < size;i++) {
a[i] = i;
}
}
AType &operator[](int i);
};
[/CODE]
[CODE]
/**
* Source for GenArray.h
*/
#include "GenArray.h" //this already includes iostream and stuff
//Provide range-checking for GenArray
template <class AType, int size> AType &GenArray<AType, size>::operator[] (int i) {
if(i<0 || i> size-1) {
cout << "\nIndex value of ";
cout << i << " is out-of-bounds.\n";
exit(1);
}
return a[i];
}
[/CODE]
Get A job in Australia now, Free!
http://www.webcv.com.au
|
|
|
|
|
When template classes are instantiated, a new class is created at compile time.
For example, GenArray<int, 10> intob; will create a new class.
Internally this class will have some mangled name (maybe .?AV?$GenArray@H$09@@ )
And GenArray<double, 15> doubleob; will create another class (maybe .?AV?$GenArray@N$0P@@@ )
So now you have 2 different classes.
In order to create a new class, the compiler needs the full information about the template class.
Since these classes are created when compiling the main function in the file TestDemo.cpp, the whole template information is necessary in the TestDemo.cpp file.
So you either put the whole template class in the GenArray.h file or include both GenArray.h and GenArray.cpp in TestDemo.cpp.
You will notice that template libraries like STL, ATL etc. have only .h files with all template class information in the header file iteself.
«_Superman_»
|
|
|
|
|
Hi Superman,
Wow thanks a million for a great response! This makes sense ...
I guess I've just been taught never to use executable code in a header file (purely for prototype purposes)
but I guess template classes are the exception eh?!
Thanks again!
Gvanto
|
|
|
|
|
Hi,
i work in c++ using Visual Studio 2005.
I'd like to know if the assignment operation to int (like int i; i=5; ) is atomic. If yes, can you show me proof for that?
It is the same in x64 OS?
|
|
|
|
|
it can be, but i doesn't have to.
if you want to be sure use the volatile keyword.
[code]
volatile int test;
int main(int argc, const char *argv[])
{
test = 5; // this is atomic!
}
[/code]
Don't try it, just do it!
|
|
|
|
|
Why the volatile keyword make it atomic?
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.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Definitely not.
Anyone who thinks he has a better idea of what's good for people than people do is a swine.
- P.J. O'Rourke
|
|
|
|
|
Not true.
All volatile does is to tell the compiler to write the value back to memory when it's changed AND that something else may change the variable. Without volatile, under some circumstance it could optimize it a register and only write and/or fetch it periodically.
Anyone who thinks he has a better idea of what's good for people than people do is a swine.
- P.J. O'Rourke
|
|
|
|
|
Dudi Avramov wrote: I'd like to know if the assignment operation to int (like int i; i=5; ) is atomic.
No, absolutely not. There is much more to atomic operations.
1.)The LOCK[^] prefix must be present to instruct the processor that it has exclusive memory access to the variable location.
2.) Modern processors perform speculative fetching and instruction reordering so the code being executed should be serialized before the atomic operaration with SFENCE[^]/LFENCE[^]/MFENCE[^] The volatile keyword[^] should give the compiler a hint that it needs to prevent out-of-order execution which may result in a Memory Barrier[^].
Interlocked Variable Access[^]
Synchronization and Multiprocessor Issues[^]
Best Wishes,
-David Delaune
|
|
|
|
|
Is it possible that the mov assembly instruction is not atomic?
|
|
|
|
|
It is possible that it's not atomic - if the processor doesn't have the bus locked while performing the MOV, some other bus attached hardware (like, for example, the other core of the processor, or some hardware performing a DMA) could theoretically write to that memory location if it has precedence over the current processor.
|
|
|
|
|
can u plz advice me how i can decode pdf format and add it into device..that it gonna used as firmware... it is a prestigious matter for me because i was insulted bitterly in a electronics forum for this question of making a pdf reader device.. so it became a challenge to make it possible.. can u just help me to establish my dream??
Every man is unique
|
|
|
|