Help with C++ Container Class: Troubleshooting w/ VS C++ 6 & .NET 2003

In summary, the author is working on a templated container class called "bag" and is running into trouble. The header file contains #ifndef BAGTEMPLATE_H and #define BAGTEMPLATE_H defines a template class called "bag". The template file includes #include "Bag.template". The driver file includes #include "bagTemplate.h" and includes <iostream> and using namespace std;. The main() function creates an empty bag and prints "test 1". The main() function creates a bag of type int and prints "test 2". If an array of type T is attempted to be held by bag, an exception is thrown and the code needs
  • #1
Math Is Hard
Staff Emeritus
Science Advisor
Gold Member
4,652
37
C++ container class help

Hi, I'm working on a templated container class called "bag" and running into some trouble. This my header file, bagTemplate.h
Code:
#ifndef BAGTEMPLATE_H
#define BAGTEMPLATE_H

template <typename T>
class bag {
public: 
    bag(int num=10); // construct a bag to hold num elements   
	~bag( );                // destructor	
	int size(); //returns current size
	int count(const bag<T> & target) const;//counts # items of certain value in bag
	void insert (const bag<T> & new_item); //add one item to the bag, double cap if cap exceeded

   
private: 
    int used; // size, or number of elements currently stored in the bag
    int capacity; // the maximum capacity of the bag 
    bag <T> * container;   // internal storage array of pointers to items 
};  

#include "Bag.template" 
#endif
And this is my template file, bag.template:
Code:
#include <iostream>
#include <stdlib.h>

using namespace std;

// constructor builds a bag with the ability to hold
// capacity items; the bag is initially empty 
template <typename T>
bag<T> :: bag(int num) 
{
    capacity = num;
    used = 0;
    container = new bag<T> [num];	
}


// destructor destroys the bag by recycling the memory it used
template <typename T>
bag<T> :: ~bag( ) 
{
	delete [ ] container;
}


template <typename T>  
int bag<T>::size()
{
	return used;
}

template <typename T> 
int bag<T>::count(const bag<T> & target) const
{
		int ct = 0;
		for (int i = 0; i < used; i++)
		{
			if (target == container[i])
				c++;
		}
		return ct;
}

template <typename T> 
void bag<T>::insert (const bag<T> & new_item)
{
	if (used == capacity)
		capacity = capacity * 2;
	container[used ++] = new_item;

}
Here is my driver:
Code:
#include "bagTemplate.h"
#include <iostream>
using namespace std;

int main()
{
	cout << "test 1";	
	bag<int> b;
	cout << "test 2";	
	return 0;
}
When I run this, "test 1" prints, but "test 2" does not. It must be something simple I'm missing, as usual. I am not getting any error messages.
Any help is appreciated. I'm using VS C++ 6 at the moment. Will be trying it in VS C++ .NET 2003 when I get home.
tanx!
 
Last edited:
Physics news on Phys.org
  • #2
Are you sure this is right?

Code:
bag <T> * container;   // internal storage array of pointers to items

You want bag to hold an array of pointers to bags? Or do you want bag to hold an array of type Ts? If you want to create an array of type T then you should use

Code:
T * container;

If you want an array of pointers to other bags, then you should use this (although I'm not entirely sure this is legitimate):

Code:
bag <T> ** container;   // internal storage array of pointers to items

I'm assuming what you want is the former case, so you have to go through your code and change things like this..

Code:
template <typename T> 
void bag<T>::insert (const bag<T> & new_item)
{
	if (used == capacity)
		capacity = capacity * 2;
	container[used ++] = new_item;

}

to

Code:
template <typename T> 
void bag<T>::insert (const T & new_item)
{
	if (used == capacity)
		capacity = capacity * 2;
	container[used ++] = new_item;

}

I might have completely misunderstood what you're trying to do, but this is what I think the problem is. By the way, if you're using VS, then just debug your code with F10, until you get to this line in main() :

Code:
bag<int> b;

You'll see that on this line an exception is thrown here. If you use F11 you can step into the calls made at this point, and you'll see that the offending bit of code is what I mentioned before.
 
  • #3
You also might want to change this:
Code:
template <typename T> 
int bag<T>::count(const bag<T> & target) const
{
		int ct = 0;
		for (int i = 0; i < used; i++)
		{
			if (target == container[i])
				c++;
		}
		return ct;
}

to read ct++ instead of c++ ?
 
  • #4
thanks! good catches! I appreciate the help!
I'm trying to sort of kluge things together based on a templated link list class example we had in class so it's been a confusing process.
And yes, I'm trying to hold an array of type Ts, but we have to expand it dynamically as needed.
 
Last edited:
  • #5
Well if you're getting confused about whether you should be putting bag<T> or T then the best thing to do is to just write your class for integers only (ignoring templates for the time being), make sure all the methods work and so on. When you're satisfied it's working, then go through your code and generalise it to use a template. So where you would have "class bag{}" and "int *container" and so on, change it to read "template<typename T> class bad{};" and "T *container" etc. This might help.
 
  • #6
Thank you. You're absolutely right. I'll see if I can get everything going for one data type. The templating has been driving me nuts because it's been an extra layer of abstraction while I'm still trying to get a grasp of the fundamentals of classes.
 

Related to Help with C++ Container Class: Troubleshooting w/ VS C++ 6 & .NET 2003

1. What is a container class in C++?

A container class in C++ is a class that can hold and manage a collection of other objects or data. It provides methods for adding, removing, and accessing elements within the collection.

2. How do I troubleshoot issues with my C++ container class in Visual Studio C++ 6 and .NET 2003?

There are a few steps you can take to troubleshoot issues with your C++ container class in Visual Studio C++ 6 and .NET 2003. First, make sure you have the correct syntax and formatting for your container class. Then, check for any errors or warnings in your code and address them accordingly. You can also use the debugger in Visual Studio to step through your code and identify any errors. If you are still having trouble, consult online resources or reach out to the C++ community for assistance.

3. How do I add elements to my C++ container class?

To add elements to your C++ container class, you can use the "push_back" method for a vector or "insert" method for a list or map. These methods will add elements to the end of the container or at a specified position, respectively. You can also use the "emplace" method to add elements directly to the container without creating a temporary object.

4. How do I remove elements from my C++ container class?

To remove elements from your C++ container class, you can use the "pop_back" method for a vector, which will remove the last element in the container. For a list or map, you can use the "erase" method to remove a specific element or range of elements. You can also use the "clear" method to remove all elements from the container.

5. How do I access elements within my C++ container class?

To access elements within your C++ container class, you can use the "at" method for a vector, which takes an index as an argument and returns the element at that index. For a list or map, you can use the "front" or "back" methods to access the first or last element, respectively. You can also use iterators to traverse through the elements in the container.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
905
  • Engineering and Comp Sci Homework Help
Replies
8
Views
859
  • Engineering and Comp Sci Homework Help
Replies
15
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
957
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
765
  • Engineering and Comp Sci Homework Help
Replies
23
Views
7K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Programming and Computer Science
Replies
31
Views
2K
Back
Top