The Sequence Class ( Data Structures)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Anan18
    New Member
    • Oct 2006
    • 11

    The Sequence Class ( Data Structures)

    Hello sir,
    I'm supposed to Implement and Test the sequence Class Using a Fixed-Sized Array (Chapter 3), from Data Structures & Other objects using c++. The header file is provided, and so is a test program to check the correctness of the sequence class. I cannot figure out what the problem is and keep looking back and forth at the textbook and its not helping, can someone please please help me , i cannot figure out the errors in the implementation file.

    [code=cpp]
    // This is the Header File sequence.h
    #define MAIN_SAVITCH_SE QUENCE_H
    #include <iostream>
    #include <cstdlib> // Provides size_t

    namespace main_savitch_3
    {
    class sequence
    {
    public:
    // TYPEDEFS and MEMBER CONSTANTS
    typedef double value_type;
    typedef size_t size_type;
    static const size_type CAPACITY = 30;
    // CONSTRUCTOR
    sequence( );
    // MODIFICATION MEMBER FUNCTIONS
    void start( );
    void advance( );
    void insert(const value_type& entry);
    void attach(const value_type& entry);
    void remove_current( );
    // CONSTANT MEMBER FUNCTIONS
    size_type size( ) const;
    bool is_item( ) const;
    value_type current( ) const;
    private:
    value_type data[CAPACITY];
    size_type used;
    size_type current_index;
    };
    }

    #endif


    // This is the implementation file which i have having problems with
    //i cant figure out what is wrong help please please



    #include <iostream>
    #include <cassert> // Provides assert function
    #include "sequence.h " // With value_type defined as double
    using namespace std;

    namespace main_savitch_3
    {
    // MODIFICATION MEMBER FUNCTIONS
    sequence::seque nce ()
    {
    current_index = 0;

    used = 0;

    }

    void sequence::start ( )
    {
    current_index = 0;

    }

    void sequence::advan ce( )
    {
    assert (is_item());
    current_index++ ;

    }

    void sequence::inser t(const value_type& entry)
    {
    int i;
    assert (is_item());
    const int old_used = used;
    for (i = used; i current_index; i--)
    {
    data[i]= data[i-1];
    data[current_index] = entry;
    used++;
    }
    assert( used == old_used - 1 );
    }

    File Edit Options Buffers Tools C++ Help
    void sequence::attac h(const value_type& entry)
    {
    int i;
    assert(size()<C APACITY);
    const int old_used = used;
    for (i = used; i current_index; i--)
    {
    data[i] = data[i+1];
    data[current_index] = entry;
    used++;
    }
    assert( used == old_used - 1 );
    }

    void sequence::remov e_current( )
    {
    int i;
    assert (is_item());
    const int old_used = used; // added line here
    for (i= current_index +1; i < used -1; i++)
    {
    data[i] = data[i+1];
    used--;
    }
    assert( used == old_used - 1 ); // added line here
    }


    // CONSTANT MEMBER FUNCTIONS
    sequence::size_ type sequence::size( ) const
    {
    return used;

    }

    bool sequence::is_it em( ) const
    {

    return current_index < used;
    }

    sequence::value _type sequence::curre nt( ) const
    {

    return data[current_index];
    }
    }
    [/code]

    Im getting all sorts of warnings or c++ does not support default int, but i followed examples from the book please help meee =(
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Originally posted by Anan18
    Im getting all sorts of warnings or c++ does not support default int, but i followed examples from the book please help meee =(
    If you are getting errors and warnings then you need to address them, as a tip it is not unusual for a low number of actual errors to produce a large number of warnings, (1 error for 3 pages of warning is my record I think) so examine the first one first.

    If you need help with a specific warning or error or set of them then you are going to have to post them otherwise we wont know what they are.

    Comment

    • Anan18
      New Member
      • Oct 2006
      • 11

      #3
      1>Compiling...
      1>sequence9.cx x
      1>c:\users\anan \documents\visu al studio 2005\projects\a ssignment2\assi gnment2\sequenc e9.cxx(34) : warning C4267: 'initializing' : conversion from 'size_t' to 'const int', possible loss of data

      1>c:\users\anan \documents\visu al studio 2005\projects\a ssignment2\assi gnment2\sequenc e9.cxx(35) : warning C4267: '=' : conversion from 'size_t' to 'int', possible loss of data

      1>c:\users\anan \documents\visu al studio 2005\projects\a ssignment2\assi gnment2\sequenc e9.cxx(35) : error C2146: syntax error : missing ';' before identifier 'current_index'

      1>c:\users\anan \documents\visu al studio 2005\projects\a ssignment2\assi gnment2\sequenc e9.cxx(35) : error C2146: syntax error : missing ')' before identifier 'i'

      1>c:\users\anan \documents\visu al studio 2005\projects\a ssignment2\assi gnment2\sequenc e9.cxx(35) : error C2059: syntax error : ';'

      1>c:\users\anan \documents\visu al studio 2005\projects\a ssignment2\assi gnment2\sequenc e9.cxx(35) : error C2059: syntax error : ')'

      1>c:\users\anan \documents\visu al studio 2005\projects\a ssignment2\assi gnment2\sequenc e9.cxx(36) : error C2143: syntax error : missing ';' before '{'

      1>c:\users\anan \documents\visu al studio 2005\projects\a ssignment2\assi gnment2\sequenc e9.cxx(44) : error C2146: syntax error : missing ';' before identifier 'Edit'

      1>c:\users\mrah il\documents\vi sual studio 2005\projects\a ssignment2\assi gnment2\sequenc e9.cxx(44) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

      1>c:\users\anan \documents\visu al studio 2005\projects\a ssignment2\assi gnment2\sequenc e9.cxx(44) : error C2146: syntax error : missing ';' before identifier 'Options'

      1>c:\users\anan \documents\visu al studio 2005\projects\a ssignment2\assi gnment2\sequenc e9.cxx(44) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

      1>c:\users\anan \documents\visu al studio 2005\projects\a ssignment2\assi gnment2\sequenc e9.cxx(44) : error C2146: syntax error : missing ';' before identifier 'Buffers'

      1>c:\users\anan \documents\visu al studio 2005\projects\a ssignment2\assi gnment2\sequenc e9.cxx(44) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

      1>c:\users\anan \documents\visu al studio 2005\projects\a ssignment2\assi gnment2\sequenc e9.cxx(44) : error C2146: syntax error : missing ';' before identifier 'Tools'

      1>c:\users\anan \documents\visu al studio 2005\projects\a ssignment2\assi gnment2\sequenc e9.cxx(44) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

      1>c:\users\anan \documents\visu al studio 2005\projects\a ssignment2\assi gnment2\sequenc e9.cxx(44) : error C2146: syntax error : missing ';' before identifier 'C'

      1>c:\users\anan \documents\visu al studio 2005\projects\a ssignment2\assi gnment2\sequenc e9.cxx(44) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

      1>c:\users\anan \documents\visu al studio 2005\projects\a ssignment2\assi gnment2\sequenc e9.cxx(44) : error C2143: syntax error : missing ';' before '++'

      1>c:\users\anan \documents\visu al studio 2005\projects\a ssignment2\assi gnment2\sequenc e9.cxx(44) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

      1>c:\users\anan \documents\visu al studio 2005\projects\a ssignment2\assi gnment2\sequenc e9.cxx(46) : error C2143: syntax error : missing ';' before '{'

      1>c:\users\anan \documents\visu al studio 2005\projects\a ssignment2\assi gnment2\sequenc e9.cxx(46) : error C2447: '{' : missing function header (old-style formal list?)

      1>c:\users\anan \documents\visu al studio 2005\projects\a ssignment2\assi gnment2\sequenc e9.cxx(63) : warning C4267: 'initializing' : conversion from 'size_t' to 'const int', possible loss of data

      1>c:\users\anan \documents\visu al studio 2005\projects\a ssignment2\assi gnment2\sequenc e9.cxx(64) : warning C4267: '=' : conversion from 'size_t' to 'int', possible loss of data

      1>c:\users\anan \documents\visu al studio 2005\projects\a ssignment2\assi gnment2\sequenc e9.cxx(64) : warning C4018: '<' : signed/unsigned mismatch
      1>Build log was saved at "file://c:\Users\Anan\D ocuments\Visual Studio 2005\Projects\A ssignment2\Assi gnment2\Debug\B uildLog.htm"
      1>Assignment2 - 19 error(s), 5 warning(s)
      ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

      Comment

      • Anan18
        New Member
        • Oct 2006
        • 11

        #4
        and when i tried to fix some of those errors, it gave me more errors, i dunno.. please help me sir

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          Originally posted by Anan18
          and when i tried to fix some of those errors, it gave me more errors, i dunno.. please help me sir
          That's not uncommon in code with errors, 1 error hides another one so you don't get the compile error for the second error until you have fixed the first.

          Fixing errors is an iterative process,
          1. Compile code
          2. If there are no errors or warnings stop
          3. Fix some or all of the errors and warnings
          4. goto 1


          Concentrate on these 3 warnings and errors to start with

          Code:
          1>Compiling...
          1>sequence9.cxx
          1>c:\users\anan\documents\visual studio 2005\projects\assignment2\assignment2\sequence9.cxx(34) : warning C4267: 'initializing' : conversion from 'size_t' to 'const int', possible loss of data
          
          1>c:\users\anan\documents\visual studio 2005\projects\assignment2\assignment2\sequence9.cxx(35) : warning C4267: '=' : conversion from 'size_t' to 'int', possible loss of data
          
          1>c:\users\anan\documents\visual studio 2005\projects\assignment2\assignment2\sequence9.cxx(35) : error C2146: syntax error : missing ';' before identifier 'current_index'
          The first 2 are warnings because the type of member variable used is size_t but you are assign it to an int. The type of size_t is platform dependent but I guess it may be unsigned long on your platform (or long if you have 16 bit ints).

          The third message, the error message is an actual mistake in you code, look at the line it references and see if you can spot any errors in it, you are missing an operator.

          These errors are probably just by products of the error you just fixed
          Code:
          1>c:\users\anan\documents\visual studio 2005\projects\assignment2\assignment2\sequence9.cx x(35) : error C2146: syntax error : missing ')' before identifier 'i'
          
          1>c:\users\anan\documents\visual studio 2005\projects\assignment2\assignment2\sequence9.cx x(35) : error C2059: syntax error : ';'
          
          1>c:\users\anan\documents\visual studio 2005\projects\assignment2\assignment2\sequence9.cx x(35) : error C2059: syntax error : ')'
          
          1>c:\users\anan\documents\visual studio 2005\projects\assignment2\assignment2\sequence9.cx x(36) : error C2143: syntax error : missing ';' before '{'
          This is the next real error
          Code:
          1>c:\users\anan\documents\visual studio 2005\projects\assignment2\assignment2\sequence9.cx x(44) : error C2146: syntax error : missing ';' before identifier 'Edit'
          Go to the line(44) of code see if you can see something wrong with the syntax

          This error then produces these by products

          Code:
          1>c:\users\mrahil\documents\visual studio 2005\projects\assignment2\assignment2\sequence9.cx x(44) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
          
          1>c:\users\anan\documents\visual studio 2005\projects\assignment2\assignment2\sequence9.cx x(44) : error C2146: syntax error : missing ';' before identifier 'Options'
          
          1>c:\users\anan\documents\visual studio 2005\projects\assignment2\assignment2\sequence9.cx x(44) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
          
          1>c:\users\anan\documents\visual studio 2005\projects\assignment2\assignment2\sequence9.cx x(44) : error C2146: syntax error : missing ';' before identifier 'Buffers'
          
          1>c:\users\anan\documents\visual studio 2005\projects\assignment2\assignment2\sequence9.cx x(44) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
          
          1>c:\users\anan\documents\visual studio 2005\projects\assignment2\assignment2\sequence9.cx x(44) : error C2146: syntax error : missing ';' before identifier 'Tools'
          
          1>c:\users\anan\documents\visual studio 2005\projects\assignment2\assignment2\sequence9.cx x(44) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
          
          1>c:\users\anan\documents\visual studio 2005\projects\assignment2\assignment2\sequence9.cx x(44) : error C2146: syntax error : missing ';' before identifier 'C'
          
          1>c:\users\anan\documents\visual studio 2005\projects\assignment2\assignment2\sequence9.cx x(44) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
          
          1>c:\users\anan\documents\visual studio 2005\projects\assignment2\assignment2\sequence9.cx x(44) : error C2143: syntax error : missing ';' before '++'
          
          1>c:\users\anan\documents\visual studio 2005\projects\assignment2\assignment2\sequence9.cx x(44) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
          
          1>c:\users\anan\documents\visual studio 2005\projects\assignment2\assignment2\sequence9.cx x(46) : error C2143: syntax error : missing ';' before '{'
          
          1>c:\users\anan\documents\visual studio 2005\projects\assignment2\assignment2\sequence9.cx x(46) : error C2447: '{' : missing function header (old-style formal list?)
          Once you have fixed 3 - 5 errors it isprobably time to compile again and see what you get

          The final three warnings

          Code:
          1>c:\users\anan\documents\visual studio 2005\projects\assignment2\assignment2\sequence9.cx x(63) : warning C4267: 'initializing' : conversion from 'size_t' to 'const int', possible loss of data
          
          1>c:\users\anan\documents\visual studio 2005\projects\assignment2\assignment2\sequence9.cx x(64) : warning C4267: '=' : conversion from 'size_t' to 'int', possible loss of data
          
          1>c:\users\anan\documents\visual studio 2005\projects\assignment2\assignment2\sequence9.cx x(64) : warning C4018: '<' : signed/unsigned mismatch
          1>Build log was saved at "file://c:\Users\Anan\Documents\Visual Studio 2005\Projects\Assignment2\Assignment2\Debug\BuildL og.htm"
          1>Assignment2 - 19 error(s), 5 warning(s)
          are merely warning of your mix use of non-matching integer types. (i.e. assign a 32 bit int to a 16 bit int or comparing a sign and an unsigned integer)

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Originally posted by Anan18
            1>c:\users\anan \documents\visu al studio 2005\projects\a ssignment2\assi gnment2\sequenc e9.cx x(34) : warning C4267: 'initializing' : conversion from 'size_t' to 'const int', possible loss of data

            1>c:\users\anan \documents\visu al studio 2005\projects\a ssignment2\assi gnment2\sequenc e9.cx x(35) : warning C4267: '=' : conversion from 'size_t' to 'int', possible loss of data
            These go away when you change your project properties to not report 64-bit portability issues. Visual Studio is concerned your size_t may be 64-bit (from Unix) but the 64-bit Windows size_t is still 32 bit and the int is till 32-bit.

            size_t is deprecated ion C++. You should not be using it or any of the _t tyoes from C, like time_t, etc.

            As with all warnings and errors. Fix the first one and rebuild. By the time you get ot the third or fourth error, the compiler is so confused the reported error is probably meaningless.

            Comment

            Working...