GIDForums

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read


 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 19-Sep-2007, 14:14
Kimmo Kimmo is offline
Member
 
Join Date: Mar 2007
Location: Finland
Posts: 229
Kimmo has a spectacular aura aboutKimmo has a spectacular aura about

Return pointer to template's nested class problem


(Sorry for the subject..)

I had hoped to reveal my linked list implementation to the world this evening, but alas, I ran into a problem I can't overcome.

The problem is, as best described, this.

I have a class LinkedList, in which I have a nested class Node. Some of my methods in LinkedList return pointers to class Node objects. Everything seemed to work (as best I could test) before templating the LinkedList class. After templating I get the following warnings and errors on my method definitions that try to return pointer to Node objects

Code:
Warning 1 warning C4346: 'LinkedList<dataType>::Node' : dependent name is not a type Error 2 error C2143: syntax error : missing ';' before '*' Error 3 error C4430: missing type specifier - int assumed.

Here are the class declarations
CPP / C++ / C Code:
template <typename dataType>
class LinkedList
{

	class Node
	{
	private:
		
		Node(const Node&);
		Node& operator=(const Node&);
		
	public:

		dataType m_data;
		Node* m_next;
		Node* m_previous;

		Node(dataType data);
		Node();
		~Node() {}

	};

	

private:

	Node* m_firstNode;
	Node* m_lastNode;
	unsigned int m_numberOfNodes;

	LinkedList(const LinkedList&);
	LinkedList& operator=(const LinkedList&);

public:

	LinkedList();
	~LinkedList();

        // methods...

        // The culprits
	// getFirstNode() returns a pointer to first Node in list
	Node* getFirstNode();

	// getLastNode() returns a pointer to last Node in list
	Node* getLastNode();
};

And here are the method definitions
CPP / C++ / C Code:
template <typename dataType>
LinkedList<dataType>::Node* LinkedList<dataType>::getFirstNode()
{

	// if list is empty, return 0
	if (m_numberOfNodes == 0)		
	{
		return 0;
	}
	else
	{
		return m_firstNode;
	}
}

// similar one for getLastNode()

So there's probably something amiss in the scoping of Node class? But I don't know what, since other methods like
CPP / C++ / C Code:
template <typename dataType>
bool LinkedList<dataType>::isEmpty() const
compile fine. And the same pattern is used for the nested class.

Yes, I tried looking for explanations, but didn't come up with any. Or I just missed them as irrelevant.
  #2  
Old 20-Sep-2007, 00:37
Kimmo Kimmo is offline
Member
 
Join Date: Mar 2007
Location: Finland
Posts: 229
Kimmo has a spectacular aura aboutKimmo has a spectacular aura about

Re: Return pointer to template's nested class problem


Okay, in the morning I'm back in business and found this poor solution. Making the methods inline works. As in the following

CPP / C++ / C Code:
template <typename dataType>
class LinkedList
{
private:
// stuff
// Node class declaration

public:

// stuff

    Node* getFirstNode()
    {

        // if list is empty, return 0
        if (m_numberOfNodes == 0)		
        {
            return 0;
        }
        else
        {
            return m_firstNode;
        }
    }
};

Now apparently I don't want to use this solution for more complex methods, so I would be grateful on tips how to get this to work non-inline.
  #3  
Old 20-Sep-2007, 23:39
Kimmo Kimmo is offline
Member
 
Join Date: Mar 2007
Location: Finland
Posts: 229
Kimmo has a spectacular aura aboutKimmo has a spectacular aura about

Re: Return pointer to template's nested class problem


Yet another morning and more business.

So I thought "I wonder how this compiles on GCC" and went off to test it. And, believe it or not, GCC told me in plain English what was wrong.

Here's how it's supposed to be for those interested.

CPP / C++ / C Code:
template <typename dataType>
typename LinkedList<dataType>::Node* returnNode()
{}

However, before getting this plain-Englished answer handed to me, I realized that returning a Node is just plain wrong. It's the data I want to return, not Node. So I changed the design already and this is not that relevant in my linked list anymore, but might come in handy sometime.
__________________
Music, programming, endless learning..
 

Recent GIDBlogMaster?s Degree by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Hard drive/CPU Diagnoses Issues binarybug Computer Hardware Forum 1 22-Jan-2007 19:23
Pointer Usage in C++: Beginner to Advanced varunhome CPP / C++ Forum 0 19-Aug-2005 09:25
[Tutorial] Pointers in C (Part II) Stack Overflow C Programming Language 0 27-Apr-2005 17:36
C++ file I/O CronoX CPP / C++ Forum 36 09-Mar-2004 17:28

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:03.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.