C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

simple compile-time error problem...

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
mark.van.dijk@perform-sol
Guest





PostPosted: Fri Mar 11, 2005 11:31 am    Post subject: simple compile-time error problem... Reply with 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
}


:-)


[ 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





PostPosted: Fri Mar 11, 2005 3:14 pm    Post subject: Re: simple compile-time error problem... Reply with quote



[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
}
Smile

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





PostPosted: Sat Mar 12, 2005 1:50 am    Post subject: Re: simple compile-time error problem... Reply with quote



[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





PostPosted: Sat Mar 12, 2005 1:53 am    Post subject: Re: simple compile-time error problem... Reply with quote

[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





PostPosted: Sat Mar 12, 2005 1:58 am    Post subject: Re: simple compile-time error problem... Reply with quote

[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





PostPosted: Sat Mar 12, 2005 2:05 am    Post subject: Re: simple compile-time error problem... Reply with quote

[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





PostPosted: Sat Mar 12, 2005 2:09 am    Post subject: Re: simple compile-time error problem... Reply with quote


[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





PostPosted: Tue Mar 15, 2005 3:56 pm    Post subject: Re: simple compile-time error problem... Reply with quote

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





PostPosted: Thu Mar 17, 2005 9:53 pm    Post subject: Re: simple compile-time error problem... Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.