Solved

vector(STL)

Posted on 2000-04-30
3
301 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

762 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now