[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Error entering classes into dynamic array of classes - Obj Oriented C++

Posted on 2009-02-17
7
Medium Priority
?
233 Views
Last Modified: 2012-05-06
The assignement is specific, but I think the problem is very general. I will really appreciate it if anyone can help me. I have uploaded the files to: http://sites.google.com/site/allenlosite/Home/lab1final.zip?attredirects=0

I have an assignment that reads lines of code and writes them into classes. The rules of the assignment are very strict and none of the files other than the Groceryapp.cpp, Get class in both Produce.cpp and Grocery.cpp can be edited. It also uses an I/O manager class file that uses pretty much ifstream and ofstream in a little more complex form. Not too hard, but here comes the problem.

While Grocery.cpp and Produce.cpp (and their headers) are almost identical, Produce uses Grocery's data members and functions through friend function( I think). I have 2 dynamic arrays, one for each type (G for Grocery classes, P for Produce classes). Grocery's datamembers are name, markup, and cost. Produce has those 3 plus 1 more, which is weight. Now, the input procedure is to put them in their respective arrays. Produce will use Grocery's "Get" function to read in the members, plus also add weight. My problem is when I enter them in, the second instance of produce will cause the program to crash. Grocery will not crash at all, it receives all the instances correctly. You will see me mark the problem areas with an asterick in the program.

Please, if you need clarification, let me know. I am so stumped and I have not gotten any help from my professor. Thank you.
0
Comment
Question by:allenlo77
  • 3
  • 3
7 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 23663837
Produce is derived from Grocery :

>>        class Produce :public Grocery

If you are unsure about what that means, I suggest reading up on classes, and inheritance. Here's a good start :

        http://www.cplusplus.com/doc/tutorial/classes.html
        http://www.cplusplus.com/doc/tutorial/classes2.html
        http://www.cplusplus.com/doc/tutorial/inheritance.html
        http://www.cplusplus.com/doc/tutorial/polymorphism.html
0
 

Author Comment

by:allenlo77
ID: 23663868
I do, to a certain extent, but I can't figure out why it is not writing into the dynamic array. I believe that is where the problem lies, but it ties in with the way the instances of Produce are made from using Grocery class
0
 
LVL 86

Accepted Solution

by:
jkr earned 1000 total points
ID: 23664012
The error you are getting is because you have

      Grocery *G;
      if( nGrocery > 0 ) G = new Grocery[nGrocery];
      Grocery *P; // <---- here!
      if( nProduce > 0 ) P = new Produce[nProduce];

so that the wrong implementation of 'Extract()' is called - make that

      Grocery *G;
      if( nGrocery > 0 ) G = new Grocery[nGrocery];
      Produce *P;
      if( nProduce > 0 ) P = new Produce[nProduce];

(in 'main()')
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:allenlo77
ID: 23664051
I can't believe I did not see that, I thought that was declared correctly or I have tried fixing that in the past. Thanks and I will see if that will work. Will have an answer in 4 hours
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 1000 total points
ID: 23664185
Two things :

1) the input file is wrong : It says :

        nGrocery: 5 nProduce: 2

    while it contains 4 grocery's and 3 produce's


2) But more importantly regarding your problem :

      Grocery *P;
      if( nProduce > 0 ) P = new Produce[nProduce];

    is wrong. You define an array of Grocery objects P, but then you allocate memory for a number of Produce objects. That won't work, especially since you are trying to access the array in two different ways.

    Allocate like this instead :

      Produce *P;
      if( nProduce > 0 ) P = new Produce[nProduce];
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 23664195
Oho ... lol ... refresh prob ;)
0
 

Author Comment

by:allenlo77
ID: 23664217
thanks infinity, I am almost positive now that this is the problem

Oh and the input file has different inputs because I am to not accept any more than the specified number on the first line, which is the reason for the overflow statements. A thousand thanks to both of you from an amateur C++ programmer
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question