开发者

size of ArrayList is not known help

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.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.)


The reason that you're getting this error is that in main.cpp, the compiler hasn't seen the definition for the class ArrayList. It's only seen the declaration

class ArrayList;

When you try to create the ArrayList by writing

new ArrayList;

The compiler doesn't know how much memory is needed to hold an ArrayList because it hasn't seen the class definition. This contrasts with other languages like Java, where this information doesn't have to immediately be available.

To fix this, update your .h file by moving the definition of the class from the .cpp file. That way, when someone #includes the header file, they'll get the class definition in addition to the declaration, which will allow you to use new, declare local variables of type ArrayList, etc.

Hope this helps!


This much

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];
};

Should be in the .h file.

Why? Because the declaration of a class (when you write class ArrayList;) is only enough when the size of the class is not needed (more specific cases are listed in the C++ standard). The definition of the class should appear in the same translation unit in which the class is used in a way in which it is required to be complete.


The way you've declared myList it has a fixed size; *myList = temp; isn't doing what you want it to.

Declare myList simply as int *myList;

In the constructor, use myList = new int[10];

Everywhere you have *myList[...] change it to myList[...]

In resize, int temp[cap*2] needs to be int *temp = new int[cap * 2] and *myList = temp needs to be myList = temp

You'll still have a memory leak, but that should get you started.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜