0
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 '='

Caleb Jares
  • 6,163
  • 6
  • 56
  • 83
  • Initialize `list` in the initializer list. – GManNickG Nov 21 '10 at 20:23
  • Okay, i reduced it to one error by changing it to T* list; I also changed the second constructor to: VectorRemake(int capacity) :capacity(capacity), :count(0), :list(new T[capacity]) {} and I am now getting error: C2059: syntax error : ':' – Caleb Jares Nov 21 '10 at 20:27
  • Remove all but the first colon. – fredoverflow Nov 21 '10 at 20:31
  • @GMan: __NO!__ That would blow up (hopefully - worse things could happen, though), because members are initialized in the order they are declared within the class definition, and that's unsuitable in this case (and fragile in general). – sbi Nov 21 '10 at 20:39
  • cable, don't destroy elements in your array by doing `list[i] = 0;`. You don't know what `T` might be, if it can be initialized with `0` at all, and if that doesn't blow up into your face. (It does for `std::string`.) You can default-construct an object using the `T()` syntax. `std::vector` does this even better, it allocates raw storage and constructs/destructs objects in-place, but leave that as an optimization for once you have mastered this stage. – sbi Nov 21 '10 at 20:51
  • Oh, and the right type for indexing of arrays would be `std::size_t`, not `int`. – sbi Nov 21 '10 at 20:52
  • @GMan: Right, and then comes `capacity`, and cable initializes `list` using `capacity`. So `capacity` must be initialized before `list`. – sbi Nov 21 '10 at 20:54
  • @sbi: Oh. I was just thinking don't do that. Initialize without going through `capacity`. But meh. – GManNickG Nov 21 '10 at 21:48

3 Answers3

3

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;

Šimon Tóth
  • 35,456
  • 20
  • 106
  • 151
2

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.

Community
  • 1
  • 1
sbi
  • 219,715
  • 46
  • 258
  • 445
  • When I change it to T* list; I get 102 errors from xstring. one of them is: error C2205: 'std::ctype `RTTI Type Descriptor'' : cannot initialize extern variables with block scope – Caleb Jares Nov 21 '10 at 20:23
  • @cable729: That's probably because in some translation unit your header is included before (some header which includes) ``. Sometimes, syntax errors only show up in code following the errors, that's how your errors might cause the compiler choking on other code. – sbi Nov 21 '10 at 20:32
  • @cable729: Just added a hint at what might cause this. – sbi Nov 21 '10 at 20:33
  • I just sent the edit through. As to the rule of three, i do have an assignment operator, but do I need to implement a copy constructor, because it looks like c++ automatically makes it for me. – Caleb Jares Nov 21 '10 at 20:35
  • thanks, i figured it out, it should have been list[i] instead of T[i]. that was a dumb mistake. also, when would one ever want to use class foo { ... } bar; ? – Caleb Jares Nov 21 '10 at 20:40
  • 1
    @cable729: Your code above doesn't show a copy ctor. The compiler "helpfully" generates a copy ctor and an assignment op for you, but these are wrong, because they make shallow copies of the data members, resulting in two instances of your class having their `list` members pointing to the same object, their destructors deleting it twice... – sbi Nov 21 '10 at 20:41
  • 1
    @cable729: We have inherited `struct { ... } foo;` from C (class being little more than a synonym to `struct`) and won't get rid of it, no matter how rarely it is needed. – sbi Nov 21 '10 at 20:43
1

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

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680