![]() |
|
|
#1 | ||
|
College Starter
Join Date: Oct 2000
Location: Calgary
|
c++: typedef inside/outside class
If I define a type and a function that returns that tyep within a private section of a class, how do I name the header when I go to write that function? I've tried several combinations but the compiler keeps screaming.
For instance: //within the private section typedef Item *Item_Ptr; //Item is a struct Item_Ptr create_item(); This doesn't work: Item_Ptr To_Do_Class::create_item() Item_Ptr is defined only within the scope of the class so I've tried using Item * or struct Item* for the header but neither works. Any ideas? TIA. |
||
|
|
|
|
|
#2 |
|
General Manager
Join Date: Oct 2000
Location: The Satellite of Love
|
Sorry, I can't even remember the last time I used "typedef". I'm quite certain I never actually used a typedef inside of a class, though.
|
|
|
|
|
|
#3 |
|
College Benchwarmer
Join Date: Oct 2000
Location: speak to the trout
|
I think you've got more than one problem there, doc. I'll need to see a lot more of your code to dicepher what you're trying to do.
__________________
No signatures allowed. |
|
|
|
|
|
#4 |
|
Pro Starter
Join Date: Oct 2000
Location: Cary, NC
|
Does this work:
To_Do_Class::Item_Ptr To_Do_Class::create_item() ?
__________________
-- Greg -- Author of various FOF utilities |
|
|
|
|
|
#5 |
|
College Starter
Join Date: Oct 2000
Location: Calgary
|
More information:
Class definition: Code:
I've stubbed out every function except create_item... Last edited by Karim : 03-25-2005 at 09:32 PM. |
|
|
|
|
|
#6 |
|
Pro Starter
Join Date: Oct 2000
Location: Cary, NC
|
Parentheses around your #includes? What compiler are you using?
__________________
-- Greg -- Author of various FOF utilities |
|
|
|
|
|
#7 |
|
General Manager
Join Date: Oct 2000
Location: The Satellite of Love
|
Is there a reason why you are using typedef? (I ask in case it's for a class and the teacher is telling you to do it this way)
|
|
|
|
|
|
#8 | |
|
General Manager
Join Date: Oct 2000
Location: The Satellite of Love
|
Quote:
He changed them to post the code because if he used the <> it wouldn't show up because of HTML formatting. |
|
|
|
|
|
|
#9 |
|
General Manager
Join Date: Oct 2000
Location: The Satellite of Love
|
Well, just to throw this out, Item_Ptr is defined as a pointer, yet in the function definition, you're trying to return it as a normal type. That might be the problem.
|
|
|
|
|
|
#10 | |
|
College Starter
Join Date: Oct 2000
Location: Calgary
|
Quote:
It's for a class. The private section was given to us and isn't supposed to be changed. |
|
|
|
|
|
|
#11 | |
|
College Starter
Join Date: Oct 2000
Location: Calgary
|
Quote:
It has something to do with the scope of the typedef. It's recognized within the class but the function has to be named differently outside the class. I'm just not sure what the "full name" of Item_Ptr would be so that it's recognized. |
|
|
|
|
|
|
#12 |
|
General Manager
Join Date: Oct 2000
Location: The Satellite of Love
|
So that entire private section for the class was given to you as it is in your post and it won't compile? What exactly (copy and paste it if you can) is the compiler bitching about?
|
|
|
|
|
|
#13 |
|
College Starter
Join Date: Oct 2000
Location: Calgary
|
The class compiles. The compiler screams at my attempt to stub out the function.
Code:
Last edited by Karim : 03-25-2005 at 10:20 PM. |
|
|
|
|
|
#14 | |
|
College Starter
Join Date: Oct 2000
Location: Calgary
|
Quote:
That works but compiler then screams at: Item_Ptr new_item = new item; todolist.cpp: In member function `To_Do_List::Item* To_Do_List::create_item(const char*) const': todolist.cpp:228: syntax error before `;' token |
|
|
|
|
|
|
#15 |
|
College Starter
Join Date: Oct 2000
Location: Calgary
|
The following compiles:
To_Do_List::Item_Ptr To_Do_List::create_item(const char desc[])const { To_Do_List::Item_Ptr new_item; new_item = new Item; return new_item; } Thanks for your help, guys! Last edited by Karim : 03-25-2005 at 10:35 PM. |
|
|
|
|
|
#16 |
|
Pro Starter
Join Date: Jul 2003
Location: South Bend, IN
|
Just FYI -- when you're asking for help with stuff like this, you should indicate which version of which compiler you're using. Without that, it's possible to address whether your code is standard-compliant, but not whether you are running into compiler-specific problems.
__________________
Hattrick - Brays Bayou FC (70854) / USA III.4 Hockey Arena - Houston Aeros / USA II.1 Thanks to my FOFC Hattrick supporters - Blackout, Brillig, kingfc22, RPI-fan, Rich1033, antbacker, One_to7, ur_land, KevinNU7, and TonyR (PM me if you support me and I've missed you) |
|
|
|
|
|
#17 |
|
College Starter
Join Date: Oct 2000
Location: Calgary
|
g++ (GCC) 3.2.3 on Red Hat Linux 3.2.3-49
Here's another question about pointers to pointers. Does the following make sense (based on the above class)? To_Do_List::Item_Ptr *tail_ptr; tail_ptr = &(tail[index].next); head[index].next = *tail_ptr; I've used trace messages and head[index].next always equals NULL as opposed to an address. Other attempts either won't compile or cause a seg fault. What I'm trying to do is set up the constructor so that: head[index].next "points to" tail[index].next tail[index].prev "points to" head[index].prev //a doubly linked list governed by a head & tail array TIA. Last edited by Karim : 03-26-2005 at 06:37 PM. |
|
|
|
|
|
#18 | |
|
Pro Starter
Join Date: Oct 2000
Location: Cary, NC
|
Quote:
That's because "new item" has "item" in lowercase instead of "Item" (as you have in your rewrite where you broke this across 2 lines).
__________________
-- Greg -- Author of various FOF utilities |
|
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|