I've made a function to set up a array of modules and then a separate function to print out these modules how would i go about passing the pointer to the array to the print function?
These are the errors i get with the code when i compile it:
Line Location Module.h:165: error: 'ModulesPointer' was not declared in this scope
Line Location Module.h:165: error: 'mods' was not declared in this scope
the structure works fine, its perfectly legal.
i'm not sure what you mean when you say that, is it that returning mods is that same as &mods[MAXARRAY]?
Well, the compiler doesn't seem to think so.
Never mind. I found the problem. On line 74 of the code you posted, neither of the two identifiers were declared. The ones declared in modulearray() are local to that function. You can't use those in main().
This would have been easier if you had posted the exact code you gave the compiler.
mods is local to the function that builds the array. In the main you are calling moduleArray() which creates and leaks memory. Then you call printArray from the main. The memory is leaked so there is no way to pass it to printarray unless you call printarray from module array. Since they are both members, you could make the array a class attribute. Since it is dynamic, use a std::vector if you are allowed to. If the array is part of the class instance then you don't have to pass it through the function. printArray can simply print the classes current array by accessing the member.
I cannot emphasize enough how important it is to copy and paste the example into this site so that we can see exactly what is being fed to the compiler.
Returning mods seems like the simplest way, to me, although I don't really know what you have in mind for that array. Perhaps it would be better to make it a member of the class, but I can't know that.
Also, I just noticed two other things:
1. You seem to inline all functions/methods.
2. You don't seem to know about for.
the program goes like this.
There area 15 students, 10 modules and 5 lecturers.
The program read in the 15 students names and the user assigns two modules to each student then the program prints out the students name and the two modules and the lecturers (each lecturer has two modules) and start times associated with them.
Its to do with inheritance and i'm finding it awfully confusing.
I don't really know what you have in mind for that array.
I'm trying to sort of hard code in the modules
1. You seem to inline all functions/methods.
2. You don't seem to know about for.
I wasnt aware of another way this is how we were told in college
I dont get what you mean by the other point?
i included std::vector in the .cpp but now it doesnt like my constructors and destructors and when i put it in the .h file it throws up a whole lot of errors
If you want to pass the array that you create in modulearray() to printarray(), you'll have to modify the signature of your funtion printarray() like this:
inlinevoid Module::printarray(module * PointerToArray);
Now the function can accept a pointer to a module object as argument.
You'll need to pass the array as this argument that we are talking about.
You really do not need to do this on line 13:
module *ModulesPointer = &mods[0];
because when you do this on line 12: module *mods = new module[MAXMOD];
You already create a pointer to the start of your array; that pointer is mods.
So you want to pass mods to printarray(). Here's how you do it:
printarray(mods);
Pretty simple? I think it will be for you if you try to understand the relationship between arrays and pointers well.
How do i pass mods back i cant get it working heres what i'm give the compiler i'm just using th function to intialize the array and using one of the elements and then trying to print it in main.
The problem is it doesn't recognise mods in main how do i return mods (pointer) from my function? any advice is very welcome, thanks
error: 'mods' was not declared in this scope Line Location DdateV001.cpp:57: