LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-29-2010, 09:31 AM   #1
Asido
Member
 
Registered: Jan 2010
Location: Denmark
Distribution: Gentoo, Archlinux, FreeBSD, Slackware
Posts: 84

Rep: Reputation: 24
first c++ mini-project. an issue


Hello guys.

I am trying to write my first small project with C++ - implement stl list like collection class. This is what i have a the moment:
Code:
#ifndef GUARD_link_list_h

#define GUARD_link_list_h

template <class T>
struct Node {
	public:
		Node() { link = NULL; }
		Node(const Node* l, const T& d) { link = l; data = d; }

		T& get_data() { return data; }
		void set_data(const T& v) { data = v; }
		Node* get_link() { return link; }
		void set_link(Node* l) { link = l; }

	private:
		Node* link;
		T data;
};

template <class T>
class Link_list {
public:
	typedef T* iterator;
	typedef const T* const_iterator;
	typedef T value_type;
	typedef size_t size_type;

	// The Rule of Three
	Link_list () { init(); }
	virtual ~Link_list () {}
	//Link_list& operator=(const Link_list&);

	// operators
	T* operator++() { return next(); }

	// methods to manage the collection
	void push_back(const T&);
	iterator begin() { return first; }
	iterator end() { return last; }
	bool empty() { return first == 0; }

private:
	Node<T>* node;
	iterator first;
	iterator last;
	
	// private methods
	void init();
	T* next();
};

template <class T>
void Link_list<T>::init() {
	first = last = 0;
}

template <class T>
void Link_list<T>::push_back(const T& v) {
	Node<T> *n = new Node<T>;
	n->set_data(v);
	n->set_link(node);
	node = n;
	if(!empty())
		last = &(node->get_data());
	else
		first = last = &(node->get_data());
}

template <class T>
T* Link_list<T>::next() {
	node = node.get_link();
	return node.get_data();
}

#endif /* end of include guard: GUARD_link_list_h */
I know it's not a very nice code.
The issue I have is with data values. I test it with this code:
Code:
Link_list<int> t;
	t.push_back(5);
	t.push_back(9);
	t.push_back(2);

	Link_list<int>::iterator it = t.begin();
	std::cout << *it << std::endl;
	++it;
	std::cout << *it << std::endl;

	Link_list<int>::iterator it2 = t.end();
	std::cout << *it << std::endl;
The begin() and end() is alright, but the problem is when I try to increment the iterator. The value is 0, while I expect 9.
What did I do wrong here (presumably with pointers)?
Also, some comments on coding style would be nice about bad C++ programming practice you might notice here.

Last edited by Asido; 12-29-2010 at 09:38 AM.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 12-29-2010, 09:46 AM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by Asido View Post
typedef T* iterator;
That means a Link_list::iterator is exactly a T* and acts in all ways as a T*. You can't redefine any of the operation of Link_list::iterator.

Quote:
T* operator++() { return next(); }
That is an operator on Link_list, not on Link_list::iterator.

Quote:
Also, some comments on coding style would be nice about bad C++ programming practice you might notice here.
Code:
        Node() { link = NULL; }
        Node(const Node* l, const T& d) { link = l; data = d; }
NULL is bad style for C++, just use 0.
Too much one one line is bad style.
Assignments inside the constructor body are bad style for member initialization in C++. Use the special syntax for member initialization:

Code:
        Node() :
           link( 0 )
        {}
        Node(const Node* l, const T& d) :
           link( l ),
           data( d )
        {}

Last edited by johnsfine; 12-29-2010 at 09:51 AM.
 
2 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
GRUB issue, multibooting on my Dell Mini NssOne Linux - Laptop and Netbook 3 02-26-2009 03:42 PM
mini project title hariji Linux - General 1 07-13-2007 12:57 PM
help in linux mini project needed. truelinux Programming 4 09-01-2005 08:46 AM
linux mini project avadhootak Linux - Software 1 11-26-2003 10:08 PM
linux mini project avadhootak Linux - General 5 11-25-2003 11:34 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration