![]() |
|
#1
|
||||
|
||||
Returning a Struct to a separate classhi all. i guess i need to brush up on my c++ classes cos this one has me stumped.
my situation is is that i have 3 classes. for simplicity lets call them Class A, B & C. Class A instaniates Class B which by inheritance instantiates Class C. Therefore Class A has access to all of the public member functions that Class B does. thats not even my problem just laying the groundwork. In Class C i have a struct that i have defined as such: CPP / C++ / C Code:
eventually i will find out how many of this type i will need and like so: x = new myStruct[some_number]; then i will proceed to fill up this in memory. easy enough. Now what i want to do is write a function in this class to return this struct to Class A so Class A can use the contents of the data. since Class A has all the public members through the instantiation of Class B this is legal as you will know. so here is my Class C function to return the struct: CPP / C++ / C Code:
now is this correct or am i missing something here? if this is correct what do i need to in Class A to make sure it calls this function correctly because right now all i get are compiler errors. i would print them here but i have tried so many different things. so far what i have done in Class A is define the exact same struct so when it does receive the Class C struct i can copy the struct's contents in Class C to the local struct in Class A. can anyone help me out here with some code to make this happen? if you would i would be eternally grateful Last edited by LuciWiz : 06-Feb-2007 at 00:05.
Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
|
|
#2
|
|||
|
|||
Re: Returning a Struct to a separate class[quote=usmsci]
CPP / C++ / C Code:
So, now the pointer x is pointing to a block of memory. The block of memory can hold some_number structs. The structs can be accessed by dereferencing a pointer expression involving x or by using array indexing with x. Quote:
What struct? If you return *x, you are returning x[0], a single struct. You can use this any way you want. There is no way that knowing x[0] can tell the program how to access any of the other members of the array. On the other hand, any piece of code, anywhere, can access all of the structs if it knows what myStruct is and if it has access to the value of x and if it knows the number of elements in the array. Quote:
Actually, I think I must be missing something here. Quote:
It's kind of hard to guess the number of incorrect ways of attempting this so it's kind of hard to guess what the compiler errors might be. How about boiling it down to simplest possible terms (a small subset of your code that illustrates what you are attempting)? Then show us the code and show us the errors. If there are oodles and oodles of errors, just show the first few. Maybe someone can help you work through this. You might also mention what compiler and operating system you are using, since this may or may not be apparent from the nature of the compiler messages, and it may be important to someone trying to help. Regards, Dave |
|
#3
|
||||
|
||||
Re: Returning a Struct to a separate classAlso, to add to Dave's info...
While brushing up on the C++ classes, why don't you have this struct as a class instead? Then it can be used as an object [or object array] with(in) the other classes. Use 'name' and 'id' as data members, and have [get/set]Name, [get/set]Id functions for them [plus, of course, whatever [con/de]structors are needed. Looks like the makings of a 'user' class [or a better name of your choice] to me... As Dave said, if you still have trouble, post your LATEST code and the error messages. __________________
Use the force...read the source!! WYCIWYG -- what you code is what you get! |
|
#4
|
||||
|
||||
Re: Returning a Struct to a separate classQuote:
hi and thanks for writing back. i have decided to use vectors instead on the hopes that it would be easier. everything else is the same. i have 2 classes, call them class1 and class 2. each class has a defined struct in its own header file like so CPP / C++ / C Code:
class1 has access to class 2's public member functions, one being getData and is defined as such in the class2 .h file: vector<myStruct> getData(); in class2.cpp i resize x as needed: myStruct.resize(some_number); then i go ahead and fill it with data. finally here is my getData function that i want class1 to call. CPP / C++ / C Code:
my goal is for class 1 to call this method to retrieve a copy of this vector. unfortunately first of all the compiler complains that myStruct is an undeclared identifier as i pointed out above even though i declared it in the .h file. Quote:
whats going on here? i hope that i have been more clear... thanks for the help in advance. Last edited by LuciWiz : 07-Feb-2007 at 11:42.
Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
|
|
#5
|
||||
|
||||
Re: Returning a Struct to a separate classnevermind about that part. i got it working , easy overlook on my part.
should be: vector<class2::myStruct> class2::getData() { return x; } now i need to be able to call this function from class 1 which has the struct definition as well and its a struct. unfortunately i have found out that the = operator doesnt work when assigning vectors.. well at least thats what the compiler seems to imply. |
|
#6
|
|||
|
|||
Re: Returning a Struct to a separate classQuote:
Quote:
Yes it does. std::vectors have the overloaded assgnment operator. What makes you think that the '=' operator doesn't work? Regards, Dave |
|
#7
|
||||
|
||||
Re: Returning a Struct to a separate classQuote:
Quote:
thats the error i get when i try to assign the vector from class2 to the class1 vector. x = reader->getData(); //trying to assign the struct vector from class 2 to the one in class 1. where x is defined in class1.h vector<myStruct> x; |
|
#8
|
||||
|
||||
Re: Returning a Struct to a separate classhere is my full code(the relevant code anyway) so you can see better everything:
ok ill start from the top: both classes, lets just call them class1 and class2 for simplicity reasons has defined the same struct from within their own header. //in class1.h CPP / C++ / C Code:
//in Class2.h CPP / C++ / C Code:
now onto the .cpp files: //in Class2.cpp CPP / C++ / C Code:
in Class1.cpp CPP / C++ / C Code:
compiler quote about error above: Quote:
i thought vectors by default override the = operator so this is where i am confused. i hope this clears things up! thanks! Last edited by LuciWiz : 07-Feb-2007 at 02:23.
Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
|
|
#9
|
||||
|
||||
Re: Returning a Struct to a separate classQuote:
However... Quote:
Quote:
Quote:
From class1.h, class1::x is a vector of class1::myStructs. From class1.h, class1::reader is a pointer to a class2 object. The class2 getData() function returns a vector of class2::myStructs. Therefore class1::reader->getData() returns a vector of class2::myStructs. Summary: Left Hand Side is a vector of class1::myStructs. Right Hand Side is a vector of class2::myStructs. You can not assign a vector of "somethings" to a vector of "something_elses" with the overloaded '=' operator, so the compiler, rightfully, complains. These two are separate classes (no inheritance involved). If you want to use public members (or typedefs) of one class in another you can, so if you define class1::x to be a vector of class2::typedefs, then it will compile (assuming you include the headers, etc. --- that's the stuff you left out of your post). You could try something like: CPP / C++ / C Code:
CPP / C++ / C Code:
I hate to repeat myself, but I haven't seen the point of all of this, but this much should, at least, compile. Having two different typedefs of structs with the same elements in two different classes gives two different things to the compiler to work with. They are not the same to the compiler, even though the look the same (to you and me). If one class were derived from the other and if the base class had a public (or protected) typedef then the derived class would inherit the typedef just as it inherits public or protected data and method members. Maybe that's what you originally had in mind??? Regards, Dave |
|
#10
|
||||
|
||||
Re: Returning a Struct to a separate classQuote:
you are right, last night i figured this out and its just something that i overlooked or just wasnt paying attention. in class1.h instead of: vector<myStruct> x; should be: vector<class2::myStruct> x; since i am looking to get back the actual class2 struct that i inherited. i am guessing that what you meant. as such there is no reason to even have a local myStruct in class1. thanks for your help! |
Recent GIDBlog
First week of IA training by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Hard drive/CPU Diagnoses Issues | binarybug | Computer Hardware Forum | 1 | 22-Jan-2007 19:23 |
| Message Class | TransformedBG | CPP / C++ Forum | 5 | 29-Nov-2006 21:28 |
| Urgent ! Pls Help Me ! | mycashmoney | C Programming Language | 4 | 01-Jul-2006 22:49 |
| [Tutorial] GUI programming with FLTK | dsmith | FLTK Forum | 10 | 03-Oct-2005 15:41 |
| Error C2146: syntax error : missing ',' before identifier 'C4' | mattchew008 | CPP / C++ Forum | 2 | 19-Dec-2004 06:06 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The