Solved

vector(STL)

Posted on 2000-04-30
3
329 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 30 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

749 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