?
Solved

vector(STL)

Posted on 2000-04-30
3
Medium Priority
?
343 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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 viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

764 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