Help with dynamic arrays of opaque types

Well my problem is i have a program that has a dynamic array of size n, and n is a cardinal that comes from the standard input.
The problem is that i want the array to be of an opaque type, by this i mean a type created by me in another module already compiled.
The problem is when i tpye
mytype * bobby;
bobby = new mytype [5];

the new mytype generates an error since i cant use the new function with a type that comes from another module, i can only use the other modules procedures.
I have a procedure that creates mytype on its original module but i dont know how to initialize each " cell" of the array with that procedure.


I dont know if i explained myself correctly if i havent please tell me what u didnt understand

thanks in adv
please someone,,, any ideas?
mytype is either exposed to outside the module or it's not. If it is, then new mytype[5]; will work.

If it's not, what you're trying to do is impossible. You can't create an instance of a class if you don't have the class to begin with.


Publicly exposing a class isn't hard. You just have to have a header file that can be #included from outside the module.
This is my problem:
I have a module called Zone.h
struct Zone;
{
bool Itscenter;
char* name;
}

this module also has a procedure
Zone* createnewzone();

Then i have another module, that its called City,
#include "Zone.h"
struct City
{
int TotalOfZones; // total number of zones, its always >= 1
Zone * aZone;
};

and it has this procedure:

City* createcity(int numberofzones);
// Creates a city with a number of zones equal to the int variable
// zones cannot be destroyed or more zones cannot be added after a city is created
{
City* c = new City;
c->totalofzones = numberofzones;
c->aZone = new Zone[numberofzones];
}


And that gives me error, i know why,,, its because of the zone type,, if i put int char bool even City type the assignment works, but not with zone.. i get an error.

How can i solve this? the user inputs a n number and i want to create an array ( or maybe some other type of structure,, a list? a set ? ) of n cells and each cells has to store a Zone...

I mean its not even complicated how is this possiblity not contemplated by the compilator?




Note : I am not allowed to add any additional procedure to Zone.h.. Zone.h has more procedures but i cannot add one i want myself, since this is a test problem
Last edited on
If you are #including that header file... and the header file has 'Zone' defined as you posted it... there is no reason why
c->aZone = new Zone[numberofzones]; should fail. It should work just fine.


I think you might be misdiagnosing your problem. What is the exact error you're getting?



----

Although... given the C-ish style of this code... maybe you're not supposed to make new zones that way. perhaps you are expected to call that createNewZone function. So maybe you're expected to do something like this:

1
2
3
4
5
6
7
Zone ** aZone;

//...

aZone = new Zone*[numberofzones];
for(int i = 0; i < numberofzones; ++i)
  aZone[i] = createnewzone();


But of course this makes cleanup much more difficult...
In an attempt to narrow down the possibility of error i tried this:
#include "Zone.h"

City* createcity(int numberofzones);
{
Zone* test = new Zone[numberofzones];
};

notice that im not even using the struct city under the assumption that that may produce an error.
The error persists and its exactly the same.

The error message returned by the compiler is the following :
city.cpp invalid use of undefined type `struct Zone'
zone.h forward declaration of `struct Zone'


While this suggest that Zone is not included i have it included and i tried:
Zone* test2 = createnewzone();


And that works perfectly, no errors, so,, how come the second line work and not the first if the problem is that Zone is not included? Which of course it is, since im watching it right now.


Its really odd.
Thanks in advantage
Okay you were diagnosing correctly. Your previous post just confused me is all.

You must not be #including the header that defines Zone. That's all there is to it. If Zone was defined you would not be having this problem.

So if the header Zone is defined in is not public and/or you cannot #include it, then you'll have to use the createnewzone interface. Which means there's probably also a deletezone() or similar function for cleanup.

So yeah.. something like the example at the end of my last post should work.
ohh this happens because Zone.h has only struct Zone; and Zone.cpp has the actual implementation of the record? if Zone.h had the
struct Zone{
}

This wouldnt have happened? Thanks a lot, really. Im gonna try the alternative you suggested.
closed account (DSLq5Di1)
Zone.h
1
2
3
4
5
struct Zone; // <- rogue semi-colon
{
    bool Itscenter;
    char* name;
}; // move it here 
Yea but i cant touch the .h files... My college gives me the .h files and i have to design the .cpp files and ulpoad those. When they test the .cpp they use the .h files they gave to me so sadly thats not an option :(
closed account (DSLq5Di1)
Well, the header file is the problem.. the compiler views struct Zone; as a forward declaration for a type yet to be defined. Hence this message:-
zone.h forward declaration of `struct Zone'

Zone has not been defined in the header, so anything including the header and attempting to use a Zone will spit out this error:-
invalid use of undefined type `struct Zone'

Speak with a lecturer or peer at your college about the issue, it's probably just a silly typo they've made.
Topic archived. No new replies allowed.