By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,572 Members | 1,225 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,572 IT Pros & Developers. It's quick & easy.

The Sequence Class ( Data Structures)

P: 11
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.

Expand|Select|Wrap|Line Numbers
  1. // This is the Header File  sequence.h
  2. #define MAIN_SAVITCH_SEQUENCE_H
  3. #include <iostream>
  4. #include <cstdlib> // Provides size_t
  5.  
  6. namespace main_savitch_3
  7. {
  8.     class sequence
  9.     {
  10.     public:
  11.         // TYPEDEFS and MEMBER CONSTANTS
  12.         typedef double value_type;
  13.         typedef size_t size_type;
  14.         static const size_type CAPACITY = 30;
  15.         // CONSTRUCTOR
  16.         sequence( );
  17.         // MODIFICATION MEMBER FUNCTIONS
  18.         void start( );
  19.         void advance( );
  20.         void insert(const value_type& entry);
  21.         void attach(const value_type& entry);
  22.         void remove_current( );
  23.         // CONSTANT MEMBER FUNCTIONS
  24.         size_type size( ) const;
  25.         bool is_item( ) const;
  26.         value_type current( ) const;
  27.     private:
  28.         value_type data[CAPACITY];
  29.         size_type used;
  30.         size_type current_index;
  31.     };
  32. }
  33.  
  34. #endif
  35.  
  36.  
  37. // This is the implementation file which i have having problems with
  38. //i cant figure out what is wrong help please please
  39.  
  40.  
  41.  
  42. #include <iostream>
  43. #include <cassert> // Provides assert function
  44. #include "sequence.h" // With value_type defined as double
  45. using namespace std;
  46.  
  47. namespace main_savitch_3
  48. {
  49. // MODIFICATION MEMBER FUNCTIONS
  50. sequence::sequence ()
  51. {
  52. current_index = 0;
  53.  
  54. used = 0;
  55.  
  56. }
  57.  
  58. void sequence::start( )
  59. {
  60. current_index = 0;
  61.  
  62. }
  63.  
  64. void sequence::advance( )
  65. {
  66. assert (is_item());
  67. current_index++;
  68.  
  69. }
  70.  
  71. void sequence::insert(const value_type& entry)
  72. {
  73. int i;
  74. assert (is_item());
  75. const int old_used = used;
  76. for (i = used; i current_index; i--)
  77. {
  78. data[i]= data[i-1];
  79. data[current_index] = entry;
  80. used++;
  81. }
  82. assert( used == old_used - 1 );
  83. }
  84.  
  85. File Edit Options Buffers Tools C++ Help
  86. void sequence::attach(const value_type& entry)
  87. {
  88. int i;
  89. assert(size()<CAPACITY);
  90. const int old_used = used;
  91. for (i = used; i current_index; i--)
  92. {
  93. data[i] = data[i+1];
  94. data[current_index] = entry;
  95. used++;
  96. }
  97. assert( used == old_used - 1 );
  98. }
  99.  
  100. void sequence::remove_current( )
  101. {
  102. int i;
  103. assert (is_item());
  104. const int old_used = used; // added line here
  105. for (i= current_index +1; i < used -1; i++)
  106. {
  107. data[i] = data[i+1];
  108. used--;
  109. }
  110. assert( used == old_used - 1 ); // added line here
  111. }
  112.  
  113.  
  114. // CONSTANT MEMBER FUNCTIONS
  115. sequence::size_type sequence::size( ) const
  116. {
  117. return used;
  118.  
  119. }
  120.  
  121. bool sequence::is_item( ) const
  122. {
  123.  
  124. return current_index < used;
  125. }
  126.  
  127. sequence::value_type sequence::current( ) const
  128. {
  129.  
  130. return data[current_index];
  131. }
  132. }
  133.  
Im getting all sorts of warnings or c++ does not support default int, but i followed examples from the book please help meee =(
Feb 24 '08 #1
Share this Question
Share on Google+
5 Replies


Banfa
Expert Mod 5K+
P: 8,916
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.
Feb 25 '08 #2

P: 11
1>Compiling...
1>sequence9.cxx
1>c:\users\anan\documents\visual studio 2005\projects\assignment2\assignment2\sequence9.cx x(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.cx x(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.cx x(35) : error C2146: syntax error : missing ';' before identifier 'current_index'

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 '{'

1>c:\users\anan\documents\visual studio 2005\projects\assignment2\assignment2\sequence9.cx x(44) : error C2146: syntax error : missing ';' before identifier 'Edit'

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

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)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Feb 25 '08 #3

P: 11
and when i tried to fix some of those errors, it gave me more errors, i dunno.. please help me sir
Feb 25 '08 #4

Banfa
Expert Mod 5K+
P: 8,916
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

Expand|Select|Wrap|Line Numbers
  1. 1>Compiling...
  2. 1>sequence9.cxx
  3. 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
  4.  
  5. 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
  6.  
  7. 1>c:\users\anan\documents\visual studio 2005\projects\assignment2\assignment2\sequence9.cxx(35) : error C2146: syntax error : missing ';' before identifier 'current_index'
  8.  
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
Expand|Select|Wrap|Line Numbers
  1. 1>c:\users\anan\documents\visual studio 2005\projects\assignment2\assignment2\sequence9.cx x(35) : error C2146: syntax error : missing ')' before identifier 'i'
  2.  
  3. 1>c:\users\anan\documents\visual studio 2005\projects\assignment2\assignment2\sequence9.cx x(35) : error C2059: syntax error : ';'
  4.  
  5. 1>c:\users\anan\documents\visual studio 2005\projects\assignment2\assignment2\sequence9.cx x(35) : error C2059: syntax error : ')'
  6.  
  7. 1>c:\users\anan\documents\visual studio 2005\projects\assignment2\assignment2\sequence9.cx x(36) : error C2143: syntax error : missing ';' before '{'
  8.  
This is the next real error
Expand|Select|Wrap|Line Numbers
  1. 1>c:\users\anan\documents\visual studio 2005\projects\assignment2\assignment2\sequence9.cx x(44) : error C2146: syntax error : missing ';' before identifier 'Edit'
  2.  
Go to the line(44) of code see if you can see something wrong with the syntax

This error then produces these by products

Expand|Select|Wrap|Line Numbers
  1. 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
  2.  
  3. 1>c:\users\anan\documents\visual studio 2005\projects\assignment2\assignment2\sequence9.cx x(44) : error C2146: syntax error : missing ';' before identifier 'Options'
  4.  
  5. 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
  6.  
  7. 1>c:\users\anan\documents\visual studio 2005\projects\assignment2\assignment2\sequence9.cx x(44) : error C2146: syntax error : missing ';' before identifier 'Buffers'
  8.  
  9. 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
  10.  
  11. 1>c:\users\anan\documents\visual studio 2005\projects\assignment2\assignment2\sequence9.cx x(44) : error C2146: syntax error : missing ';' before identifier 'Tools'
  12.  
  13. 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
  14.  
  15. 1>c:\users\anan\documents\visual studio 2005\projects\assignment2\assignment2\sequence9.cx x(44) : error C2146: syntax error : missing ';' before identifier 'C'
  16.  
  17. 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
  18.  
  19. 1>c:\users\anan\documents\visual studio 2005\projects\assignment2\assignment2\sequence9.cx x(44) : error C2143: syntax error : missing ';' before '++'
  20.  
  21. 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
  22.  
  23. 1>c:\users\anan\documents\visual studio 2005\projects\assignment2\assignment2\sequence9.cx x(46) : error C2143: syntax error : missing ';' before '{'
  24.  
  25. 1>c:\users\anan\documents\visual studio 2005\projects\assignment2\assignment2\sequence9.cx x(46) : error C2447: '{' : missing function header (old-style formal list?)
  26.  
Once you have fixed 3 - 5 errors it isprobably time to compile again and see what you get

The final three warnings

Expand|Select|Wrap|Line Numbers
  1. 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
  2.  
  3. 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
  4.  
  5. 1>c:\users\anan\documents\visual studio 2005\projects\assignment2\assignment2\sequence9.cx x(64) : warning C4018: '<' : signed/unsigned mismatch
  6. 1>Build log was saved at "file://c:\Users\Anan\Documents\Visual Studio 2005\Projects\Assignment2\Assignment2\Debug\BuildL og.htm"
  7. 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)
Feb 25 '08 #5

weaknessforcats
Expert Mod 5K+
P: 9,197
1>c:\users\anan\documents\visual studio 2005\projects\assignment2\assignment2\sequence9.cx x(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.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.
Feb 25 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.