From: Joseph M. Newcomer on
I agree; I have largely replaced my use of CArray with std::vector.

Note that the declartion is not CArray(type) but CArray<type>,

Reading afxtempl.h is about the worst possible way to learn about this, because templates
are not trivial aspects of C++ to work with.

What do you WANT to do with it?

The most relevant operations are
SetSize
GetSize
Add
GetAt
SetAt
SetAtGrow
[]

joe
****
On Fri, 7 May 2010 16:37:14 +0200, "Giovanni Dicanio"
<giovanniDOTdicanio(a)REMOVEMEgmail.com> wrote:

>"RB" <NoMail(a)NoSpam> wrote:
>
>> Hello, I am struggling to learn the concept of template language
>> and the usage of CArray. I have found the CArray declaration in
>> the afxtempl.h file and I have managed to compile with no errors
>> the apparent construction of
>
>May I suggest you to just go with STL std::vector?
>It is more powerful than CArray.
>For example, CArray does copies in a "naive" way using memcpy. This works
>correctly only for PODs (Plain Ol' Data) but not for more complex classes.
>Strange bugs may arise if you use CArray with non-POD classes.
>(I recall some of these bugs were discussed in this newsgroup and on some
>web forum.)
>
>Moreover, you can easily compose std::vector with other containers (e.g. you
>could simply build a vector<vector>).
>
>And vector has a better dynamically-growth policy than CArray (not bad if
>you store few items, but can make a difference for millions of items).
>CArray uses arithmetic growth, which has a very bad O(N^2) asympotic
>complexity; instead vector::push_back increases vector's capacity using a
>1.5x factor, offering an amortized O(1) time.
>
>
>> I am having a mental block with understanding the template
>> declaration of
>> template<class TYPE, class ARG_TYPE>
>> What exactly is this saying ? The constructor is just CArray( );
>
>Note that STL vector in its simplest form has only one template parameter,
>i.e. the type you store in the vector, e.g.
>
> vector<int> someIntegers;
> vector<MyStruct> collectionOfMyStructs;
>
>No need for the (useless and confusing, IMHO) ARG_TYPE thing.
>
>Using vector is very easy:
>
> #include <vector> // header file for std::vector
>
> // Creates an empty vector storing instances of MyClass
> std::vector<MyClass> data;
>
> // Add some stuff
> data.push_back( MyClass(...) );
> data.push_back( MyClass(...) );
> ...
>
>Use operator[] to retrieve vector elements (index is 0-based).
>
>And you can use resize() to change vector size.
>
>Other samples on using std::vector and description of its public methods can
>be easily found using your favourite search engine.
>
>HTH,
>Giovanni
>
Joseph M. Newcomer [MVP]
email: newcomer(a)flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
From: RB on
Thanks I decided to take your advice since the vector sounds so
much more versatile. However my compiler docs doesn't have much
on this either, just linked refrences to the same declarations I am
struggling to understand.
I did find one search page that does give some examples at
http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c4027
but my compiler doesn't want to compile some of the code samples.
So I am pretty much still stuck. Below are the examples that won't
compile (from the above link) . When I cut these out of my text, it
compiles with zero errors.

#include <vector>
//...
size_t size = 10; //<-error C2258: illegal pure syntax, must be '= 0'
std::vector<int> array(size); //<- error C2252: 'size' : pure specifier can only be specified for functions
// also error C2061: syntax error : identifier 'size'
for(int i=0; i<size; ++i) //<-error C2059: syntax error : 'for'
{array[i] = i;}
// compiles with 180 errors but I think the rest are result from the above first 3.

---------Also this example will not compile
#include <vector>
//...
std::vector<int> v(2); //<- error C2059: syntax error : 'constant'
int& first = v.front(); //<-error C2327: 'CFileHandlingDoc::v' : member from enclosing class is not a type name, static, or
enumerator
//error C2065: 'v' : undeclared identifier
//error C2228: left of '.front' must have class/struct/union type
int& last = v.back(); //<-error C2252: 'last' : pure specifier can only be specified for functions



From: Giovanni Dicanio on
"RB" <NoMail(a)NoSpam> ha scritto nel messaggio
news:#kKrnki7KHA.1316(a)TK2MSFTNGP02.phx.gbl...

>However my compiler docs doesn't have much
> on this either, just linked refrences to the same declarations I am
> struggling to understand.

What compiler do you use?
I recall VC6 had some problems with STL implementation, but using STLport
(open source implementation of STL) fixed several of them.
(Since VC7.1 there were big improvements.)


> I did find one search page that does give some examples at
> http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c4027
> but my compiler doesn't want to compile some of the code samples.
> So I am pretty much still stuck. Below are the examples that won't
> compile (from the above link) . When I cut these out of my text, it
> compiles with zero errors.
>
> #include <vector>
> //...
> size_t size = 10; //<-error C2258: illegal pure syntax,
> must be '= 0'

....it's like the compiler misinterprets 'size' like if it was a pure virtual
method?!

I believe you had some syntax errors.

Please post your entire C++ source code to have a better diagnosis.

Giovanni


From: Giovanni Dicanio on
"RB" <NoMail(a)NoSpam> ha scritto nel messaggio
news:#kKrnki7KHA.1316(a)TK2MSFTNGP02.phx.gbl...

> #include <vector>
> //...
> size_t size = 10; //<-error C2258: illegal pure syntax,
> must be '= 0'
> std::vector<int> array(size); //<- error C2252: 'size' : pure specifier
> can only be specified for functions
> // also error C2061: syntax
> error : identifier 'size'
> for(int i=0; i<size; ++i) //<-error C2059: syntax error : 'for'
> {array[i] = i;}
> // compiles with 180 errors but I think the rest are result from the above
> first 3.

This small test code compiles just fine with VC10:

<code>

#include <vector>
#include <iostream>

using namespace std;

int main()
{
size_t size = 10;
vector<int> array(size);

for (size_t i = 0; i < size; i++) {
array[i] = i;
}

for (size_t j = 0; j < size; j++) {
cout << array[j] << endl;
}

return 0;
}

</code>


C:\TEMP>cl /EHsc /W4 test.cpp

Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.30319.01 for
80x86
Copyright (C) Microsoft Corporation. All rights reserved.

test.cpp
Microsoft (R) Incremental Linker Version 10.00.30319.01
Copyright (C) Microsoft Corporation. All rights reserved.

/out:test.exe
test.obj

C:\TEMP>test.exe
0
1
2
3
4
5
6
7
8
9



Giovanni


From: RB on

> What compiler do you use?
> I recall VC6 had some problems with STL implementation, but using STLport (open source implementation of STL) fixed several of
> them.
> (Since VC7.1 there were big improvements.)

Yes I am using VC6 and unfortunately I like it but this appears to be yet another
reason to move on. I have two questions at this point.

1. Where can I get examples of this STLport ?

2. Can I install VS 2005 on the same machine without uninstalling my VC 6 ?