Vector as a class?

Just wondering if I can have a vector as a class? I have an array as a class, and I have the functions within to add/change/delete an item, and even resize the array if need be. However, I had a go with doing the same with a vector but I got an error.

I was just wondering if this is possible or am I asking too much of vector? If it is possible, I'll post the bit of code I did which flagged up an error.

Thanks,
S
You mean

1
2
3
class Foo {
   vector<int> v;
};


?

Yes, that's fine. Post your code.
Thanks for the reply, but I was thinking something more along the lines of:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class NewVector {
public:
	void AddValue(int p_item) {
		m_vector.push_back(p_item); //Here's where I get the error, something about m_vector not being class/struct/union
	}
private:
	int m_vector;
};

int main() {

	NewVector v1;

	v1.AddItem('1');
                return 0;
}


But it doesn't work, one line gives me an error.

Any ideas?
Well, of course it gives you a compiler error. You're trying to call a method of a native type, and native types don't have methods/members.

So, you want to write your own vector class, is that it? Is it because you need a vector class or because you want to see how vectors work?
I wanted to use a vector class. Orginally, I had an array class, but it gets resized alot so I thought I might be better with a vector.
In that case, you might be glad to know there already is a vector class defined in the standard library: http://www.cplusplus.com/reference/stl/vector/

Also, your "array" sounds an awful lot like a vector -- a dynamically resized data structure of adjacent elements of the same type. Anything that matches that description is a vector.
Topic archived. No new replies allowed.