944,028 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 22730
  • C++ RSS
Feb 7th, 2005
0

Structure of Arrays in C++ using NEW operator

Expand Post »
HI guys,

This code dosn't compile, I think it is right but it gives an error when i use the -> to access the members of my struct.



#include <stdio.h>
#include <stddef.h>
#define MAX_EVENTS 100


//Structure for Queue Elements
struct Event
{
int event_type;
int time_value;
int bus_num;
int bus_stop;
};


void main()
{
Event *eventPtr;
// EventPtr event_queue;

eventPtr = new Event[MAX_EVENTS];

event_queue[0]->event_type = 1;
event_queue[0]->time_value = 2;
event_queue[0]->bus_num = 0;
event_queue[0]->bus_stop = 1;

}



It works when i create it without using the "new" operator. But this way it dosn't. WHat's going on here? I'm using the Visual C++ 6.0 compiler.

thanks..
Similar Threads
F50
Reputation Points: 10
Solved Threads: 0
Newbie Poster
F50 is offline Offline
6 posts
since Feb 2005
Feb 7th, 2005
0

Re: Structure of Arrays in C++ using NEW operator

Try this:
int main()
{
// EventPtr event_queue;
   Event *eventPtr;
   eventPtr = new Event[MAX_EVENTS];

   eventPtr[0].event_type = 1;
   eventPtr[0].time_value = 2;
   eventPtr[0].bus_num = 0;
   eventPtr[0].bus_stop = 1;
   return 0;
}
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Feb 7th, 2005
0

Re: Structure of Arrays in C++ using NEW operator

I'm sorry, i cut and pasted the wrong thing last time. It should have pasted it like this.

#include <stdio.h>
#include <stddef.h>
#define MAX_EVENTS 100


//Structure for Queue Elements
struct Event
{
int event_type;
int time_value;
int bus_num;
int bus_stop;
};


void main()
{
Event *eventPtr;

eventPtr = new Event[MAX_EVENTS];

eventPtr[0]->event_type=1;
eventPtr[0]->time_value = 2;
eventPtr[0]->bus_num = 0;
eventPtr[0]->bus_stop = 1;

}



when i compile that it says. "error C2819: type 'Event' does not have an overloaded member 'operator ->'"
F50
Reputation Points: 10
Solved Threads: 0
Newbie Poster
F50 is offline Offline
6 posts
since Feb 2005
Feb 7th, 2005
0

Re: Structure of Arrays in C++ using NEW operator

Same answer: use the . operator.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Feb 7th, 2005
0

Re: Structure of Arrays in C++ using NEW operator

okay. thanks. I was just wondering why the -> operator wasn't working.

Also, would it be possible to create this same structure, but with a doubly linked ring, or a balanced tree? Instead of using arrays.

Links to already compilable data strucutres would be great. Professor said that is okay to use since it is not the focus of this program.
F50
Reputation Points: 10
Solved Threads: 0
Newbie Poster
F50 is offline Offline
6 posts
since Feb 2005
Feb 7th, 2005
0

Re: Structure of Arrays in C++ using NEW operator

Quote originally posted by F50 ...
I was just wondering why the -> operator wasn't working.
Because the -> operator is used with a pointer; eventPtr[0] is not a pointer, it is an object. The . operator is used with an object.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Feb 7th, 2005
0

Re: Structure of Arrays in C++ using NEW operator

eventPtr[0] is a variable associated with a memory address.
Pointers store the address of some other variable associated with a memory address. We dereference the pointer to get the value that is stored at the memory location in the pointer, ie using '->'.
Reputation Points: 10
Solved Threads: 1
Light Poster
Intel is offline Offline
30 posts
since Feb 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Borland Compiler
Next Thread in C++ Forum Timeline: Help... assembly in Dev C++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC