Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

x I keep getting these two errors with this C++ code in a template: sequence4.h:96:33: error: redefinition of stack_sequence_4::sequence::size_type stack_sequence_4::sequence:: size() const typename sequence::size_type sequence::size()

x

I keep getting these two errors with this C++ code in a template: sequence4.h:96:33: error: redefinition of stack_sequence_4::sequence::size_type stack_sequence_4::sequence:: size() const typename sequence::size_type sequence::size() const ^ And check next comment. and sequence5.h:79:19: error: stack_sequence_4::sequence::size_type stack_sequence_4::sequence::size() const previously declared here size_type size( ) const {return first.size()+second.size();} ^

What am I doing wrong? I think the code is good beside these two parts. Here is the code:

#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 typename 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

Informix Database Administrators Survival Guide

Authors: Joe Lumbley

1st Edition

0131243144, 978-0131243149

More Books

Students also viewed these Databases questions

Question

KIND bar specfic marketing action

Answered: 1 week ago