Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I keep getting this error with this C++ code in a template: template sequence::size_type sequence::size() const //ERROR IS HERE { //at any point of time,

I keep getting this error with this C++ code in a template: template sequence::size_type sequence::size() const //ERROR IS HERE { //at any point of time, some are on first and some are on 2nd, //so the total of these stack size will be the number of elements //in sequence return first.size()+second.size(); } The error is : C:\Windows\system32\cmd.exe /C C:/TDM-GCC-64/bin/mingw32-make.exe -j4 SHELL=cmd.exe -e -f Makefile "----------Building project:[ 08-lab - Debug ]----------" need 'typename' before 'stack_sequence_4::sequence::size_type' because 'stack_sequence_4::sequence' is a dependent scope sequence::size_type sequence::size() const ^" What am I doing wrong? I think the code is good beside this part. Here is the code:

sequence4.h

// FILE: sequence4.h // CLASS PROVIDED: sequence // TYPEDEFS and MEMBER CONSTANTS for the sequence class: // typedef ____ size_type // sequence::size_type is the data type of any variable that keeps track of // how many items are in a sequence. //

// CONSTRUCTOR for the sequence class: // sequence( ) // Postcondition: The sequence has been initialized as an empty sequence. // // MODIFICATION MEMBER FUNCTIONS for the sequence class: // void start( ) // Postcondition: The first item on the sequence becomes the current item // (but if the sequence is empty, then there is no current item). // // void advance( ) // Precondition: is_item returns true. // Postcondition: If the current item was already the last item in the // sequence, then there is no longer any current item. Otherwise, the new // current item is the item immediately after the original current item. // // void insert(const value_type& entry) // Postcondition: A new copy of entry has been inserted in the sequence // before the current item. If there was no current item, then the new entry // has been inserted at the front of the sequence. In either case, the newly // inserted item is now the current item of the sequence. // // void attach(const value_type& entry) // Postcondition: A new copy of entry has been inserted in the sequence after // the current item. If there was no current item, then the new entry has // been attached to the end of the sequence. In either case, the newly // inserted item is now the current item of the sequence. // // void remove_current( ) // Precondition: is_item returns true. // Postcondition: The current item has been removed from the sequence, and the // item after this (if there is one) is now the new current item. // // CONSTANT MEMBER FUNCTIONS for the sequence class: // size_type size( ) const // Postcondition: The return value is the number of items in the sequence. // // bool is_item( ) const // Postcondition: A true return value indicates that there is a valid // "current" item that may be retrieved by activating the current // member function (listed below). A false return value indicates that // there is no valid current item. // // value_type current( ) const // Precondition: is_item( ) returns true. // Postcondition: The item returned is the current item in the sequence. // // VALUE SEMANTICS for the sequence class: // Assignments and the copy constructor may be used with sequence objects.

#ifndef _STACK_SEQUENCE_H_ #define _STACK_SAVITCH_SEQUENCE_H_ #include // Provides size_t #include namespace stack_sequence_4 { template class sequence { public: // TYPEDEFS and MEMBER CONSTANTS typedef std::size_t size_type; // CONSTRUCTOR sequence( ); // MODIFICATION MEMBER FUNCTIONS void start( ); void advance( ); void insert(const T& entry); void attach(const T& entry); void remove_current( ); // CONSTANT MEMBER FUNCTIONS size_type size( ) const; bool is_item( ) const; T current( ) const; private: std::stack first; std::stack second; };

template sequence::sequence() { //initialize with empty stacks as no data is there first=std::stack(); second=std::stack(); }

template sequence::size_type sequence::size() const { //at any point of time, some are on first and some are on 2nd, //so the total of these stack size will be the number of elements //in sequence return first.size()+second.size(); } template void sequence::start() { //when we start we want any elements we have in first stack to all //go into second and just put one element(if exists) from second to first stack while(first.size()>0) {

second.push(first.top()); first.pop(); } if(!second.empty()) //if there is atleast one element, get that, its the 1st of sequence { first.push(second.top()); second.pop(); } }

template T sequence::current() const { //always first stack's top gives the current element return first.top(); }

template bool sequence::is_item() const { //since first stack's top shows current element, return whether there is an item in //first stack or not return !first.empty(); }

template void sequence::remove_current() { if(!first.empty()) //pop out the current element if it exists first.pop();

if(!second.empty()) //now we need to get next element from the second stack, check if its empty { first.push(second.top()); second.pop(); } else //second is empty , so no more next element, dump all element from first onto second . So //we now dont have any current element since we are clearing out first { while(first.size()>0) {

second.push(first.top()); first.pop(); } }

} template void sequence::insert(const T &entry) { T new_copy=entry; //make a copy of the entry to be inserted before current if(!first.empty()) //if there any current item, first get it out of first and put it on second { second.push(first.top()); first.pop(); } //now we put out new entry and that becomes current since its on top first.push(new_copy); }

template void sequence::attach(const T&entry) { T new_copy=entry; //make a copy of the entry first.push(new_copy); //simply push onto stack first, and since its on top , it will be current item }

template void sequence::advance() { //the next item (if any) to current is always on second stack if(!second.empty()) //is there a next item? { first.push(second.top()); //just get the next item and push it on first second.pop(); } else //if next item is not there, we should no longer have a current item , so dump everythign from first onto second { while(first.size()>0) {

second.push(first.top()); first.pop(); } } }

}

#endif

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intranet And Web Databases For Dummies

Authors: Paul Litwin

1st Edition

0764502212, 9780764502217

More Books

Students also viewed these Databases questions

Question

Discuss the history of human resource management (HRM).

Answered: 1 week ago