简体   繁体   中英

error C2229: class 'VectorRemake<T>' has an illegal zero-sized array

template <class T>
class VectorRemake
{
private:
    T* list[];
    int count;
    int capacity;

public:
    VectorRemake() :capacity(DEFAULT_CAPACITY) :count(0) {list = new T[capacity];}
    VectorRemake(int capacity) :capacity(capacity) :count(0) {list = new T[capacity];}
~VectorRemake() {delete [] list;}

    ...
}

I'm not sure what i'm doing wrong here. It's the constructors that cause the problems.

void resize(int size, T t=T())
    {
        if (size < capacity)
        {
            for (int i = size; i < capacity; i++)
                T[i] = 0;
            count = size;
        }
        else if(size > capacity)
        {
            T *newlist = new T[size];

            for (int i = 0; i < count; i++) newlist[i] = list[i];
            for (int i = count; i < size; i++) newlist[i] = t;

            delete [] list;
            list = newlist;
        }
        else return;
        capacity = size;
    }

I'm getting 4 errors @T[i] = 0; (6th line).

I'm trying to set it to NULL, but my instructor told me that NULL isn't a c++ standard, what should I be doing?

Warning 1 warning C4091: '' : ignored on left of 'double' when no variable is declared 3\\solution11-3\\solution11-3.cpp 46 Error 2 error C2143: syntax error : missing ';' before '['
Error 3 error C2337: 'i' : attribute not found
Error 4 error C2143: syntax error : missing ';' before '='

T* list[]; is array of pointers (array of arrays if dynamically allocated). And you can't use open arrays as attributes.

You most likely wanted T* list;

It's not the constructor. It's T* list[] which defines a member list as an array of pointers to T without giving the array's size.
You probably want T* list; instead.

Also note that, according to the Rule of Three , your class, having a destructor, will also need a copy constructor and an assignment operator .
Implement the assignment operator on top of the destructor and the copy constructor using the Copy & Swap idiom .

Then, the syntax for your initialization list is wrong. It's

VectorRemake() : capacity(DEFAULT_CAPACITY), count(0) {list = new T[capacity];}
//                                         ^
//                                         comma, not colon

Finally, in C++ a class definition must be followed by a semicolon , otherwise you'll get funny errors in code following your class definition.
The reason for this is that a class definition might be part of a variable definition:

class foo { ... } bar;

(This defines bar to be a variable of the type foo . In fact, it's even possible to use a class that doesn't have a name:

class { ... } foobar;

although that is rarely done.)

The compiler needs the semicolon to know whether

class x {}

y yaddayadda ...

is the definition of y as an instance of x or the beginning of whatever yaddayadda might be.

The declaration of list should be T* list; . Your current declaration says that you want an unsized-array of pointers-to- T .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM