?
Solved

vector(STL)

Posted on 2000-04-30
3
Medium Priority
?
352 Views
Last Modified: 2013-12-14
I use a vector to store some instances of a class. right now I need to remove all elements in the vector, I tried to use clear() function but was told "vector<classname>::clear() undefined". what's wrong?
0
Comment
Question by:Embt
  • 2
3 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 2764390
that is the name of the function!   it should be defined.  Did you call it like a member function, i.e. like

vector<int> V;

V.clear():

Are you using the right include file?  You should use <vector> not <vector.h>.  (if you have one).  <vector> will place the vector class name in the ""std" namespace though, so you may wish to add a

using namespace std;

line too.
0
 

Author Comment

by:Embt
ID: 2764422
thanks for the response. I have no problem with header file etc. the problem is with the type of elements in the vector.  it is something like:

vector <MyClass> V;

when I called V.clear(), I was told member function vector<MyClass>::clear() undefined. if I use some other types like int, that will be fine.  I am not familiar with C++ class, I think I should define something in the class to make the function clear() work, right?
0
 
LVL 22

Accepted Solution

by:
nietod earned 120 total points
ID: 2764442
I think you are interpreting the eror message incorrectly--or it is a really bad error message (maybe that is even more likely!).

Anyways what I think is happening is that your class (MyClass) does not define some member function needed by the vector class.  To work within the vector class, your class needs to have:

a default constructor
a destructor
a copy constructor
an assigment operator (=).

None of these have to be explicitly defined though.  i.e. the compiler will trie to generate these if possible, but might fail for various reasons.

If that doesn't help, post your class definition, and the exact error message.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question