Discuss Cannot access struct in h file in the C Programming forum on Dev Shed. Cannot access struct in h file C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
Posts: 290
Time spent in forums: 3 Days 3 h 48 m 33 sec
Reputation Power: 11
Cannot access struct in h file
I created a node class and list class and declared the struct in the private of BookNode.. everytime i compile it says name undeclared..
i have declared a pointer to that class to access it and getter/setter methods too. but the function cant see the struct itself
Posts: 290
Time spent in forums: 3 Days 3 h 48 m 33 sec
Reputation Power: 11
i didnt know you can define something out of a class.. because i thought Anoed which is a struct unique to BookNode and should be inside BookNode class...
Posts: 1,431
Time spent in forums: 2 Weeks 3 Days 13 h 33 m 57 sec
Reputation Power: 152
it may be unique but its only unique to BookNode in this case. yet you are trying to create an instance of it in BookList. you would need to define it outside of the class so that BookList can use it also or try a different design.
Posts: 4,378
Time spent in forums: 1 Month 2 Weeks 2 Days 11 h 5 m 4 sec
Reputation Power: 1509
He could define it in the class, and use scope resolution to tell the compiler what he wants. ... but he still has to make it public rather than private.
It seems like what he really wants, though, is to have booknode inherit from Anode or use templates such that he can define Anode generically as a node in a list and have Booknode define the payload for this particular use/application.
__________________
Primary Forum: .Net Development
Holy cow, I'm now an ASP.Net MVP!
Posts: 4,378
Time spent in forums: 1 Month 2 Weeks 2 Days 11 h 5 m 4 sec
Reputation Power: 1509
Looking more at his code, I think he'll have a real problem when he runs that and tries to create his first instance of a booknode. When the booknode contsructor is first called, if will take bn (a booknode pointer) and create a new instance, which will of course call the booknode constructor which will take it's own bn and create another instance... and on to ifinity, or when memory runs out.
Posts: 290
Time spent in forums: 3 Days 3 h 48 m 33 sec
Reputation Power: 11
I was originally thinking of creating just 1 Header file to make it simpler...
but that means i have to include the BookList methods as well as the BookNode methods..
So there would need to be 2 classes inside the one header file...
Posts: 1,431
Time spent in forums: 2 Weeks 3 Days 13 h 33 m 57 sec
Reputation Power: 152
flar makes another good point. you will want to rethink your design for the BookNode class. you wont need to have a BookNode as a member of BookNode. BookList will be the one that needs to have those. BookNode only needs enough to create itself and initialize its members.
Posts: 1,431
Time spent in forums: 2 Weeks 3 Days 13 h 33 m 57 sec
Reputation Power: 152
well, your BookList is a list of BookNodes.
while you have a Anode as a member of the BookNode class, do you really need that? couldnt you just have BookNode be the Anode like
c++ Code:
Original
- c++ Code
class BookNode
{
private:
String name;
BookNode*next;
public:
BookNode();
void setBookname(String name);
String getName();
BookNode* getNext();
}
that way your BookNode is 1 node object by itself. it contains the name and a pointer to another BookNode. and then BookList can maintain the nodes.
EDIT: out of curiosity, is this an assignment or a learning thing? if its an assignment, you would need to follow the requirements given by your instructor.
Posts: 290
Time spent in forums: 3 Days 3 h 48 m 33 sec
Reputation Power: 11
Yes it is an assignment...
Because i have to create the following classes...
"BookList"
"BookNode"
"Book"
and others.. thats why i got a little confused, so i wasnt sure to create 2 header files, one for booknode and booklist...
Posts: 1,431
Time spent in forums: 2 Weeks 3 Days 13 h 33 m 57 sec
Reputation Power: 152
its totally possible to make a node out of a struct. but from the looks of your assignment, i think they want you to go with the class way that i mentioned. but you can always verify with your instructor. their requirements are what you need to base it on. it never hurts to ask advice from them on your design and such also to make sure you are on the same wavelength with the instructor.