Thread: a strange error!!I can't understand why!!please help me!!

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    52

    a strange error!!I can't understand why!!please help me!!

    I'm using Visual C++ Studio compiler,Console Application ,when I debug my code,I edited some errors of code.After that
    I exited debugging mode and I debuged again..But the arrow pointed to places that blank lines(did't have code),and value of parameters still changed ,totally values are not true or not suitable with lines arrow pointed to...Even I exited my compiler and open it again ,but this still didn't change... I don't understand why??....Please help me!!thanks very much....
    Last edited by zaracattle; 10-06-2006 at 09:03 PM.

  2. #2
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Post code... and the error messages. and what version of Visual Studio it is.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    52
    I'm using Visual C++ 6.0,I think my code is true,It can compile and run,but when I debug,it has this error....

    "NodeList.h"
    Code:
    #ifndef NODELIST_H
    #define NODELIST_H
    template <class Entry>
    struct Node{
    	Node();
    	Entry entry;
    	Node<Entry> * next;
    	Node(const Entry &item, Node<Entry> * p =NULL);
    };
    #endif
    "NodeList.cpp"
    Code:
    #ifndef NODELIST_CPP
    #define NODELIST_CPP
    #include "NodeList.h"
    template <class Entry>
    Node<Entry>::Node(){
    	next = NULL;
    }
    
    template <class Entry>
    Node<Entry>::Node(const Entry & item, Node<Entry> * p){
    	entry = item;
    	next = p;
    }
    
    #endif
    "List.cpp"

    Code:
    #ifndef LIST_CPP
    #define LIST_CPP
    #include "List.h"
    
    template <class Entry>
    List<Entry>::List(){
    	count = 0;
    	pchinh = NULL;
    	pcurrent = NULL;
    	CurrentPosition = 0;
    }
    
    template <class Entry>
    List<Entry>::~List(){
    	while(!Empty())
    		Remove();
    }
    	
    
    template <class Entry>
    List<Entry>::List(const List<Entry> & list0){
    	Node<Entry> *psao, * pduyet = list0.pchinh;
    	if (pduyet==NULL) pchinh = NULL;
    	else
    	{
    		pchinh = psao = new Node<Entry> (pduyet->entry);
    		while (pduyet->next != NULL)
    		{
    			pduyet = pduyet->next;
    			psao->next = new Node<Entry>(pduyet->entry);
    			psao = psao-> next;
    		}
    	}
    	return sucess;
    }
    
    template <class Entry>
    List<Entry>& List<Entry>::operator=(const List<Entry> & list0){
    	Node <Entry> *ptam, *psao, *pduyet = list0.pchinh;
    	count = 1;
    	if (pduyet==NULL) ptam = NULL;
    	else
    	{
    		ptam = psao = new Node<Entry> (pduyet->entry);
    		while(pduyet->next!= NULL)
    		{
    			pduyet = pduyet->next;
    			psao->next = new Node<Entry>(pduyet->entry);
    			psao = psao -> next;
    			count++;
    		}
    	}
    	while(!Empty())
    		Remove();
    		pchinh = ptam;
    		return (*this);
    }
    
    template <class Entry>
    ErrorCode List<Entry>::Insert(const Entry&item, int position){
    	Node <Entry> *ptruoc, *psau, *newnode = new Node<Entry>(item);
        if (newnode == NULL)
    	{
    		 cout<<"Da het bo nho!Khong cap phat duoc nua.";
    		 return overflow;
    	}
    	if (position < 0 || position > count)
    	{
    		cout<<"Vi tri chen khong hop le!";
    		return errorrange;
    	}
    
    	if (position == 0)
    	{
    		if (pchinh ==NULL)
    		{
    		pchinh = newnode;
    		count++;
    		return sucess;
    		}
    		else
    		{
    		newnode -> next = pchinh;
    		pchinh = newnode;
    		count++;
    		return sucess;
    		}
    	}
    	else
    	ptruoc = SetPosition(position-1);
    	psau = ptruoc->next;
    	newnode->next = psau;
    	ptruoc -> next = newnode;
    	count++;
    	return sucess;
    }
    
    template <class Entry>
    ErrorCode List<Entry>::Remove(int position){
    	Node<Entry> * psau, *ptruoc, *ptam = pchinh;
    	if (position < 0 || position >= count)
    	{
    		cout<<"Vi tri xoa khong hop le!";
    		return errorrange;
    	}
    	if (position == 0)
    		if (pchinh == NULL)
    		{
    			cout<<"Chua co phan tu nao de xoa!";
    			return errorcode;
    		}
    		else
    		{
    		pchinh = pchinh->next;
    		count--;
    		delete ptam;
    		return sucess;
    		}
    	else
    		ptruoc = SetPosition(position-1);
    		psau = ptruoc->next;
    		ptam = psau;
    		psau = psau -> next;
    		delete ptam;
    		count--;
    		return sucess;
    }
    
    template <class Entry>
    ErrorCode List<Entry>::Replace(const Entry &item, int position){
    	Node<Entry> * psau, *ptruoc;
    	if (position < 0 || position > count)
    	{
    		cout<<"Vi tri thay the khong hop le!";
    		return errorrange;
    	}
    
    	if (position == 0)
    		if (pchinh==NULL)
    		{
    			cout<<"Chua co phan tu nao de thay the!";
    			return errorcode;
    		}
    		else
    		{
    		pchinh->entry = item;
    		return sucess;
    		}
    	else
    	ptruoc = SetPosition(position-1);
    	psau = ptruoc->next;
    	psau->entry = item;
    	return sucess;
    }
    
    template <class Entry>
    ErrorCode List<Entry>::Retrieve(Entry &item,int position){
    	Node<Entry> * psau, *ptruoc;
    	if (position < 0 || position > count)
    	{
    		cout<<"Vi tri tim khong hop le!";
    		return errorrange;
    	}
    
    	if (position == 0){
    		item = pchinh->entry;
    		cout<<item<<endl;
    		return sucess;
    	}
    
    	else
    	{
    		ptruoc = SetPosition(position-1);
    		psau = ptruoc -> next;
    	}
    		
    	item = psau->entry;
    	cout<<item<<endl;
    	return sucess;
    }
    
    template <class Entry>
    Node<Entry> * List<Entry>::SetPosition(int position) const{
    	Node<Entry> * ptam = pchinh;
    	if (CurrentPosition > =position)
    	{
    	for (int i =0;i<position;i++)
    		ptam = ptam->next;
    		pcurrent = ptam;
    		CurrentPosition = position;
    		return ptam;
    	}
    	else
    		for (int i = CurrentPosition;i<position;i++)
    			pcurrent = pcurrent -> next;
    			CurrentPosition = position;
    			return pcurrent;
    }
    
    template <class Entry>
    int List<Entry>::Size()const {
    	return count;
    }
    
    template <class Entry>
    bool List<Entry>::Empty() const {
    	if (count == 0)
    		return true;
    	else
    	return true;	
    }
    
    template <class Entry>
    bool List<Entry>::Full() const{
      NodeList<Entry> * location;
      try
      {
        location = new NodeList<Entry>;
        delete location;
        return false;
      }
      catch(std::bad_alloc exception)
      {
        return true;
      }
    }
    
    template <class Entry>
    void List<Entry>::Clear() const {
    	int i;
    	while (count!=0)
    		Remove(i);
    }
    
    template <class Entry>
    void List<Entry>::Traverse(void (*duyet)(Entry & item)){
    	Node<Entry> * ptam= pchinh;
    	for (int i =0;i<count;i++)
    	{
    		(*duyet)(ptam->entry);
    		ptam = ptam->next;
    	}
    }
    
    #endif
    "List.h"

    Code:
    #ifndef LIST_H
    #define List_H
    
    #include "NodeList.h"
    enum ErrorCode{sucess, underflow, overflow, errorrange, errorcode};
    template <class Entry>
    class List{
    	public:
    	List();
    	~List();
    	List(const List<Entry> &);
    	List<Entry> &operator=(const List<Entry> &);
    	int Size() const;
    	void Clear() const;
    	bool Empty() const ;
    	bool Full() const;
    	ErrorCode Insert(const Entry &item = 0, int position = 0);
    	ErrorCode Remove(int position = 0);
    	ErrorCode Replace(const Entry &item = 0 ,  int position = 0 );
    	ErrorCode Retrieve(Entry &item , int position = 0);
    	void Traverse(void (*duyet)(Entry&x));
    	private:
    		int count;
    		Node<Entry> * pchinh;
    		mutable int CurrentPosition;
    		mutable Node<Entry> * pcurrent;
    	protected:
    		Node<Entry> * SetPosition(int position) const;
    };
    
    #endif
    "UseList.cpp"
    Code:
    #include "stdafx.h"
    #include "List.cpp"
    #include "NodeList.cpp"
    #include <iostream>
    #include <conio.h>
    using namespace std;
    
    void main(){
    	List<int> list1;
    	List<int> list2;
    	int i;
    	list1.Insert(2);
    	list1.Insert(4,1);
    	list1.Insert(7,2);
    	list1.Insert(12,3);
    	list1.Insert(14,4);
    	list1.Insert(9,5);
    	//list1.Remove(4);
    	list1.Replace(5);
    	list1.Replace(9,3);
    	list2 = list1;
    	getch();
    }
    Example when I debug Remove function I have arrow:

    Code:
    template <class Entry>
    ErrorCode List<Entry>::Remove(int position){
    	Node<Entry> * psau, *ptruoc, *ptam = pchinh;
    
    arrow point here although don't have code this line
    
    After that arrow is here	if (position < 0 || position >= count)
    	{
    		cout<<"Vi tri xoa khong hop le!";
    		return errorrange;
    	}
    
    After that Arrow points here(don't have code this line)
    
    	if (position == 0)
    		if (pchinh == NULL)
    		{
    			cout<<"Chua co phan tu nao de xoa!";
    			return errorcode;
    		}
    		else
    		{
    		pchinh = pchinh->next;
    		count--;
    		delete ptam;
    		return sucess;
    		}
    	else
    		ptruoc = SetPosition(position-1);
    		psau = ptruoc->next;
    		ptam = psau;
    		psau = psau -> next;
    		delete ptam;
    		count--;
    		return sucess;
    }
    So Value of parameters change and I can't manage them....
    Last edited by zaracattle; 10-06-2006 at 09:23 PM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > http://cboard.cprogramming.com/showthread.php?t=83656
    Why do you still have bits of your templates in .cpp files?

    > I'm using Visual C++ 6.0
    Are you completely up to date with service packs ?
    VC6 template handling is pretty poor by comparison with more modern compilers.

    Since Visual Studio express is free, I would suggest you get that as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    52
    I'm using service packs 6 of visual C++ 6.0
    My program is true,it can compile and run,but in debugging mode ,sometimes I have this error,only when I edit code in debugging mode,if I exit debugging mode and edit,It OK...
    I think if we can divide files that are very long into file shorter,It's good....
    please tell me where I can get Visual Studio express....
    Thanks for your interesting.....
    Like you saw,my codes are very long,if I write them into a file it's very long and hard for me to edit,find error...

    If you can ,you can show me way to write code better that is easy to manage code,edit code, find error...Thanks very much
    Last edited by zaracattle; 10-07-2006 at 01:03 AM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    http://msdn.microsoft.com/vstudio/express/

    > only when I edit code in debugging mode,if I exit debugging mode and edit,It OK
    Ah, the edit and continue feature.
    I say live with it.
    VSE might make a better job of it, but edit-continue does some pretty horrid things and no doubt templates don't make it any easier either.


    > Like you saw,my codes are very long,if I write them into a file it's very long
    I would suggest you fully debug your linked list using say just ONE type (say an int), using regular source files. Only when it's fully debugged do you make it a template for any type.

    I mean, if it works for int, then it will work for string, double, messages, lists or anything else you can think of.


    > void main()
    It's int main you know - see the FAQ
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Quote Originally Posted by Salem
    > void main()
    It's int main you know - see the FAQ
    Or see Salem's avatar.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange concat bugg
    By Zarniwoop in forum C Programming
    Replies: 7
    Last Post: 05-01-2008, 09:43 PM
  2. strange number problem
    By kkjj in forum C Programming
    Replies: 9
    Last Post: 08-09-2007, 07:30 AM
  3. Strange results using dnsapi and windns
    By Niara in forum Networking/Device Communication
    Replies: 3
    Last Post: 08-13-2005, 10:21 AM
  4. strange strange functions
    By threahdead in forum C Programming
    Replies: 4
    Last Post: 10-13-2002, 05:31 PM
  5. bcc32 compiling error (really strange!!)
    By jester in forum C++ Programming
    Replies: 14
    Last Post: 01-26-2002, 04:00 PM