Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems passing a pointer to struct 1

Status
Not open for further replies.

nychic

Programmer
Nov 13, 2002
7
US
I defined a new type in my first class and declared an array of this structure. I created a pointer to the array and need to pass this into another class so I can access the data in the array, but the other class won't recognize the structure that I created...
Here's the necessary code for the two classes:

class Document {

private:
struct Plaintiff{
AnsiString asName;
AnsiString asCounty;
AnsiString asState;
};

Plaintiff myPlaintiffs[5];
Plaintiff *myPtrPlaintiffs;

}

class Paragraph {
private:
public:
Paragraph(Plaintiff *myPtrPlaintiffs);
}

In the Constructor for the Document class, I put values into the array(not showing that here) and then set the ptr pointing to the first element in the array like this:

myPtrPlaintiffs = myPlaintiffs;

In another function, I now try to pass this ptr into the constructor of my Paragraph class like this:

Paragraph myParagraph(myPtrPlaintiffs);

When I call this constructor, the structure is not recognized, even though I include the header for where the structure is defined. I also tried making the structure public, but it didn't change anything. Is there a way to do this or does the structure need to be declared globally?
Any suggestions are much appreciated, I'm really stumped here. I did end up making a class instead of a structure to get it to work, but I would still prefer to do it this way.

 
Code:
myPtrPlaintiffs
cannot be accessed because it is declared private in the
Code:
Document
class. Rome did not create a great empire by having meetings, they did it by
killing all those who opposed them.

- janvier -
 
What do you mean defining struct Plaintiff inside of class Document? Why not globally? There is no difference in using of terms "struct" or "class".
 
You have multiple Plantiff *myPlantiff pointers in different places in different classes. It would help with the clarity if the Planfiff pointers were labelled differently when they are, in fact, different.

My first suggestion would be to define the Plantiff struct in its own header file (yes, you can do this for structs), and include it in both the Document and Paragraph classes seperately. It seems as if you may be running into trouble because the struct is a private member of Document, and Paragraph can't access this member.
 
Thanks for the tips, my problem was indeed that I was trying to declare the struct privately but I changed it into a class instead and it works great.
 
Since you said you didn't want to make a class, you can also do something like this:

#ifndef PLANTIFF_H
#define PLANTIFF_H

#include //whatever the heck has AnsiString

struct Plaintiff
{
AnsiString asName;
AnsiString asCounty;
AnsiString asState;
};

#endif

Then initialize stuff like this:

Plantiff p = { "Name", "County", "State" };
or p.asName = ...

I think the syntax for the above is all good, but it's a new concept for me too, so I'll have to check everything after work.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top