Avatar billede d_dogg Nybegynder
05. marts 2007 - 20:34 Der er 18 kommentarer

Mange errors

1>c:\documents and settings\makhdoom\my documents\visual studio 2005\projects\dynarray\dynarray\dynarray.h(61) : error C2061: syntax error : identifier 'ostream'
1>c:\documents and settings\makhdoom\my documents\visual studio 2005\projects\dynarray\dynarray\dynarray.h(61) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\documents and settings\makhdoom\my documents\visual studio 2005\projects\dynarray\dynarray\dynarray.h(61) : error C2805: binary 'operator <<' has too few parameters
1>c:\documents and settings\makhdoom\my documents\visual studio 2005\projects\dynarray\dynarray\main.cpp(14) : error C2065: 'cout' : undeclared identifier
1>c:\documents and settings\makhdoom\my documents\visual studio 2005\projects\dynarray\dynarray\main.cpp(14) : error C2065: 'endl' : undeclared identifier
1>Generating Code...
____________________________________________________________


#include "iostream"

typedef double itemType; // need for another type, change it here

class dynarray
{
    /*
    *    Outout a representation of this array to an output stream 'str'. Say,
    *
    *      dynarray var1;
    *      ............
    *      Do somthing with var1.
    *      ............
    *      cout << var1  ---> This function will be called now.
    *                          'cout' will be 'str'.
    *                          'var1' will be 'rhs'.
    *      ............
    *
    * 'str' => The output stream.
    * 'rhs' => The array to the right side of '<<' operator.
    *
    * Returns: The output stream after appending the representation of this array.
    * Throws:  None.
    */
    friend ostream& operator<<( ostream &str, const dynarray &rhs );

public:
    /*
    *    Create an arrays of this class.
    *
    * 's'  => Initial size of the array. Default size is 0.
    * 'inc' => Initial increment of the array. Default increment is 5.
    *
    * Returns: Nothing.
    * Throws:
    *    INVALID_SIZE if 's' is negative.
    *    INVALID_INCREMENT if 'inc' is negative.
    *    OUT_OF_MEMORY if memory is not available.
    */
    dynarray( int s = 0, int inc = 5 );


    /*
    *    Create an object of this class. The information (size, capacity, data etc)
    * will be copied from 'rhs' to 'this'.
    *
    * 'rhs' => An object of 'dynarray'.
    *
    * Returns: Nothing.
    * Throws:
    *    OUT_OF_MEMORY if memory is not available.
    */
    dynarray( const dynarray &rhs );

    /*
    *    Destroy the object. The memory of the array will be freed.
    *
    * Returns: Nothing.
    * Throws:  None.
    */
    ~dynarray();

    /*
    *    The item 'item' will be stored after the last element of this array.
    * If no space is available, then the capacity will be increased at the value of 'inc'.
    * For this, necessary spaces will be allocated.
    *
    * 'item' => The item to insert.
    *
    * Returns: Nothing.
    * Throws:
    *    OUT_OF_MEMORY if memory is not available.
    */
    void push_back( itemType item );


    /*
    *    The item 'item' will be stored at the position 'pos'. All the elements from 'pos'
    * to the last will be shifted to the right by 1 step. For this, necessary spaces
    * will be allocated. If 'pos' equals to 'num', then 'item' will be placed at the last.
    *
    * 'item' => The item to insert.
    * 'pos'  => The position where to insert. It must be in the range [0, num].
    *
    * Returns: Nothing.
    * Throws:
    *    INVALID_POSITION if 'pos' is not in the range [0, num].
    *    OUT_OF_MEMORY if memory is not available.
    */
    void insert( itemType item, int pos );

    /*
    *    Remove the last element of the array from it. Also returns this element.
    * After removing the element, if the extra space (capacity - size) in the array
    * is more than 2 * increment, then 'inc' amount of space will be truncated.
    * For example, say, size = 20, capacity = 20, inc = 5.
    *    Now if we call 'pop_back()' 10 times, then size = 10, capacity = 20, inc = 5.
    * Next call to this function will truncate the array. Spaces will be freed.
    * Then the situation will be, size = 9, capacity = 15, inc = 5. The other 5 spaces
    * are freed.
    *
    * Returns: The item which will be removed (popped).
    * Throws:
    *    POP_NOT_POSSIBLE if no data exists in the array.
    */
    itemType pop_back( );


    /*
    *    The element at the position 'pos' will be removed from the array. All the elements
    * from 'pos + 1' to the last will be shifted to the left by 1 step. After removing the
    * element, if the extra space (capacity - size) in the array is more than 2 * increment,
    * then 'inc' amount of space will be truncated. This case is similar to 'pop_back()'.
    *
    * 'pos'  => The position of the deleting item. It must be in the range [0, num).
    *
    * Returns: The item which will be erased.
    * Throws:
    *    INVALID_POSITION if 'pos' is not in the range [0, num).
    */
    itemType erase( int pos );

    /*
    *    Free all the spaces of the array. After calling this function, the situation will be
    * size = 0, capacity = 0. 'inc' will be retained.
    *
    * Returns: Nothing.
    * Throws:  None.
    */
    void clear( );

    /*
    *    Return the size of the array.
    *
    * Returns: The size of the array.
    * Throws:  None.
    */
    int size( ) const;

    /*
    *    Check whether the array is empty or not.
    *
    * Returns: true if there is no data in the array (i.e., if size = 0).
    *          false otherwise.
    *
    * Throws:  None.
    */
    bool empty( ) const;

    /*
    *    The element at the position 'pos' will be returned.
    *
    * 'pos' => The position of the requesting item. It must be in the range [0, num).
    *
    * Returns: The item which is at 'pos'.
    * Throws:
    *    INVALID_POSITION if 'pos' is not in the range [0, num).
    */
    itemType &at( int pos ) const;

    //////////////////////////////////////////////////////////////////////
    // overloaded operators

    /*
    *    Copy the array 'rhs' to 'this'. Say,
    *      dynarray var1;
    *      ............
    *      Do somthing with var1.
    *      ............
    *      dynarray var2;
    *      var2 = var1.    -----> This function will be called now.
    *                              'var2' will be 'this'.
    *      ............
    *
    * 'rhs' => The right side object which will be copied to 'this'.
    *
    * Returns: Constant reference to this array.
    * Throws:
    *    OUT_OF_MEMORY if memory is not available.
    */
    const dynarray &operator=( const dynarray & rhs);

    /*
    *    Return the item at the position 'pos'. Say,
    *      dynarray var1;
    *      ............
    *      Do somthing with var1.
    *      ............
    *      itemType a = var1[3]  ---> This function will be called now.
    *                                  'var1' will be 'this'.
    *                                  'pos' will be 3.
    *      ............
    *
    * 'pos' => The position of the requested item.
    *
    * Returns: The reference to the item at position 'pos'.
    * Throws:
    *      INVALID_POSITION if 'pos' is not in the range [0, num).
    */
    itemType & operator[]( int pos ); // non const variant

    /*
    *    Same as above 'operator[]'. But this time, constant reference to the item
    * will be returned.
    *
    * 'pos' => The position of the requested item.
    *
    * Returns: Constant reference to the item at position 'pos'.
    * Throws:
    *      INVALID_POSITION if 'pos' is not in the range [0, num).
    */
    const itemType & operator[]( int pos ) const; // const variant

    /*
    *    Build a new array by concatenating the elements of 'this' and 'rhs'. If 'this'
    * has the elements { 10, 12, 13 } and 'rhs' has the elements { 23, 1, 20 }, then
    * the new array will have { 10, 12, 13, 23, 1, 20 }. Say,
    *
    *      dynarray var1;
    *      ............
    *      Do somthing with var1.
    *      ............
    *      dynarray var2;
    *      ............
    *      Do somthing with var2.
    *      ............
    *      dynarray newVar = var1 + var2  ---> This function will be called now.
    *                                          'var1' will be 'this'.
    *                                          'var2' will be 'rhs'.
    *      ............
    *
    * 'rhs' => The object to the right side of '+' operator.
    *
    * Returns: New array after concatenating 'this' and 'rhs'.
    * Throws:
    *    OUT_OF_MEMORY if memory is not available.
    */
    dynarray operator+( const dynarray &rhs );

    /*
    *    Change 'this' to a new array by concatenating the elements of 'this' and 'rhs'.
    * If 'this' has the elements { 10, 12, 13 } and 'rhs' has the elements { 23, 1, 20 },
    * then 'this' will be now { 10, 12, 13, 23, 1, 20 }. Say,
    *
    *      dynarray var1;
    *      ............
    *      Do somthing with var1.
    *      ............
    *      dynarray var2;
    *      ............
    *      Do somthing with var2.
    *      ............
    *      var1 += var2  ---> This function will be called now.
    *                          'var1' will be 'this'.
    *                          'var2' will be 'rhs'.
    *      ............
    *
    * 'rhs' => The object to the right side of '+=' operator.
    *
    * Returns: Reference to this array after being concatenated by 'rhs'.
    * Throws:
    *    OUT_OF_MEMORY if memory is not available.
    */
    dynarray &operator+=( const dynarray &rhs );

    /*
    *    Check 'this' to 'rhs' for equality. Say,
    *
    *      dynarray var1;
    *      ............
    *      Do somthing with var1.
    *      ............
    *      dynarray var2;
    *      ............
    *      Do somthing with var2.
    *      ............
    *      if (var1 == var2)  ---> This function will be called now.
    *                              'var1' will be 'this'.
    *                              'var2' will be 'rhs'.
    *      ............
    *
    * 'rhs' => The array to the right side of '==' operator.
    *
    * Returns: True if 'this' and 'rhs' are euqal. false otherwise.
    * Throws:  None.
    */
    bool operator==( const dynarray &rhs );

    /*
    *    Check 'this' to 'rhs' for inequality. Say,
    *
    *      dynarray var1;
    *      ............
    *      Do somthing with var1.
    *      ............
    *      dynarray var2;
    *      ............
    *      Do somthing with var2.
    *      ............
    *      if (var1 != var2)  ---> This function will be called now.
    *                              'var1' will be 'this'.
    *                              'var2' will be 'rhs'.
    *      ............
    *
    * 'rhs' => The array to the right side of '!=' operator.
    *
    * Returns: True if 'this' and 'rhs' are ineuqal. false otherwise.
    * Throws:  None.
    */
    bool operator!=( const dynarray &rhs );

    //////////////////////////////////////////////////////////////////////////////
    // optional functions

    /*
    *    Returns the last element in the array.
    *
    * Returns: Last element in the array.
    * Throws:
    *      BACK_NOT_POSSIBLE if there is no data in the array.
    */
    itemType &back( ); // optional

    /*
    *    Returns the 1st element in the array.
    *
    * Returns: 1st element in the array.
    * Throws:
    *      FRONT_NOT_POSSIBLE if there is no data in the array.
    */
    itemType &front( ); // optional

    /*
    *    Returns the capacity of the array.
    *
    * Returns: Capacity of the array.
    * Throws:  None
    */
    int capacity( ) const; // optional

    /*
    *    Check whether 'item' exists in the array or not.
    *
    * 'item' => The item whose existence is being traced.
    *
    * Returns: true if 'item' exists in the array. false otherwise.
    * Throws:  None
    */
    int contains( const itemType &item); // optional

    /*
    *    Resize the array. This may increase or decrease the array. If the size is
    * increased, then extra space will be filled with 'fill'. If the size is
    * decreased, then space will be truncated. This time data may be lost.
    *
    * 'newSize' => The new size of the array.
    * 'fill'    => The data with which extra space will be filled.
    *
    * Returns: Nothing.
    * Throws:
    *      OUT_OF_MEMORY if memory is not available.
    */
    void resize( int newSize, itemType fill = NULL ); // optional

    /*
    *    Check 'this' to 'rhs'. Say,
    *
    *      dynarray var1;
    *      ............
    *      Do somthing with var1.
    *      ............
    *      dynarray var2;
    *      ............
    *      Do somthing with var2.
    *      ............
    *      if (var1 < var2)  ---> This function will be called now.
    *                              'var1' will be 'this'.
    *                              'var2' will be 'rhs'.
    *      ............
    *
    * 'rhs' => The object to the right side of '<' operator.
    *
    * Returns: True if 'this' is less than 'rhs'. false otherwise.
    * Throws:  None.
    */
    bool operator< ( const dynarray &rhs ); // optional

    /*
    *    Check 'this' to 'rhs'. Say,
    *
    *      dynarray var1;
    *      ............
    *      Do somthing with var1.
    *      ............
    *      dynarray var2;
    *      ............
    *      Do somthing with var2.
    *      ............
    *      if (var1 <= var2)  ---> This function will be called now.
    *                              'var1' will be 'this'.
    *                              'var2' will be 'rhs'.
    *      ............
    *
    * 'rhs' => The object to the right side of '<=' operator.
    *
    * Returns: True if 'this' is less than or equal to 'rhs'. false otherwise.
    * Throws:  None.
    */
    bool operator<=( const dynarray &rhs ); // optional

    /*
    *    Check 'this' to 'rhs'. Say,
    *
    *      dynarray var1;
    *      ............
    *      Do somthing with var1.
    *      ............
    *      dynarray var2;
    *      ............
    *      Do somthing with var2.
    *      ............
    *      if (var1 > var2)  ---> This function will be called now.
    *                              'var1' will be 'this'.
    *                              'var2' will be 'rhs'.
    *      ............
    *
    * 'rhs' => The object to the right side of '>' operator.
    *
    * Returns: True if 'this' is greater than 'rhs'. false otherwise.
    * Throws:  None.
    */
    bool operator> ( const dynarray &rhs ); // optional

    /*
    *    Check 'this' to 'rhs'. Say,
    *
    *      dynarray var1;
    *      ............
    *      Do somthing with var1.
    *      ............
    *      dynarray var2;
    *      ............
    *      Do somthing with var2.
    *      ............
    *      if (var1 >= var2)  ---> This function will be called now.
    *                              'var1' will be 'this'.
    *                              'var2' will be 'rhs'.
    *      ............
    *
    * 'rhs' => The object to the right side of '>=' operator.
    *
    * Returns: True if 'this' is greater than or equal to 'rhs'. false otherwise.
    * Throws:  None.
    */
    bool operator>=( const dynarray &rhs ); // optional

private:

    //////////////////////////////////////////////////////////////////////////
    // private functions

    /*
    *    Copy the array 'rhs' to 'this'.
    *
    * 'rhs' => The array which will copied to 'this'.
    *
    * Returns: Nothing.
    * Throws:
    *    OUT_OF_MEMORY if memory is not available.
    */
    void copy_from( const dynarray &rhs );

    /*
    *    Allocate 'n' amount of spaces.
    *
    * 'n' => The number of space which will be allocated.
    *
    * Returns: The pointer to the allocated array of 'n' items.
    * Throws:
    *    OUT_OF_MEMORY if memory is not available.
    */
    itemType* try_allocate_space( int n );

    /*
    *    Free all the allocated spaces for this array.
    *
    * Returns: Nothing.
    * Throws:  None.
    */
    void try_deallocate_space();
   
    //Data Members.
    itemType *ptr; // pointer to the first element of the array
    int cap; // capacity of the array
    int num; // size of the array
    int inc; //When an array is exhausted, 'inc' amount of space will be allocated.
};
Avatar billede bertelbrander Novice
05. marts 2007 - 21:21 #1
Det er:
#include <iostream>

Og der mangler:
using namespace std;
Avatar billede d_dogg Nybegynder
05. marts 2007 - 21:41 #2
1>c:\documents and settings\makhdoom\my documents\visual studio 2005\projects\dynarray\dynarray\dynarray.cpp(19) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\documents and settings\makhdoom\my documents\visual studio 2005\projects\dynarray\dynarray\dynarray.cpp(19) : error C2761: '{ctor}' : member function redeclaration not allowed
1>c:\documents and settings\makhdoom\my documents\visual studio 2005\projects\dynarray\dynarray\dynarray.cpp(20) : error C2447: '{' : missing function header (old-style formal list?)
1>c:\documents and settings\makhdoom\my documents\visual studio 2005\projects\dynarray\dynarray\dynarray.cpp(289) : error C2447: '{' : missing function header (old-style formal list?)
1>main.cpp
1>c:\documents and settings\makhdoom\my documents\visual studio 2005\projects\dynarray\dynarray\main.cpp(4) : error C2144: syntax error : 'int' should be preceded by ';'
1>c:\documents and settings\makhdoom\my documents\visual studio 2005\projects\dynarray\dynarray\main.cpp(4) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>Generating Code...
1>Build log was saved at "file://c:\Documents and Settings\Makhdoom\My Documents\Visual Studio 2005\Projects\Ass2_dynarray\Ass2_dynarray\Debug\BuildLog.htm"
1>Ass2_dynarray - 7 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Avatar billede d_dogg Nybegynder
05. marts 2007 - 21:42 #3
her er koden for ovenbeskrevne fejl


/Define some string values.

#define OUT_OF_MEMORY "Out of memory."
#define INVALID_SIZE  "Size can't be negative."
#define INVALID_INCREMENT  "Increment can't be 0 or negative."
#define INVALID_POSITION  "Invalid index is specified."
#define POP_NOT_POSSIBLE  "No data exists in the array. Pop operation is not possible."
#define BACK_NOT_POSSIBLE  "No data exists in the array. Back operation not possible."
#define FRONT_NOT_POSSIBLE "No data exists in the array. Front operation is not possible."


//////////////////////////////////////////////////////////
//Constructor and destructor.

// Default size is 0. Default increment is 5.
dynarray::dynarray( int s /* = 0*/, int inc /* = 5*/)
{
    //Check for a valid size.
    if (s < 0)
    {
        throw INVALID_SIZE;
    }

    //Check for a valid increment.
    if (inc <= 0)
    {
        throw INVALID_INCREMENT;
    }

    //Initialize the size, capacity and the increment.
    this->num = s;
    this->cap = s;
    this->inc = inc;

    //Try to allocate sapces upto 'cap' number of item.
    this->ptr = this->try_allocate_space(this->cap);
}

dynarray::dynarray( const dynarray &rhs )
{
    //Just copy the data from 'rhs' to 'this'.
    this->copy_from(rhs);
}

//Destructor.
dynarray::~dynarray()
{
    //Free the allocated spaces.
    this->try_deallocate_space();
}
Avatar billede bertelbrander Novice
05. marts 2007 - 22:36 #4
Jeg kan ikke lige se fejlen, hvad står der i dynarray.cpp linje 19?
Avatar billede d_dogg Nybegynder
05. marts 2007 - 22:39 #5
// Default size is 0. Default increment is 5.
dynarray::dynarray( int s /* = 0*/, int inc /* = 5*/)
{
Avatar billede bertelbrander Novice
05. marts 2007 - 22:41 #6
Har du inkluderet headeren med dynarray class'en i dynarray.cpp?
Avatar billede d_dogg Nybegynder
05. marts 2007 - 22:49 #7
ja..headeren er inkluderet i class'en
Avatar billede bertelbrander Novice
05. marts 2007 - 22:57 #8
Spørgsmålet var om headeren er inkluderet i .cpp filen.

(Man kan ikke inkludere en header i en class...)
Avatar billede bertelbrander Novice
05. marts 2007 - 22:57 #9
Prøv at poste den komplette kode.
Avatar billede d_dogg Nybegynder
05. marts 2007 - 23:05 #10
det er clas filen og ovenfor kan du header filen


#include "dynarray.h"

//////////////////////////////////////////////////////////
//Define some string values.

#define OUT_OF_MEMORY "Out of memory."
#define INVALID_SIZE  "Size can't be negative."
#define INVALID_INCREMENT  "Increment can't be 0 or negative."
#define INVALID_POSITION  "Invalid index is specified."
#define POP_NOT_POSSIBLE  "No data exists in the array. Pop operation is not possible."
#define BACK_NOT_POSSIBLE  "No data exists in the array. Back operation not possible."
#define FRONT_NOT_POSSIBLE "No data exists in the array. Front operation is not possible."


//////////////////////////////////////////////////////////
//Constructor and destructor.

// Default size is 0. Default increment is 5.
dynarray::dynarray( int s /* = 0*/, int inc /* = 5*/)
{
    //Check for a valid size.
    if (s < 0)
    {
        throw INVALID_SIZE;
    }

    //Check for a valid increment.
    if (inc <= 0)
    {
        throw INVALID_INCREMENT;
    }

    //Initialize the size, capacity and the increment.
    this->num = s;
    this->cap = s;
    this->inc = inc;

    //Try to allocate sapces upto 'cap' number of item.
    this->ptr = this->try_allocate_space(this->cap);
}

dynarray::dynarray( const dynarray &rhs )
{
    //Just copy the data from 'rhs' to 'this'.
    this->copy_from(rhs);
}

//Destructor.
dynarray::~dynarray()
{
    //Free the allocated spaces.
    this->try_deallocate_space();
}


//////////////////////////////////////////////////////////

void dynarray::push_back( itemType item )
{
    //Check whether the array has space to keep 'item' or not.
    if (this->num == this->cap)
    {
        //No space available. Array expansion is needed.
        this->resize(this->cap + this->inc);
    }

    //Add the item. Increase the 'num' also.
    this->ptr[this->num++] = item;
}

itemType dynarray::pop_back( )
{
    //Check whether any data exists or not.
    if (this->num == 0)
    {
        throw POP_NOT_POSSIBLE;
    }

    //Save the deleting item. It will be returned later.
    itemType temp = this->ptr[--this->num];

    //We are keeping 2 * inc amount of free space. Whenever we are going below
    //this, we will decrease 'inc' amount of spaces. That is we are ensuring
    //that at least 'inc' amout of extra spaces are available.
    int extra_space = (2 * inc) - (this->cap - this->num);
    if (extra_space < 0)
    {
        //Decrease the extra spaces. It will occur when this function is called
        //more times.
        this->resize(this->cap - this->inc);
    }

    return(temp);
}

/*
* 1) If the position is the last, then just call 'push_back()'.
* 3) 'push_back()' the last item to the right space. This is done for memory allocating.
* 2) Right shift all the items from 'pos' to last.
*/
void dynarray::insert( itemType item, int pos )
{
    //Check for position validity.
    if (pos < 0 || pos > this->num)
    {
        throw INVALID_POSITION;
    }

    //Insert at the last position.
    if (pos == this->num)
    {
        this->push_back(item);
    }

    //Insert in a position.
    else
    {
        //1st, push the last item. The advantage to call this function is that
        //this function will enlarge the array if needed.
        this->push_back(this->ptr[this->num - 1]);

        //2nd, push the other items to the right.
        int i;
        for(i = this->num - 3; i >= pos; i--)
        {
            //Shift each element to the right side.
            this->ptr[i + 1] = this->ptr[i];
        }

        //Keep the item in its dedicated position.
        this->ptr[pos] = item;
    }
}

/*
* 1) Save the removing element.
* 2) Left shift all the items from 'pos' to last.
* 3) Pop the last item. It will sone simply by calling 'pop_back()'.
*/
itemType dynarray::erase( int pos )
{
    //Check for position validity.
    if (pos < 0 || pos >= this->num)
    {
        throw INVALID_POSITION;
    }

    //1st, Save the deleting item to return later.
    itemType temp = this->ptr[pos];

    //2nd, push all the items to the left.
    int i;
    for(i = pos + 1; i < this->num; i++)
    {
        //Shift each element to the left side.
        this->ptr[i - 1] = this->ptr[i];
    }

    //There exists some item. Now we can pop.
    if (this->num != 0)
    {
        //3rd, pop the last item. The advantage to call this function is that
        //this function will decrease the array if needed.
        this->pop_back();
    }

    return(temp);
}

void dynarray::clear( )
{
    //Free all the spaces.
    this->try_deallocate_space();
}

int dynarray::size( ) const
{
    //Return the size.
    return(this->num);
}

bool dynarray::empty( ) const
{
    //Check whether there is any data or not.
    return(this->num == 0);
}

itemType& dynarray::at( int pos ) const
{
    //Check for position validity.
    if (pos < 0 || pos >= this->num)
    {
        throw INVALID_POSITION;
    }

    //Return the item.
    return(this->ptr[pos]);
}


//////////////////////////////////////////////////////////
// overloaded operators

const dynarray& dynarray::operator=( const dynarray & rhs)
{
    //Just call 'copy_from()' to copy 'rhs' to 'this'.
    this->copy_from(rhs);

    //Return a reference to this object.
    return(*this);
}

itemType& dynarray::operator[]( int pos ) // non const variant
{
    //Just call 'at()'.
    return(this->at(pos));
}

const itemType& dynarray::operator[]( int pos ) const // const variant
{
    //Just call 'at()'. But now a constant reference will be returned.
    return(this->at(pos));
}

dynarray dynarray::operator+( const dynarray &rhs )
{
    int i, j = 0;

    //Create temporary spaces.
    dynarray temp(this->num + rhs.num);
   
    //Copy 'this' array to the temporary space.
    for(i = 0; i < this->num; i++)
    {
        temp[j++] = this->ptr[i];
    }

    //Copy 'rhs' array to the temporary space.
    for(i = 0; i < rhs.num; i++)
    {
        temp[j++] = rhs.ptr[i];
    }

    return(temp);
}

dynarray& dynarray::operator+=( const dynarray &rhs )
{
    //It will call the overloaded function of "=".
    dynarray temp = (*this) + rhs;

    //Copy the data from temporary space to this variable.
    this->copy_from(temp);

    //Return a reference to this object.
    return(*this);
}

bool dynarray::operator==( const dynarray &rhs )
{
    //Number of data are not equal.
    if (this->num != rhs.num)
    {
        return(false);
    }

    //Check the data for equality.
    int i;
    for(i = 0; i < this->num; i++)
    {
        //2 items mismatched.
        if (this->ptr[i] != rhs.ptr[i])
        {
            return(false);
        }
    }

    //Equal.
    return(true);
}

bool dynarray::operator!=( const dynarray &rhs )
{
    //It will call the overloaded function of "==".
    return(!((*this) == rhs));
}

ostream &operator<<( ostream &str, const dynarray &rhs );
{
    str << "Number of element: " << rhs.num << endl;

    int i;
    for(i = 0; i < rhs.num; i++)
    {
        if (i == 0)
        {
            str << rhs.ptr[i];
        }
        else
        {
            str << ", " << rhs.ptr[i];
        }
    }

    str << endl;

    return(str);
}


//////////////////////////////////////////////////////////
// optional functions

itemType& dynarray::back( )
{
    //Check whether the array is empty or not.
    if (this->num == 0)
    {
        throw BACK_NOT_POSSIBLE;
    }

    //Return the last item.
    return(this->ptr[this->num - 1]);
}

itemType& dynarray::front( )
{
    //Check whether the array is empty or not.
    if (this->num == 0)
    {
        throw FRONT_NOT_POSSIBLE;
    }

    //Return the 1st item.
    return(this->ptr[0]);
}

int dynarray::capacity( ) const
{
    //Return the capacity.
    return(this->cap);
}

int dynarray::contains( const itemType &item)
{
    int i;

    //Linear Search 'item' in the array.
    for(i = 0; i < this->num; i++)
    {
        //Yes, a match is found.
        if (item == this->ptr[i])
        {
            return(i);
        }
    }

    //No match is found. Return a negative.
    return(-1);
}

void dynarray::resize( int newSize, itemType fill /* = NULL*/)
{
    //OUT_OF_MEMORY will be thrown from 'try_allocate_space()' on memory hunger.
    itemType *temp = try_allocate_space(newSize);
   
    //Copy the data.
    int i = 0;
    while(i < this->num && i < newSize)
    {
        temp[i++] = this->ptr[i];
    }
   
    //'try_deallocate_space()' will clear 'num'. So, save it 1st.
    int dataNum = i;

    //Fill extra space if exists.
    while(i < newSize)
    {
        temp[i++] = fill;
    }

    //Free the previous spaces.
    this->try_deallocate_space();

    //Assign the new space.
    this->ptr = temp;
    this->num = dataNum;
    this->cap = newSize;
}

bool dynarray::operator< ( const dynarray &rhs )
{
    if (this->num > rhs.num)
    {
        return(false);
    }

    //Check the data for LessThan comparison.
    int i;
    for(i = 0; i < this->num; i++)
    {
        if (this->ptr[i] < rhs.ptr[i])
        {
            return(true);
        }
    }

    //Greater than.
    return(false);
}

bool dynarray::operator<=( const dynarray &rhs )
{
    return(!((*this) > rhs));
}

bool dynarray::operator> ( const dynarray &rhs )
{
    if (this->num < rhs.num)
    {
        return(false);
    }

    //Check the data for LessThan comparison.
    int i;
    for(i = 0; i < this->num; i++)
    {
        if (this->ptr[i] > rhs.ptr[i])
        {
            return(true);
        }
    }

    //Less than.
    return(false);
}

bool dynarray::operator>=( const dynarray &rhs )
{
    return(!((*this) < rhs));
}

////////////////////////////////////////////////////////////////////////
//Private functions.

void dynarray::copy_from(const dynarray &src)
{
    this->num = src.num;
    this->inc = src.inc;
    this->cap = src.cap;

    //First free the previously allocated spaces.
    try_deallocate_space();

    //Try to allocate sapces upto 'cap' number of item.
    this->ptr = try_allocate_space(this->cap);

    //Copy the data from 'src' to 'this'.
    int i;
    for(i = 0; i < src.num; i++)
    {
        this->ptr[i] = src.ptr[i];
    }
}

void dynarray::try_deallocate_space()
{
    //Check whether really there is any data in the array.
    if (this->ptr != NULL)
    {
        delete [] this->ptr;
    }

    //No item and no capacity.
    this->num = 0;
    this->cap = 0;
    this->ptr = new itemType[0];
}

itemType* dynarray::try_allocate_space( int n )
{
    itemType *temp;

    //Allocate new spaces.
    temp = new itemType[n];
   
    //Oops. Memory is not available.
    if (temp == NULL)
    {
        throw OUT_OF_MEMORY;
    }

    return(temp);
}
Avatar billede bertelbrander Novice
05. marts 2007 - 23:18 #11
Jeg kan ikke se andre fejl end et ; for meget i denne linje:
ostream &operator<<( ostream &str, const dynarray &rhs );

(dynarray.cpp ca. linje 288)
Avatar billede d_dogg Nybegynder
05. marts 2007 - 23:26 #12
hvis jeg fjerner denne linie...så kal jeg jo slette..og det ødekægger programmet
Avatar billede bertelbrander Novice
05. marts 2007 - 23:33 #13
Du skal kun slette ;
Ikke hele linien.
Avatar billede d_dogg Nybegynder
05. marts 2007 - 23:41 #14
jeg har prøvet at udkommentere linien , men får stadig fejl...jeg ved snarkikke havd der skyldes at jeg får fejl..
Avatar billede jpk Nybegynder
06. marts 2007 - 08:05 #15
Som bertelbrander skriver, du skal KUN fjerne ';' sidst i linien.
Avatar billede d_dogg Nybegynder
06. marts 2007 - 11:30 #16
har fjernet ';'...men der er stadig fejl..


1>------ Build started: Project: Ass2_dynarray, Configuration: Debug Win32 ------
1>Compiling...
1>dynarray.cpp
1>c:\documents and settings\makhdoom\my documents\visual studio 2005\projects\dynarray\dynarray\dynarray.cpp(19) : error C2143: syntax error : missing ';' before 'dynarray::{ctor}'
1>c:\documents and settings\makhdoom\my documents\visual studio 2005\projects\dynarray\dynarray\dynarray.cpp(19) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>main.cpp
1>c:\documents and settings\makhdoom\my documents\visual studio 2005\projects\dynarray\dynarray\main.cpp(4) : error C2144: syntax error : 'int' should be preceded by ';'
1>c:\documents and settings\makhdoom\my documents\visual studio 2005\projects\dynarray\dynarray\main.cpp(4) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>Generating Code...
1>Build log was saved at "file://c:\Documents and Settings\Makhdoom\My Documents\Visual Studio 2005\Projects\Ass2_dynarray\Ass2_dynarray\Debug\BuildLog.htm"
1>Ass2_dynarray - 4 error(s), 0 warning(s)
Avatar billede d_dogg Nybegynder
06. marts 2007 - 15:04 #17
jeg har fundet fejlen
Avatar billede jpk Nybegynder
09. marts 2007 - 14:43 #18
Hvad var det..?
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester