Front Office Football Central  

Go Back   Front Office Football Central > Archives > FOFC Archive
Register FAQ Members List Calendar Mark Forums Read Statistics

Reply
 
Thread Tools
Old 03-25-2005, 07:43 PM   #1
Karim
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.

Karim is offline   Reply With Quote
Old 03-25-2005, 08:22 PM   #2
sabotai
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.
sabotai is offline   Reply With Quote
Old 03-25-2005, 08:27 PM   #3
dacman
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.
dacman is offline   Reply With Quote
Old 03-25-2005, 08:44 PM   #4
gstelmack
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
gstelmack is offline   Reply With Quote
Old 03-25-2005, 09:31 PM   #5
Karim
College Starter
 
Join Date: Oct 2000
Location: Calgary
More information:

Class definition:

Code:
#ifndef TODOLIST_H #define TODOLIST_H #include (fstream) #include (iostream) //parentheses instead of HTML tags using namespace std; #define MAX_DESC_LEN 60 #define NUM_PRIORITIES 3 class To_Do_List { public: To_Do_List(); ~To_Do_List(); bool read(ifstream &input)const; bool write(ofstream &output)const; void display()const; void add(const char desc[], int priority); bool remove(int position); bool update(int position, int priority); bool lookup(int position, char desc[], int &priority)const; private: struct Item { Item *next; Item *prev; char desc[MAX_DESC_LEN + 1]; }; typedef Item *Item_Ptr; bool search(int pos, Item_Ptr &item, int &priority)const; Item_Ptr create_item(const char desc[])const; //offending function void link(Item_Ptr item, int priority); void unlink(Item_Ptr item); void display_priority(int priority, int &position, ostream &out)const; Item head[NUM_PRIORITIES]; Item tail[NUM_PRIORITIES]; }; #endif /* TODOLIST_H */

I've stubbed out every function except create_item...

Last edited by Karim : 03-25-2005 at 09:32 PM.
Karim is offline   Reply With Quote
Old 03-25-2005, 09:41 PM   #6
gstelmack
Pro Starter
 
Join Date: Oct 2000
Location: Cary, NC
Parentheses around your #includes? What compiler are you using?
__________________
-- Greg
-- Author of various FOF utilities
gstelmack is offline   Reply With Quote
Old 03-25-2005, 09:41 PM   #7
sabotai
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)
sabotai is offline   Reply With Quote
Old 03-25-2005, 09:42 PM   #8
sabotai
General Manager
 
Join Date: Oct 2000
Location: The Satellite of Love
Quote:
Originally Posted by gstelmack
Parentheses around your #includes? What compiler are you using?

He changed them to post the code because if he used the <> it wouldn't show up because of HTML formatting.
sabotai is offline   Reply With Quote
Old 03-25-2005, 09:43 PM   #9
sabotai
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.
sabotai is offline   Reply With Quote
Old 03-25-2005, 10:09 PM   #10
Karim
College Starter
 
Join Date: Oct 2000
Location: Calgary
Quote:
Originally Posted by sabotai
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)

It's for a class. The private section was given to us and isn't supposed to be changed.
Karim is offline   Reply With Quote
Old 03-25-2005, 10:14 PM   #11
Karim
College Starter
 
Join Date: Oct 2000
Location: Calgary
Quote:
Originally Posted by sabotai
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.

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.
Karim is offline   Reply With Quote
Old 03-25-2005, 10:14 PM   #12
sabotai
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?
sabotai is offline   Reply With Quote
Old 03-25-2005, 10:18 PM   #13
Karim
College Starter
 
Join Date: Oct 2000
Location: Calgary
The class compiles. The compiler screams at my attempt to stub out the function.

Code:
Stub: Item_Ptr To_Do_List::create_item(const char desc[])const { Item_Ptr new_item = new item; //do stuff return new_item; } Compiler message: todolist.cpp:230: syntax error before `::' token

Last edited by Karim : 03-25-2005 at 10:20 PM.
Karim is offline   Reply With Quote
Old 03-25-2005, 10:25 PM   #14
Karim
College Starter
 
Join Date: Oct 2000
Location: Calgary
Quote:
Originally Posted by gstelmack
Does this work:

To_Do_Class::Item_Ptr To_Do_Class::create_item()

?

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
Karim is offline   Reply With Quote
Old 03-25-2005, 10:33 PM   #15
Karim
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.
Karim is offline   Reply With Quote
Old 03-26-2005, 01:54 AM   #16
Mr. Wednesday
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)
Mr. Wednesday is offline   Reply With Quote
Old 03-26-2005, 06:30 PM   #17
Karim
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.
Karim is offline   Reply With Quote
Old 03-26-2005, 08:18 PM   #18
gstelmack
Pro Starter
 
Join Date: Oct 2000
Location: Cary, NC
Quote:
Originally Posted by Karim
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

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
gstelmack is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

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 On
Forum Jump


All times are GMT -5. The time now is 07:45 PM.



Powered by vBulletin Version 3.6.0
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.