I need help with a C++ assingment. The code is pretty much done, but I\'m gettin
ID: 3918153 • Letter: I
Question
I need help with a C++ assingment. The code is pretty much done, but I'm getting this error: "Control may reach end of non-void function" that I need help fixing. You'll see it when you run the code. This assignment includes two files (sequence.cpp and sequence.h) and a client program written by my professor. The error is in sequence.cpp.
Here's the assignment:
Add attach(), remove_current(), and the big-3 to the sequence class from the last assignment. All requirements from the previous assignment are still in force. Your score on this assignment will take into consideration your work on both the previous assignment and this assignment.
Specification and Implementation
Please refer to the specification and implementation information from the last assignment.
To simplify your code, you should write two private helper functions named "copy()" and "clear()". Then your copy constructor will simply call "copy()", your destructor will simply call "clear()", and your assignment operator will include calls to both.
Make sure that when you copy your list the four pointer data members point to the nodes in the new list that correspond to the nodes that they pointed to in the original list.
When writing the attach() and remove_current() functions, I prefer to start with the most restrictive cases, and then the last "else" is the general case. You can see that my insert() function (from last assignment) follows this pattern: (1) inserting into an empty list, (2) inserting at the front of a list with at least one item, (3) inserting anywhere else. For attach() I also have 3 cases: (1) attaching to an empty list, (2) attaching at the end of a list with at least one item, (3) attaching anywhere else. For remove_current() I start with an assert statement to make sure there is a current item (since that is a precondition), then the cases are (1) remove from a list that has exactly one item, (2) remove the first item in the list, (3) remove any other item. (This third case sort of has another case embedded in it: if the item being removed is the last item I have to reset tailptr and set precursor to NULL.)
It is very hard to get this working for all conceivable cases, and also very hard to test it exhaustively by thinking of every conceivable case. Here is a client program that tests the class pretty exhaustively. (Even this program does not test every possible case.)
In an attempt to make this client program even more helpful, at about line 100 (inside the test_items() function) I've provided a block of code that you can uncomment to make the client show the actual contents of the sequence. However, if you uncomment that block, you'll need to temporarily make the data members of your sequence class public.
This is the client program that we will be testing your class with. If you can't get every case to work but most of them work (as evidenced by the point total that client program reports) you can still get a pretty good score on the assignment.
Client program:
Here's my code:
sequence.cpp:
#include "sequence.h"
void Sequence::clear(){
Node *c = headptr;
while(c!= NULL){
c = c->next;
delete headptr;
headptr = c;
}
headptr = tailptr = NULL;
curr = prevcurr = NULL;
s = 0;
}
void Sequence::copy(const Sequence &s){
Node *cur = s.headptr;
clear();
start();
while(cur != NULL){
attach(cur->data);
cur = cur->next;
}
}
Sequence::~Sequence(){
clear();
}
Sequence::Sequence(const Sequence &s):Sequence(){
copy(s);
}
void Sequence::operator=(const Sequence &s){
if(this == &s){
return;
}
copy(s);
prevcurr = s.prevcurr;
curr = s.curr;
}
Sequence::Sequence()
{
headptr = NULL;
tailptr = NULL;
curr = NULL;
prevcurr = NULL;
s = 0;
}
void Sequence::start(){
prevcurr = NULL;
curr = headptr;
}
void Sequence::insert(const int entry)
{
if (s == 0)
{
Node *temp = new Node();
temp->data = entry;
temp->next = NULL;
headptr = temp;
tailptr = temp;
curr = temp;
s++;
}
else
{
Node *temp = new Node();
temp->data = entry;
temp->next = NULL;
if (prevcurr == NULL || curr == NULL)
{
temp->next = headptr;
headptr = temp;
curr = temp;
prevcurr = NULL;
s++;
}
else
{
temp->next = curr;
prevcurr->next = temp;
curr = temp;
s++;
}
}
}
void Sequence::attach(const int entry){
if (s == 0)
{
Node *temp = new Node();
temp->data = entry;
temp->next = NULL;
headptr = temp;
tailptr = temp;
curr = temp;
s++;
}
else
{
Node *temp = new Node();
temp->data = entry;
if (!is_item())
{
prevcurr->next = temp;
curr = temp;
tailptr = temp;
temp->next = NULL;
s++;
}
else
{
prevcurr = curr;
temp->next = curr->next;
curr->next = temp;
curr = temp;
s++;
}
}
}
void Sequence::advance()
{
if (is_item())
{
prevcurr = curr;
curr = curr->next;
}
}
void Sequence::remove_current()
{
if(is_item())
{
if (prevcurr == NULL)
{
headptr = curr->next;
Node *tmp = curr;
curr = curr->next;
s--;
delete tmp;
}
else
{
prevcurr->next = curr->next;
Node *tmp = curr;
curr = curr->next;
s--;
delete tmp;
}
}
}
int Sequence::current()const
{
if (is_item())
{
return curr->data;
}
}
size_t Sequence::size()const
{
return s;
}
bool Sequence::is_item()const
{
if (curr == NULL)
return false;
return true;
}
sequence.h:
#ifndef _SEQUENCE_H
#define _SEQUENCE_H
#include<iostream>
using namespace std;
struct Node{
int data;
Node *next;
};
class Sequence{
Node *headptr,*tailptr,*curr,*prevcurr;
int s;
void clear();
void copy(const Sequence &s);
public:
Sequence();
~Sequence();
Sequence(const Sequence &s);
void operator=(const Sequence &s);
void start();
void advance();
void insert(const int entry);
void attach(const int entry);
void remove_current();
size_t size()const;
bool is_item()const;
int current()const;
};
#endif
Explanation / Answer
"Control may reach end of non-void function"
Above error occurs in the scenario when return value of a function is not void and expects some value or address. It means, if a function has possible to exit in any case without returning anything in a non-void function then this error occur.
In sequence.cpp code the error lies in below function.
int Sequence::current()const
{
if (is_item())
{
return curr->data;
}
}
1. Here, if is_item() is true then only we are returning data value, if is_item() is false then if() condition will not execute and we are not returning anything, so while compilation the compiler do some static analysis and returns the possible errors.
2. If is_item() is flase then also we should return some error value.
Solution:
int Sequence::current()const
{
if (is_item())
{
return curr->data;
}
else
{
return -1;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.