control may reach end of non-void function

Good day to all! I'm experiencing some issue with my custom 'vector' template class that I'm required to built to understand how the mechanism of a STL vector work. However one of the template function kept returning a 'control may reach end of non-void function. A quick search on the error and mainly its because the compiler is expecting a 'return'. But I've already achieved that. I'm using Xcode v9.4.1

e.g.

1
2
3
4
5
6
7
  template <class elemType>
elemType& Vector<elemType>::at(int i)
{
    if ((i >= 0) && (i<length)) {
        return list[i];
    }
}


the full class is here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#ifndef Vector_hpp
#define Vector_hpp

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <sstream>


using namespace std;

template <class elemType>
class Vector
{
public:
    
    Vector(); //constructor
    ~Vector();//destructor
    
    //Functions
    void create(int n);
    void push_back(const elemType& i);
    void pop_back();
    void print() const;
    void resize(int newsize);
    
    int size() const;
    int capSize() const;
    elemType& at(int i);
    
    //Array Functions
    bool isEmpty() const;
    bool isFull() const;
    
private:
    elemType *list;
    int length;
    int maxSize;
    
};

template <class elemType>
Vector<elemType>::Vector() // constructor
{
    create(2);
}

template <class elemType>
Vector<elemType>::~Vector() //destructor
{
    delete[]list;
}

template <class elemType>
void Vector<elemType>::create(int n)
{
    list = new elemType[n]; //create array of size n
    
    maxSize = n;
    length = 0;
}

template <class elemType>
void Vector<elemType>::push_back(const elemType& i)
{
    
    if (length >= maxSize)
    {
        resize(maxSize * 2); //increase the array size
    }
    list[length] = i;
    length++; //increase the length to keep track
}

template <class elemType>
void Vector<elemType>::pop_back()
{
    length--;
}

template <class elemType>
void Vector<elemType>::print() const
{
    for (int i = 0; i < length; i++)
    {
        cout << list[i] << " ";
        cout << endl;
    }
}

template <class elemType>
int Vector<elemType>::size() const
{
    return length;
}

template <class elemType>
void Vector<elemType>::resize(int newSize)
{
    elemType *newList = new elemType[newSize];
    elemType *newListPointer = newList;
    elemType *oldListPointer = list;
    
    //copy data from old array to new array
    while (oldListPointer != (list + length)) //while not yet at a blank position
    {
        *(newListPointer) = *(oldListPointer); //copy the value to the new position
        newListPointer++; //shift the pointer
        oldListPointer++;
    }
    
    maxSize = newSize;
    
    //throw away old Array
    delete[] list;
    list = newList;
}

template <class elemType>
int Vector<elemType>::capSize() const
{
    return maxSize;
}

template <class elemType>
elemType& Vector<elemType>::at(int i)
{
    if ((i >= 0) && (i<length)) {
        return list[i];
    }
}

template <class elemType>
bool Vector<elemType>::isEmpty() const
{
    return (length == 0);
}

template <class elemType>
bool Vector<elemType>::isFull() const
{
    return (length == maxSize);
}


#endif /* Vector_hpp */
If that if statement is not true, then where is the return?

How does that function return when the statement ((i >= 0) && (i<length)) is false?
Last edited on
@repeater do you mean that I should include a 'else' part and return something in it?
You could do that. Or you could just have a return at the end on its own:

1
2
3
4
5
6
7
8
9
10
11
12
template <class elemType>
elemType& Vector<elemType>::at(int i)
{
    if ((i >= 0) && (i<length)) {
        return list[i];
    }
    else
   {
      // Uh oh - what do you do here?
     return ...   // return what?
   }
}

or
1
2
3
4
5
6
7
8
9
10
template <class elemType>
elemType& Vector<elemType>::at(int i)
{
    if ((i >= 0) && (i<length)) {
        return list[i];
    }

      // Uh oh - what do you do here?
     return ...   // return what?
}


Either of these will fix the problem. But what should you return? If someone using your vector class asks for contents of element 50 , but the vector doesn't have an element 50 because the vector is smaller than that, what should you return? This is something you need to think about.
Last edited on
Topic archived. No new replies allowed.