Design/usage problems in your code notwithstanding, the most obvious problem is that you want to put the class definition in the functs.h
file instead of the functs.cpp
file:
functs.h
:
// This is declaration is highly not recommended for use in header files.
// using namespace std;
#ifndef FUNCTS_H_INCLUDED
#define FUNCTS_H_INCLUDED
#include <string>
class ArrayList{
public:
void add(int num);
void add(int num, int index);
void remove(int index);
void removeNum(int num);
std::string toString();
ArrayList(int init);
private:
void resize();
int size, cap;
int *myList[10];
};
#endif // FUNCTS_H_INCLUDED
functs.cpp
:
#include "functs.h"
void ArrayList::add(int num){
if (size>=cap/2)
{
resize();
}
*myList[size] = num;
size++;
}
void ArrayList::resize(){
int temp[cap*2];
int i;
for (i = 0; i < size; i++)
{
temp[i] = *myList[i];
}
*myList = temp;
}
ArrayList::ArrayList(){
size = 0;
cap = 10;
}
void ArrayList::add(int num, int index){
int temp = *myList[index];
int i;
for (i = index; i < size; i++)
{
*myList[index] = num;
num = temp;
temp = *myList[i+1];
}
add(temp);
}
std::string ArrayList::toString(){
std::string returnString = "{";
int i;
for (i = 0; i < size; i++)
returnString.append(*myList[i] +",");
returnString.replace(returnString.length()-1,1,"}");
return returnString;
}
templatetypedef provides a reason why this is necessary. Basically the compiler needs to know how much space an ArrayList
needs, and a class ArrayList;
provides no such information.
It's not a good idea to declare using namespace std;
inside a header file, because then everyone that includes the functs.h
file (including your clients!) will also have a using namespace std;
, increasing the possibility of name collisions.
I highly recommend that you pick up a good introductory C++ book if you wish to learn C++ properly. You demonstrate in your question a rather big misunderstanding of how good C++ is written. That's not to say you're incompetent as a person, but there are some serious problems with the code you present. Just to name a few:
- Standard C++ already provides a perfectly fine array class called
std::vector
. There's no need to reinvent the wheel for what you're doing. And even if you have to reinvent the wheel, an advanced understanding of C++ and plenty of C++ experience is a prerequisite to implementing an array container that's appropriate for production use.
- The public interface of your class is incomplete. There's no way for clients to know how many elements are actually in the array.
int *myList[10];
declares a fixed array of 10 pointers to an int
. This is not appropriate for an array class. Especially if you want the array to be resizable.
- There's not sufficient memory management for this class to be useful in any sense. There are no destructors and apparently the constructors are not complete (nor do they match), so you have no real logical place to put things like
new[]
and delete[]
.
- You have a
ArrayList *al = new ArrayList;
but you don't have a corresponding delete al;
anywhere. This is a memory leak.
- But the last point is moot because you should be using
ArrayList a1;
instead of ArrayList *al = new ArrayList;
. The former will automatically "delete" itself at the end of the scope (in this case, the main()
function) while the latter requires a delete
statement. C++ is not like Java where unused new
'ed objects are automatically collected.
I can't comment on the correctness of the algorithms you used, because (and I'm sorry to say this because it'll sound harsh) what you have simply won't work. Again, I recommend that you pick up a good introductory C++ book, which will cover these kinds of issues. (I must emphasize that none of these shortcomings are a statement of you as a person. I'm talking specifically about the code you have in your question.)