GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 05-Feb-2007, 19:57
usmsci's Avatar
usmsci usmsci is offline
New Member
 
Join Date: Oct 2004
Location: USA
Posts: 25
usmsci is on a distinguished road

Returning a Struct to a separate class


hi 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:
struct myStruct
{
  string name;
  int id;
};

myStruct *x;

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:
ClassC::myStruct ClassC::getData()
{ return *x; }

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 thanks all in advance!
Last edited by LuciWiz : 06-Feb-2007 at 00:05. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #2  
Old 06-Feb-2007, 10:19
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,623
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Returning a Struct to a separate class


[quote=usmsci]
CPP / C++ / C Code:
x = new myStruct[some_number];

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:
Originally Posted by usmsci
Now what i want to do is write a function in this class to return this struct

CPP / C++ / C Code:
{ return *x; }


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:
Originally Posted by usmsci
now is this correct or am i missing something here?

Actually, I think I must be missing something here.
Quote:
Originally Posted by usmsci
right now all i get are compiler errors.

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  
Old 06-Feb-2007, 11:00
TurboPT's Avatar
TurboPT TurboPT is offline
Regular Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 924
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Returning a Struct to a separate class


Also, 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  
Old 06-Feb-2007, 14:28
usmsci's Avatar
usmsci usmsci is offline
New Member
 
Join Date: Oct 2004
Location: USA
Posts: 25
usmsci is on a distinguished road

Re: Returning a Struct to a separate class


Quote:
Originally Posted by TurboPT
Also, 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.

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:
struct myStruct
{ 
 string name;
 string description;
};

vector<myStruct> x;

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:
vector<myStruct> class2::getData()  <--- compiler error
{ return x; }

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:
error C2065: 'myStruct' : undeclared identifier

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  
Old 06-Feb-2007, 15:49
usmsci's Avatar
usmsci usmsci is offline
New Member
 
Join Date: Oct 2004
Location: USA
Posts: 25
usmsci is on a distinguished road

Re: Returning a Struct to a separate class


nevermind 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  
Old 06-Feb-2007, 15:58
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,623
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Returning a Struct to a separate class


Quote:
Originally Posted by usmsci
nevermind about that part.
OK by me! I never figured out what you needed to do anyhow.
Quote:
Originally Posted by usmsci
i have found out that the = operator doesnt work when assigning vectors

Yes it does. std::vectors have the overloaded assgnment operator. What makes you think that the '=' operator doesn't work?

Regards,

Dave
  #7  
Old 06-Feb-2007, 16:05
usmsci's Avatar
usmsci usmsci is offline
New Member
 
Join Date: Oct 2004
Location: USA
Posts: 25
usmsci is on a distinguished road

Re: Returning a Struct to a separate class


Quote:
Originally Posted by davekw7x
OK by me! I never figured out what you needed to do anyhow.


Yes it does. std::vectors have the overloaded assgnment operator. What makes you think that the '=' operator doesn't work?

Regards,

Dave

Quote:
error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::vector<_Ty>' (or there is no acceptable conversion)....

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  
Old 06-Feb-2007, 20:41
usmsci's Avatar
usmsci usmsci is offline
New Member
 
Join Date: Oct 2004
Location: USA
Posts: 25
usmsci is on a distinguished road

Re: Returning a Struct to a separate class


here 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:
#include class2.h  //include class2

class class1
{
 public:
  class1();
  virtual ~class1();
  .....
  .....
  private:
  struct myStruct
  {
   string name;
   string description;
  };

  vector<myStruct> x;
  class2 *reader; //will access the class2 public functions
  void copyVectorData();
  .......
};


//in Class2.h
CPP / C++ / C Code:
class class2
{
  public:
    class2();
    virtual ~class2();
    struct myStruct
     {
       string name;
       string description;
     }
     
    vector<myStruct> getData();
    int getNum();

  private:
    long count;
    vector<myStruct> x;
    void setNum(long);
};    

now onto the .cpp files:

//in Class2.cpp
CPP / C++ / C Code:

class2::class2()
{
  //read in a number from a file here, then store it into count
  setNum(count); //pass in whatever we read from the file
}

class2::setNum(long count)
{ 
  x.resize(count); 
  //now we can fill up the vector with data which goes here
}

long class2::getNum()
{ return x.size(); }
   
vector<class2::myStruct> class2::getData()
{ return x; } //return the vector to the caller(will be class1)

in Class1.cpp
CPP / C++ / C Code:

class1::class1()
{
  reader = new class2();
  x.resize(reader->getNum());
  copyVectorData();
}

void class1::copyVectorData()
{
 x = reader->getData(); <-- compiler complains here
}

compiler quote about error above:
Quote:
error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::vector<_Ty>' (or there is no acceptable conversion)

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  
Old 06-Feb-2007, 23:56
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,623
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Returning a Struct to a separate class


Quote:
Originally Posted by usmsci
here is my full code(the relevant code anyway) so you can see better everything:
But I can't see what you left out, and sometimes what you leave out is more important than what you leave in.

However...
Quote:
Originally Posted by usmsci
CPP / C++ / C Code:
//in class1.h

#include class2.h  //include class2

class class1
{
.
.
.
  private:
  struct myStruct
  {
   string name;
   string description;
  };
.
.
  vector<myStruct> x;
.
.
You have made a private typedef of myStruct, and class1::x is a vector of such thingies.

Quote:
Originally Posted by usmsci
CPP / C++ / C Code:
//in Class2.h
class class2
{
.
.
public:
.
.
    struct myStruct
     {
       string name;
       string description;
     }
.
.    
    vector<myStruct> getData();
 .
.
.
Now, class2 has a public typdef of its own myStruct and class2::getData() returns a vector of such thingies. The definition of the class2::myStruct is available anywhere.
Quote:
Originally Posted by usmsci
CPP / C++ / C Code:
//in Class1.cpp


class1::class1()
{
  reader = new class2();
  x.resize(reader->getNum());
  copyVectorData();
}

void class1::copyVectorData()
{
 x = reader->getData(); <-- compiler complains here
}

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:
// in class1.h
.
.
.
    vector<class2::myStruct> x;
    class2 *reader; //will access the class2 public functions

.
.
CPP / C++ / C Code:
//In class1.cpp
void class1::copyVectorData()
{
    x = reader->getData(); // both sides are class2::myStruct
}

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  
Old 07-Feb-2007, 08:34
usmsci's Avatar
usmsci usmsci is offline
New Member
 
Join Date: Oct 2004
Location: USA
Posts: 25
usmsci is on a distinguished road

Re: Returning a Struct to a separate class


Quote:
Originally Posted by davekw7x
But I can't see what you left out, and sometimes what you leave out is more important than what you leave in.

However...

You have made a private typedef of myStruct, and class1: is a vector of such thingies.


Now, class2 has a public typdef of its own myStruct and class2::getData() returns a vector of such thingies. The definition of the class2::myStruct is available anywhere.


From class1.h, class1: 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: 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:
// in class1.h
.
.
.
    vector<class2::myStruct> x;
    class2 *reader; //will access the class2 public functions

.
.
CPP / C++ / C Code:
//In class1.cpp
void class1::copyVectorData()
{
    x = reader->getData(); // both sides are class2::myStruct
}

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

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 GIDBlogFirst week of IA training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 19:40.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.