| View previous topic :: View next topic |
| Author |
Message |
mark.van.dijk@perform-sol Guest
|
Posted: Fri Mar 11, 2005 11:31 am Post subject: simple compile-time error problem... |
|
|
Hi, whats wrong with the line marked below??? I don't get it...
{
std::vector<int>* tmp = new std::vector<int>(10);
//access the third element of the vector in various ways
int i1 = tmp->at(3); //ok
int i2 = tmp->operator[](3); //ok
int i3 = (*tmp)[3]; //ok
int i4 = tmp->[3]; //compile-time error
}
:-)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Vinzenz Feenstra Guest
|
Posted: Fri Mar 11, 2005 3:14 pm Post subject: Re: simple compile-time error problem... |
|
|
[email]mark.van.dijk (AT) perform-sol (DOT) com[/email] wrote:
| Quote: | Hi, whats wrong with the line marked below??? I don't get it...
{
std::vector<int>* tmp = new std::vector<int>(10);
//access the third element of the vector in various ways
int i1 = tmp->at(3); //ok
int i2 = tmp->operator[](3); //ok
int i3 = (*tmp)[3]; //ok
int i4 = tmp->[3]; //compile-time error
}
|
operator[]()s aren't usable with pointers.
| Quote: | int i1 = tmp->at(3); //ok
int i2 = tmp->operator[](3); //ok
int i3 = (*tmp)[3]; //ok
or int i4 = (*tmp).at(3); // will be ok |
but
int i4 = tmp->[3]; <- will never work in C++
BR
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Antoun Kanawati Guest
|
Posted: Sat Mar 12, 2005 1:50 am Post subject: Re: simple compile-time error problem... |
|
|
[email]mark.van.dijk (AT) perform-sol (DOT) com[/email] wrote:
| Quote: | Hi, whats wrong with the line marked below??? I don't get it...
{
std::vector<int>* tmp = new std::vector<int>(10);
//access the third element of the vector in various ways
int i1 = tmp->at(3); //ok
int i2 = tmp->operator[](3); //ok
int i3 = (*tmp)[3]; //ok
int i4 = tmp->[3]; //compile-time error
}
|
The syntax ->[3] is not part of the language.
--
A. Kanawati
[email]NO.antounk.SPAM (AT) comcast (DOT) net[/email]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Falk Tannhäuser Guest
|
Posted: Sat Mar 12, 2005 1:53 am Post subject: Re: simple compile-time error problem... |
|
|
[email]mark.van.dijk (AT) perform-sol (DOT) com[/email] wrote:
| Quote: | Hi, whats wrong with the line marked below??? I don't get it...
std::vector<int>* tmp = new std::vector<int>(10);
[...]
int i2 = tmp->operator[](3); //ok
int i3 = (*tmp)[3]; //ok
int i4 = tmp->[3]; //compile-time error
|
Why do you expect 'i3' and 'i4' to be equivalent?
Note that generally
foo->bar
is equivalent to
(*foo).bar
but not to
(*foo)bar
The right-hand side of '->' must designate a member of the object
pointed to by its left-hand side, and 'std::vector<int>' simply
has no member called '[3]' - it just has two member functions
int& std::vector<int>::operator[](std::vector<int>::size_type n);
int const& std::vector<int>::operator[](std::vector<int>::size_type n) const;
which can be designated by the name 'operator[]' and called using
function syntax (as in your initialisation of 'i2') or operator syntax
(as in your initialisation of 'i3').
When you check the list of possible postfix expressions in § 5.2/1,
there is just no valid match for 'tmp->[3]'. In particular, '[3]' is
not an id-expression.
Another question is why the vector was dynamically allocated in the
first place. C++ is not Java where class objects must be put on the
heap, and
std::vector<int> tmp(10);
or for a vector being a class member
struct foo
{
std::vector<int> tmp;
foo() : tmp(10) {}
};
are just fine!
Falk
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Hiroaki Ramone Guest
|
Posted: Sat Mar 12, 2005 1:58 am Post subject: Re: simple compile-time error problem... |
|
|
[email]mark.van.dijk (AT) perform-sol (DOT) com[/email] wrote in message news:<1110504289.119543.155400 (AT) g14g2000cwa (DOT) googlegroups.com>...
| Quote: | Hi, whats wrong with the line marked below??? I don't get it...
{
std::vector<int>* tmp = new std::vector<int>(10);
//access the third element of the vector in various ways
int i1 = tmp->at(3); //ok
int i2 = tmp->operator[](3); //ok
int i3 = (*tmp)[3]; //ok
int i4 = tmp->[3]; //compile-time error
}
:-)
|
tmp->[3] <=> (*tmp).[3] <=> (*tmp)..operator[](3)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
johnchx2@yahoo.com Guest
|
Posted: Sat Mar 12, 2005 2:05 am Post subject: Re: simple compile-time error problem... |
|
|
[email]mark.van.dijk (AT) perform-sol (DOT) com[/email] wrote:
| Quote: | Hi, whats wrong with the line marked below??? I don't get it...
{
std::vector<int>* tmp = new std::vector<int>(10);
//access the third element of the vector in various ways
int i1 = tmp->at(3); //ok
int i2 = tmp->operator[](3); //ok
int i3 = (*tmp)[3]; //ok
int i4 = tmp->[3]; //compile-time error
}
|
5.2.5/1 says that the arrow operator must be followed by an
id-expression. "[3]" is not an id-expression.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Randy Guest
|
Posted: Sat Mar 12, 2005 2:09 am Post subject: Re: simple compile-time error problem... |
|
|
[email]mark.van.dijk (AT) perform-sol (DOT) com[/email] wrote:
| Quote: | Hi, whats wrong with the line marked below??? I don't get it...
{
std::vector<int>* tmp = new std::vector<int>(10);
//access the third element of the vector in various ways
int i1 = tmp->at(3); //ok
int i2 = tmp->operator[](3); //ok
int i3 = (*tmp)[3]; //ok
int i4 = tmp->[3]; //compile-time error
}
:-)
|
Try:
int i4 = tmp->operator[](3);
You have a pointer to a std::vector and need to use it as such.
Randy.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Seungbeom Kim Guest
|
Posted: Tue Mar 15, 2005 3:56 pm Post subject: Re: simple compile-time error problem... |
|
|
Falk Tannhäuser wrote:
| Quote: | Another question is why the vector was dynamically allocated in the
first place. C++ is not Java where class objects must be put on the
heap, and
std::vector<int> tmp(10);
or for a vector being a class member
struct foo
{
std::vector<int> tmp;
foo() : tmp(10) {}
};
are just fine!
|
Good point. I don't think I've ever had to dynamically allocate a
container object, and such cases I've seen are almost always from those
who had a Java background.
--
Seungbeom Kim
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Mark van Dijk Guest
|
Posted: Thu Mar 17, 2005 9:53 pm Post subject: Re: simple compile-time error problem... |
|
|
Seungbeom Kim wrote:
| Quote: | Falk Tannhäuser wrote:
Another question is why the vector was dynamically allocated in the
first place. C++ is not Java where class objects must be put on the
heap, and
std::vector<int> tmp(10);
or for a vector being a class member
struct foo
{
std::vector<int> tmp;
foo() : tmp(10) {}
};
are just fine!
Good point. I don't think I've ever had to dynamically allocate a
container object, and such cases I've seen are almost always from those
who had a Java background.
|
Hmmm, i certainly don't have a Java background...
I have a low-level class XYZ that represents binary data stored in large
files on disk. Clients of XYZ can request various sized blocks of data
from this file. These blocks may exist im memory after the destruction
of the original XYZ object.
My first attempt has XYZ return std::vector<char> blocks, however the
rest of the application ended up copying these vectors multiple times as
this block was passed around my app. As the original XYZ object may no
longer exist, using references to std::vector<>'s is not acceptable.
The solution was to dynamically allocate these vectors and pass pointers
around instead. Of course, for safety, I don't use raw pointers, I
actually pass boost::shared_ptr<std::vector objects around
instead...
The boost library documentation also suggests that this is an acceptable
C++ idiom - see documemtation for boost::shared_array (paragraph 5),
where they suggest that boost::shared_ptr<std::vector<> > is a flexible
alternative to boost::shared_array's.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|