I have this code
main.cpp
#include <iostream>
#include "functs.h"
using namespace std;
int main()
{
ArrayList *al = new ArrayList;
return 0;
}
functs.h
using namespace std;
#ifndef FUNCTS_H_INCLUDED
#define FUNCTS_H_INCLUDED
class ArrayList;
#endif // FUNCTS_H_INCLUDED
functs.cpp
#include <iostream>
#include "functs.h"
class ArrayList{
public:
void add(int num);
void add(int num, int index);
void remove(int index);
void removeNum(int num);
string toString();
ArrayList(int init);
private:
void resize();
int size, cap;
int *myList[10];
};
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);
}
string ArrayList::toString(){
string returnString = "{";
int i;
for (i = 0; i < size; i++)
returnString.append(*myList[i] +",");
returnString.replace(returnString.length()-1,1,"}");
return returnString;
}
and I’m extremely new to C++ but whenever I try to compile the code it gives me a “size of ArrayList is not know”. Please help me figure out the error. =(
Design/usage problems in your code notwithstanding, the most obvious problem is that you want to put the class definition in the
functs.hfile instead of thefuncts.cppfile:functs.h:functs.cpp:templatetypedef provides a reason why this is necessary. Basically the compiler needs to know how much space an
ArrayListneeds, and aclass 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 thefuncts.hfile (including your clients!) will also have ausing 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:
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.int *myList[10];declares a fixed array of 10 pointers to anint. This is not appropriate for an array class. Especially if you want the array to be resizable.new[]anddelete[].ArrayList *al = new ArrayList;but you don’t have a correspondingdelete al;anywhere. This is a memory leak.ArrayList a1;instead ofArrayList *al = new ArrayList;. The former will automatically “delete” itself at the end of the scope (in this case, themain()function) while the latter requires adeletestatement. C++ is not like Java where unusednew‘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.)