Compiler crash (VC2008 Prof)

A

Anna Smidt

Hello,

Can somebody tell me how to rewrite this in order to have the compiler
compile this without crashing?
Thanks a lot!
Anna

#ifndef cvec_hpp
#define cvec_hpp
#include<vector>

using namespace std; // so "vector" below is actually "std::vector", etc.

template<class T> class cvec : public vector<T>
{
public: // typenames like iterator are also inherited
typedef typename cvec::size_type size_type;
typedef typename cvec::iterator iterator;
typedef typename cvec::difference_type difference_type;
typedef typename cvec::reference reference;
typedef typename cvec::const_reference const_reference;

cvec() {}

cvec(size_type n, const T& value = T()): vector<T>(n, value) {}

cvec(iterator i, iterator j): vector<T>(i, j) {}

reference operator[](difference_type i)
{
DASSERT(i >=0 && i < static_cast<difference_type>(this->size()));
return vector<T>::blush:perator[](i);
}

const_reference operator[](difference_type i) const
{
DASSERT(i >=0 && i < static_cast<difference_type>(this->size()));
return vector<T>::blush:perator[](i);
}
};
#endif // cvec_hpppp
 
A

Anna Smidt

The compiler also says:

"To work around this problem, try simplifying or changing the program
near the locations listed above."
And that's where it crashes:

cvec(iterator i, iterator j): vector<T>(i, j) {}
 
D

David Lowndes

Anna,

Can you submit this as a bug on the MS connect site please and post
back a link to your bug report here so others can find it and
vote/verify it.

It's difficult to know what to recommend - the code snippet compiles
fine with the Comeau online compiler which is usually a good
indication that the code itself is not invalid.

The immediate crash can be eliminated by commenting out either of the
2 constructor forms, but whether you need them in your actual code I
don't know.

It also disappears for me if I change the typedef names (and their
usage):

size_type->size_typ, iterator -> iterat.

Dave
 
A

Anna Smidt

David, thanks for your reply.
-------------
Does it crash for you? It was not really clear because you said ->
It's difficult to know what to recommend - the code snippet compiles
fine with the Comeau online compiler
-------------
Do you know this from just looking at the code or have you been able to
reproduce the crash? ->
The immediate crash can be eliminated by commenting out either of the
2 constructor forms, but whether you need them in your actual code I
don't know.
It also disappears for me if I change the typedef names (and their
usage):

size_type->size_typ, iterator -> iterat.

Well, I don't know that either because I'm still too newbie. I did click
on "Send error report" quite a dozen times when the compiler crashed.
Should I post it on MS Connect anyway? I am still too new to C++ to be
able to describe the steps to reproduce them.

Anna
 
A

Anna Smidt

It also disappears for me if I change the typedef names (and their
usage):

size_type->size_typ, iterator -> iterat.

Dave

This helped! I am not yet sure if it breaks anything, but the crash is
gone!
Thanks very much.
 
D

David Lowndes

Do you know this from just looking at the code or have you been able to
reproduce the crash? ->

I could repro the crash with the VS2008 compiler, and I also checked
the code on http://www.comeaucomputing.com/tryitout since it's widely
regarded as being as conformant a compiler as there is.
Should I post it on MS Connect anyway?

Yes please - that way we'll get some feedback as to whether MS can
repro it - and possibly a recommended work-around/fix.

Please post the link from your Connect bug report back here and I'll
vote/validate it.

Dave
 
B

Bo Persson

Anna said:
Hello,

Can somebody tell me how to rewrite this in order to have the
compiler compile this without crashing?
Thanks a lot!
Anna

#ifndef cvec_hpp
#define cvec_hpp
#include<vector>

using namespace std; // so "vector" below is actually
"std::vector", etc.

This is generally not a good idea in a header file. Everyone including
this header will get a "using namespace std;", whether they like it or
not.
template<class T> class cvec : public vector<T>

This is not as bad as the line above, but very rarely a good idea.
{
public: // typenames like iterator are also inherited
typedef typename cvec::size_type size_type;
typedef typename cvec::iterator iterator;
typedef typename cvec::difference_type difference_type;
typedef typename cvec::reference reference;
typedef typename cvec::const_reference const_reference;

Here you define cvec::size_type to be ... eh... cvec::size_type. What
does that mean?

For sure, it confuses the compiler. :)

Did you mean

typedef typename std::vector<T>::size_type size_type;
etc

?
cvec() {}

cvec(size_type n, const T& value = T()): vector<T>(n, value) {}

cvec(iterator i, iterator j): vector<T>(i, j) {}

reference operator[](difference_type i)
{
DASSERT(i >=0 && i < static_cast<difference_type>(this->size()));
return vector<T>::blush:perator[](i);
}

Why difference_type and not size_type, if you want the index >=
anyway?

Have you checked out what vector<T>::at() already does for you?



Bo Persson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top