Constructor not constructing!

This is a discussion on Constructor not constructing! within the C++ Programming forums, part of the General Programming Boards category; Hello again! following this little blurb are two things, my main.cpp which calls my class bag and my bag.h where ...

  1. #1
    Slime Dragoon_42's Avatar
    Join Date
    Feb 2003
    Location
    Vancouver
    Posts
    90

    Constructor not constructing!

    Hello again! following this little blurb are two things, my main.cpp which calls my class bag and my bag.h where bags and iterators for those bags are defined. When I construct a bag in main.cpp my program crashes and I can't figure out why. Any help would be appreciated!

    main.cpp
    Code:
    //Program Name:  main.cpp
    #include<iostream>
    #include<fstream>
    #include<string>
    #include<math.h>
    #include<time.h>
    using namespace std;
    #include "bag.h"
    
    void main() {
    	string file1;
    	ifstream fin;
    	cout<<"Where is your list of chores?\n";
    	cin>>file1;
    
    	fin.open(file1.c_str());
    	bag<string> chores;
    	char item[256];
    	int x=0;
    
    	while((cin.getline(item,256,'\n'))!=NULL)
    	{
    		chores.insert(item);
    	}
    
    	bag<string>::Iterator itr;
    
    	cout<<"Here is your list of chores:\n";
    	for(itr=chores.begin();!(itr==chores.end());++itr)
    		cout<<*itr;
    
    	itr=chores.begin();
    	string chore;
    	char task;
    
    	while(!(chores.begin()==chores.end()))
    	{
    		srand(unsigned(time(NULL)));
    		int x=(rand()%chores.size());  //random number 1-6
    		cout<<"Would you like to "<<*itr<<"?  (y/n)\n";
    		cin>>task;
    
    		if(task=='Y'||task=='y')
    		{
    			cout<<"When you're finished come back and hit enter.";
    			cin.get();
    			cout<<"Good!  You've finished your task!\n";
    			chore=*itr;
    			chores.remove(chore);
    		}
    		else
    		{
    			cout<<"Okay, we'll do that later.\n";
    		}
    	}
    	cout<<"You're completed all of your chores, go have fun!\n";
    }
    bag.h
    Code:
    //Programe Name: Bag.h
    template<class item>
    class BIter
    {
    	public:
    		BIter(){}
    		BIter(item* p){}
    		item& operator*() {return *ptr;}
    		BIter& operator++(){ptr++; return *this; }
    		bool operator==(const BIter& b)
    		{
    			return ptr==b.ptr;
    		}
    	private:
    		item *ptr;
    };
    
    template<class type>//allows more variability than typedef
    class bag
    {
    public:
    	bag(int init_cap=30);
    	bag(bag& b); //copy constructor
    	void insert(type);
    	void remove(type);
    	void operator=(bag b);
    	//Post: The bag has a deep copy of bag b
    	int size(){return used;}
    	~bag();//destructor
    	typedef BIter<type> Iterator;
    	Iterator begin()
    	{
    		Iterator x=pBegin;
    		return x;
    	}
    	Iterator end()
    	{
    		Iterator x=pEnd;
    		return x;
    	}
    
    private:
    	int used;//a member to record the number of items in bag
    	type *pData;//a pointer to point to the dynamic array
    	int capacity;//a member to record the capacity of a bag
    	type *pBegin;
    	type *pEnd;
    };
    
    
    template<class type>
    bag<type>::bag(int init_cap)
    {
    	used=0;//an empty bag
    	pData=new type[init_cap];//a dynamic array is created
    	                         //and assigned to the pointer pData
    	capacity=init_cap;
    	*pBegin=pData[0];
    	*pEnd=pData[used];
    }
    
    template<class type>
    void bag<type>::insert(type x)
    {
    	if(used==capacity)
    		return;
    	pData[used]=x;
    	used++;
    	*pEnd=pData[used];
    }
    
    template<class type>
    void bag<type>::remove(type t)
    {
    	for(int i=0;i<used;i++)
    		if(pData[i]==t)
    		{
    			pData[i]=pData[used-1];
    			used--;
    			return;
    		}
    }
    
    template<class type>
    void bag<type>::operator=(bag b)
    {
    	if(capacity!=b.capacity)
    		return;
    
    	for(int i=0;i<b.used;i++)
    		pData[i]=b.pData[i];
    
    	used=b.used;
    }
    
    template<class type>
    bag<type>::bag(bag& b)
    {
    	capacity=b.capacity;
    	pData=new type[capacity];
    	for(int i=0;i<b.used;i++)
    		pData[i]=b.pData[i];
    	used=b.used;
    }
    
    template<class type>
    bag<type>::~bag()
    {
    	delete [] pData;
    }

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    *pBegin=pData[0];
    *pEnd=pData[used];

    should be

    pBegin=&pData[0];
    pEnd=&pData[used];

    Both pBegin and pEnd are pointers. They don't initially point at anything, so de-referencing is causing an access-violation. In any case, you want to be storing the addresses not the actual objects so what you're trying to do is wrong. Read up on pointers if you're not sure.
    Joe

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    36

    Copy constructor

    I didn't read the whole code thoroghly... but what i spotted at a glance is your copy constructor... It may not be the soultion you are expecting. What i know of is the parameter of the copy constructor has to be constant..
    [edit]
    bag(bag& b); //copy constructor
    [/edit]
    Code:
    bag(const bag& b); //copy constructor
    please correct me if i'm wrong coz i'm just into OOP...
    will be there one day!

    I usually use VStudio 6!

  4. #4
    Slime Dragoon_42's Avatar
    Join Date
    Feb 2003
    Location
    Vancouver
    Posts
    90
    Yeah, that's a problem, but now it's fixed. Thank you. BTW my program kind of works now! It does not do anything inside the while statements though, any ideas?

  5. #5
    Slime Dragoon_42's Avatar
    Join Date
    Feb 2003
    Location
    Vancouver
    Posts
    90
    here is how my code looks now


    main.cpp
    Code:
    #include<iostream>
    #include<fstream>
    #include<string>
    #include<math.h>
    #include<time.h>
    using namespace std;
    #include "bag.h"
    
    void main() {
    	string file1;
    	ifstream fin;
    
    	cout<<"Where is your list of chores?\n";
    	cin>>file1;
    
    	fin.open(file1.c_str());
    	bag<string> chores(20);
    	string temp;
    	int x=0;
    
    	while((fin>>temp)!=NULL)
    	{
    		chores.insert(temp);
    		cout<<"got one";
    	}
    
    	bag<string>::Iterator itr=chores.begin();
    
    	cout<<"Here is your list of chores:\n";
    	for(itr=chores.begin();!(itr==chores.end());++itr)
    		cout<<*itr;
    
    	string chore;
    	char task;
    
    	while(!(chores.begin()==chores.end()))
    	{
    		itr=chores.begin();
    		srand(unsigned(time(NULL)));
    		x=(rand()%chores.size());
    
    		for(int p=0;p<x;p++)
    			++itr;
    		cout<<"Would you like to "<<*itr<<"?  (y/n)\n";
    		cin>>task;
    
    		if(task=='Y'||task=='y')
    		{
    			cout<<"When you're finished come back and hit enter.";
    			cin.get();
    			cout<<"Good!  You've finished your task!\n";
    			chore=*itr;
    			chores.remove(chore);
    		}
    		else
    		{
    			cout<<"Okay, we'll do that later.\n";
    		}
    	}
    	cout<<"You've completed all of your chores, go have fun!\n";
    	fin.close();
    }
    bag.h
    Code:
    template<class item>
    class BIter
    {
    	public:
    		BIter(){}
    		BIter(item* p){}
    		item& operator*() {return *ptr;}
    		BIter& operator++(){ptr++; return *this; }
    		bool operator==(const BIter& b)
    		{
    			return (*ptr==*b.ptr);
    		}
    	private:
    		item *ptr;
    };
    
    template<class type>//allows more variability than typedef
    class bag
    {
    public:
    	bag(int init_cap=30);
    	bag(const bag& b) //copy constructor
    	{pData=NULL; *this=b;}
    	void insert(type);
    	void remove(type);
    	void operator=(bag b);
    	//Post: The bag has a deep copy of bag b
    	int size(){return used;}
    	~bag();//destructor
    	typedef BIter<type> Iterator;
    	Iterator begin()
    	{
    		Iterator x=pBegin;
    		return x;
    	}
    	Iterator end()
    	{
    		Iterator x=pEnd;
    		return x;
    	}
    
    private:
    	int used;//a member to record the number of items in bag
    	type *pData;//a pointer to point to the dynamic array
    	int capacity;//a member to record the capacity of a bag
    	type *pBegin;
    	type *pEnd;
    };
    
    
    template<class type>
    bag<type>::bag(int init_cap)
    {
    	used=0;//an empty bag
    	pData=new type[init_cap];//a dynamic array is created
    	                         //and assigned to the pointer pData
    	capacity=init_cap;
    	pBegin=&pData[0];
    	pEnd=&pData[used];
    }
    
    template<class type>
    void bag<type>::insert(type x)
    {
    	if(used==capacity)
    		return;
    	pData[used]=x;
    	used++;
    	pEnd++;
    }
    
    template<class type>
    void bag<type>::remove(type t)
    {
    	for(int i=0;i<used;i++)
    		if(pData[i]==t)
    		{
    			pData[i]=pData[used-1];
    			used--;
    			return;
    		}
    		pEnd=&pData[used];
    }
    
    template<class type>
    void bag<type>::operator=(bag b)
    {
    	if(capacity!=b.capacity)
    		return;
    
    	for(int i=0;i<b.used;i++)
    		pData[i]=b.pData[i];
    
    	used=b.used;
    }
    
    template<class type>
    bag<type>::~bag()
    {
    	delete [] pData;
    }

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    >BIter(item* p){}

    This isn't doing anything. You need to fix it.

    >for(itr=chores.begin();!(itr==chores.end());++itr )

    You already constructed itr. You may as well do -

    for(;!(itr==chores.end());++itr)

    Then you'll probably have to remove stuff from your input buffer before calling operator>> on cin.

    There's probably more that needs fixing. A debugger is your friend.
    Joe

  7. #7
    Slime Dragoon_42's Avatar
    Join Date
    Feb 2003
    Location
    Vancouver
    Posts
    90
    I'm going to thank you and apollogize at the same time. Thanks for all your help, those were problems that needed fixing. My biggest problem was that I wasn't reading the file I though I was reading. I was trying to read todo.txt, but notepad saved the file as todo.txt.txt So the problem was that the computer was doing what I was telling it to do, not what I wanted it to do. Oh well, thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I don't get weak_ptr's?
    By cpjust in forum C++ Programming
    Replies: 13
    Last Post: 12-09-2008, 01:28 AM
  2. Problem while constructing IP packet and sending using socket() system call
    By cavestine in forum Networking/Device Communication
    Replies: 10
    Last Post: 10-15-2007, 06:49 AM
  3. Double constructing of a class
    By DV64h in forum C++ Programming
    Replies: 5
    Last Post: 05-28-2006, 06:11 PM
  4. More random thoughts on constructing scripting languages.
    By suzakugaiden in forum Tech Board
    Replies: 2
    Last Post: 01-21-2006, 01:30 AM
  5. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 05:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21