Reading template string

My code is suppose read a string and place each section of the string into an array. My code works with <int>, but it does not work with <string>. I'm having trouble seeing what's wrong, since this is a templated, if it works with int it should also work with string. I'm getting segmentation fault error, so I believe that maybe my loop is incorrect, but I fail to see how it works with int types but not string types.
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
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

template<class T>
class A{
    
    public:
    
    A(): size(0), array(new T[size]) {}
    ~A(){
        delete [] array;
    }

    void read() {
        
    string wholeString, sizeOfArray;
    // ignores '['
    cin.ignore();
    getline(cin, wholeString);
    // converts the string before the ':' as the size of the array
    sizeOfArray = wholeString.substr(0, wholeString.find(":"));
    stringstream convert(sizeOfArray);
    convert >> size;
    // The string is everything after the semicolon and 1 whitespace
    wholeString = wholeString.substr(wholeString.find(":")+2);
    for(int index = 0; index < size; index++){
      if(index != size-1){
        // Converts the string before the whitespace as a templated object
        T aObject;
        stringstream convert(wholeString.substr(0,wholeString.find(" ")));
        convert >> aObject;
        // Adds it into the array
        array[index] = aObject;
        cout << "index "<< index << " has:    ";  
        cout << aObject<< '\n';
        // Update the string
        wholeString = wholeString.substr(wholeString.find(" ")+1);
      }
      else{
        // Places the string before the ']' into the templated array
        T aObject;
        stringstream convert(wholeString.substr(0,wholeString.find("]")));
        convert >> aObject;
        cout << "index "<< index << " has:    ";  
        cout << aObject << '\n';
        array[index] = aObject; // This line causes segmentation fault error when reading string
      }
    }
  }
  int size_() const {
      return size;
  }
   
    const T& operator[](int location) const { 
    for(int index = 0; index < size; index++){
      if (index == location){
        return array[location];
      }
    }
     abort();
  }
  
   T& operator[](int location)  { 
    for(int index = 0; index < size; index++){
      if (index == location){
        return array[location];
      }
    }
     abort();
  }
  
   friend std::ostream &operator<<(std::ostream &out, const A &anArray) {
    out << "[";
    for(int index = 0; index<anArray.size_(); index++){
      if(index == anArray.size_()-1){
        out << anArray[index];
      }
      else{
        out << anArray[index] << " ";
      }
    }
    return out << "]" << '\n';
 } 
    private:
    int size;
    T* array;
};

int main()
{
    A<int>intArray;
    intArray.read(); // [4: 10 -3 49 50]
    cout << intArray; // [10 -3 49 50]
    A<string>stringArray;
    stringArray.read(); // [3: xx y abbc]
    cout << stringArray; // [xx y abbc]
}
Last edited on
Because of the if statement on line 58, you have the following warnings:


In member function 'const T& A<T>::operator[](int) const [with T = int]':
62:3: warning: control reaches end of non-void function [-Wreturn-type]
In member function 'const T& A<T>::operator[](int) const [with T = std::basic_string<char>]':
62:3: warning: control reaches end of non-void function [-Wreturn-type]


Why do you have a for loop on line 57? Why not just return the value?
@TheIdeasMan

location
is the index to the location to the array, and the function returns the value at location. I forgot the abort(), but the function aborts if it's out of range.
Last edited on
Ok, why not this:

1
2
3
4
5
6
if (location < size) {
  return array[location]; 
}
else {
   abort();  // this is a bit drastic, consider throw exception instead.
}


Imagine you had 1 million items, can you see how inefficient it is to loop through most or all of them?

Consider using a vector instead of an array, then you won't need to use new, which is bad news in modern C++.

Edit:

but the function aborts if it's out of range.


It aborts the entire program.
Last edited on
As per my assignment specs, I have to use array. I don't think the overload[] operator is causing the segmentation fault error though.

Edit: I forgot at line 26: array = new T[size];
Last edited on
array is a pointer to T, but on lines 35 & 48 you are assigning a T.

Is that the problem ?
Topic archived. No new replies allowed.