PDA

View Full Version : c++: typedef inside/outside class


Karim
03-25-2005, 07:43 PM
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.

sabotai
03-25-2005, 08:22 PM
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.

dacman
03-25-2005, 08:27 PM
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.

gstelmack
03-25-2005, 08:44 PM
Does this work:

To_Do_Class::Item_Ptr To_Do_Class::create_item()

?

Karim
03-25-2005, 09:31 PM
More information:

Class definition:


#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...

gstelmack
03-25-2005, 09:41 PM
Parentheses around your #includes? What compiler are you using?

sabotai
03-25-2005, 09:41 PM
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
03-25-2005, 09:42 PM
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
03-25-2005, 09:43 PM
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.

Karim
03-25-2005, 10:09 PM
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
03-25-2005, 10:14 PM
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.

sabotai
03-25-2005, 10:14 PM
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?

Karim
03-25-2005, 10:18 PM
The class compiles. The compiler screams at my attempt to stub out the function.


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

Karim
03-25-2005, 10:25 PM
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
03-25-2005, 10:33 PM
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!

Mr. Wednesday
03-26-2005, 01:54 AM
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.

Karim
03-26-2005, 06:30 PM
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.

gstelmack
03-26-2005, 08:18 PM
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).