Searching \ for '[EE] I am sooo stupid with C++ std...' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/language/index.htm?key=c%2B%2B
Search entire site for: 'I am sooo stupid with C++ std...'.

Exact match. Not showing close matches.
PICList Thread
'[EE] I am sooo stupid with C++ std...'
2006\03\23@221538 by James Newtons Massmind

face picon face
Why doesn't this work? Two days of reading everything I can find, pounding
my head against the wall and I can't make heads or tails of it. Could have
had it done in an hour if I was using C (malloc and atoi and all that rot)
but I wanted to try and use C++ ideas and standard library stuff. I thought
I understood C++ to some degree. Objects, information hiding, inheritance,
operator overloading... I got all that. But this standard library stuff is
just... Like a whole 'nother language.

Here is as close as I can get to something that almost compiles, but it
doesn't. "delete can not convert from tRePosition to void *"


//move any element in one of the src areas to the associated dst area

struct tRePosition {
       struct {
               int x1,x2;
               int y1,y2;
               } src;
       struct {
               int x;
               int y;
               } dst;
       };

struct delete_RePosition : std::unary_function<tRePosition,void>{
   void operator()(tRePosition p) const {
       delete [] p;
               }
       };

std::vector<tRePosition> RePositions;

std::ifstream fin("D:\\input.txt");
//file with some number of lines each with 6 integers seperated by spaces
while (!fin.eof()) {
       tRePosition *RePosition = new tRePosition;
       fin >> RePosition->src.x1;
       fin >> RePosition->src.y1;
       fin >> RePosition->src.x2;
       fin >> RePosition->src.y2;
       fin >> RePosition->dst.x;
       fin >> RePosition->dst.y;
       RePositions.push_back(*RePosition);
       }
/*        
 Do stuff with the RePosition 's in RePositions. For example: Given an
element with an X and Y position (PosX, PosY)
*/
for(i=RePositions.size(); i>0; i--) {
/*
This should be a function so for_each could be used, but I cant even get
the delete function to work in for_each...
*/
       if ( PosX >= RePositions[i].src.x1
         && PosX <= RePositions[i].src.x2
         && PosY >= RePositions[i].src.y1
         && PosY <= RePositions[i].src.y2 ) {
//...

               }
       }


// Then...

std::for_each(RePositions.begin(), RePositions.end(), delete_RePosition() );

---
James Newton, massmind.org Knowledge Archiver
spam_OUTjamesTakeThisOuTspammassmind.org 1-619-652-0593 fax:1-208-279-8767
http://www.massmind.org Saving what YOU know.

2006\03\23@225906 by Matthew Miller

flavicon
face
Hi James. I think I see some problems. I've interspersed my comments in your
code...

On Thu, Mar 23, 2006 at 07:15:35PM -0800, James Newtons Massmind wrote:
{Quote hidden}

Your declaring a vector of tRePosition structs. To later be able to call
delete on the elements you must use the type *tRePosition for the
vector. You can't call delete on the object itself, but only on a pointer to
it. Also, since you're not deleting an array of tRePosition objects you
shouldn't call delete with [].

{Quote hidden}

I don't see anything else that might be wrong. The compiler error you
reported doesn't seem to match up with what I see here, but I've seen
compilers give incomprehensible errors and warnings before.

Take care.

Matthew

--
"It is a miracle that curiosity survives formal education"
    -- Albert Einstein

2006\03\24@171728 by James Newtons Massmind

face picon face
Matthew Miller Sent: 2006 Mar 23, Thu 19:59
> Hi James. I think I see some problems. I've interspersed my
> comments in your code...
>
> On Thu, Mar 23, 2006 at 07:15:35PM -0800, James Newtons
> Massmind wrote:
<SNIP>
> >
> > std::vector<tRePosition> RePositions;
>
> Your declaring a vector of tRePosition structs. To later be
> able to call delete on the elements you must use the type
> *tRePosition for the vector. You can't call delete on the
> object itself, but only on a pointer to it. Also, since
> you're not deleting an array of tRePosition objects you
> shouldn't call delete with [].

Oh my... Ok, so I've changed it to

std::vector<tRePosition *> RePositions;

And taken out the [] and all seems to be well.

THANK YOU!

---
James.


2006\03\24@171836 by Gerhard Fiedler

picon face
Matthew Miller wrote:

> On Thu, Mar 23, 2006 at 07:15:35PM -0800, James Newtons Massmind wrote:
>>
>> struct tRePosition {
>>        struct {
>>                int x1,x2;
>>                int y1,y2;
>>                } src;
>>        struct {
>>                int x;
>>                int y;
>>                } dst;
>>        };
>>
>> struct delete_RePosition : std::unary_function<tRePosition,void>{
>>     void operator()(tRePosition p) const {
>>         delete [] p;
>>                }
>>        };

James, when you cite an error message plus code, it helps to inform the
offending line :)

> The compiler error you reported doesn't seem to match up with what I see
> here, but I've seen compilers give incomprehensible errors and warnings
> before.

I may miss something (it's been some time with C++ :), but for me it does
match up (and matches your explanation of the problem).

It seems to be the delete operator above. It calls an operator delete
function that has as sole argument a void pointer. This (and the general
functionality of delete) requires that what you want to delete has to be a
pointer -- preferably with a content that came from a previous call to new.

p above is not a pointer, it is a struct tRePosition. p in "delete [] p"
can't be converted into a void*, because it is a tRePosition -- which is
what your error message seems to complain about. It probably would not
complain if p were tRePosition*.

Gerhard

2006\03\24@235921 by Matthew Miller

flavicon
face
On Fri, Mar 24, 2006 at 02:17:22PM -0800, James Newtons Massmind wrote:
>
> Oh my... Ok, so I've changed it to
>
> std::vector<tRePosition *> RePositions;
>
> And taken out the [] and all seems to be well.
>
> THANK YOU!

I'm glad I could help. Take care.

Matthew

--
...We shouldn't test people for drugs, we should test them for
stupidity, ignorance, greed and love of power. -- P.J. O'Rourke

More... (looser matching)
- Last day of these posts
- In 2006 , 2007 only
- Today
- New search...