Having trouble figuring out why my program keeps crashing this is coded in c++.
ID: 3860503 • Letter: H
Question
Having trouble figuring out why my program keeps crashing this is coded in c++. the error in my IDE is currently "syntax error: missing"," before '<'" in the "class gVector: public vector<T>" line
//gVector.h
#pragma once
#include <vector>
template <typename T>
class gVector: public vector<T>
{
public:
gVector(int low, int high);
// vector has high - low+1 elements in range [low,high]
T& operator [] (int i);
// operator verifies that lower <= i <= upper.
// if not, it throws the indexRangeError exception
void resize(int lowIndex, int highIndex);
// resize vector and set range to [lowIndex, highIndex]
private:
int lower;
int upper;
};
#include "gVector.cpp"
--------------------------------------------------------------------
//gVector.cpp
#ifndef __G_VECTOR_CPP__
#define __G_VECTOR_CPP__
#include "gVector.h"
// For all gVector<T> member function definitions, utilize base class vector
template <typename T>
//You must implement utilize vector<T>’s constructor(s)
gVector<T>::gVector(int low, int high) {
vector<int>(low, high);
lower = low;
upper = high;
}
//Override vector<T>’s []
//You must implement
template <typename T>
T& gVector<T>::operator[] (int i) {
if ((i < lower) || (i > upper)) {
throw indexRangeError("index range error", i, size());
}
else {
return gVector<T>::operator[](i - lower);
}
}
//Overload vector<T>’s resize
//You must implement
template <class T>
void gVector<T>::resize(int lowIndex, int highIndex) {
if (!(low <= high)) {
int temp;
temp = low;
low = high;
high = temp;
}
else {
vector.resize(highIndex - lowIndex + 1);
lower = lowIndex;
upper = highIndex;
}
}
#endif
-------------------------------------------------------------------------
//mainDriver.cpp
#include <iostream>
#include "gVector.h" //use gVector class
#include "CS255_Exceptions.h"
using namespace std;
int main() {
// this test declares and defines two types of gVector objects
const int MIN_TEMP = -10;
const int MAX_TEMP = 15;
gVector<char> code('A', 'Z');
gVector<double> fahrVector{ MIN_TEMP, MAX_TEMP }; //Alternate way of calling constructor
// a very simple encryption of upper-case Latin letters
for (char ch = 'A'; ch <= 'Z'; ch++)
code[ch] = ch + 5;
// tempVector [cel_temp] holds Fahrenheit equivalent of Celsius
// temperature cel_temp. Conversion is F = 9/5 C + 32
for (int cel_temp = MIN_TEMP; cel_temp <= MAX_TEMP; cel_temp++)
fahrVector[cel_temp] = 9.0 / 5.0*cel_temp + 32.0;
// Show simple encryption shift
cout << "Encryption Shift:" << endl;
for (char ch = 'A'; ch <= 'Z'; ch++)
cout << ch;
cout << endl;
for (char ch = 'A'; ch <= 'Z'; ch++)
cout << code[ch];
cout << endl << endl;
// output the temperature conversions
cout << "Temperature Conversion C - > F:" << endl;
cout << " C" << " " << "F" << endl;
for (int cel_temp = MIN_TEMP; cel_temp <= MAX_TEMP; cel_temp++) {
cout.setf(ios::fixed, ios::floatfield);
cout.precision(2);
cout.width(7);
cout << cel_temp << " " << fahrVector[cel_temp] << endl;
}
return 0;
}
--------------------------------------------------------------------------------------
#pragma once
#ifndef __CS255_EXCEPTION_CLASSES__
#define __CS255_EXCEPTION_CLASSES__
#include <sstream>
#include <string>
using namespace std;
class baseException
{
public:
baseException(const string& str = "") :
msgString(str)
{
if (msgString == "")
msgString = "Unspecified exception";
}
string what() const
{
return msgString;
}
// protected allows a derived class to access msgString.
protected:
string msgString;
};
// failure to allocate memory (new() returns NULL)
class memoryAllocationError : public baseException
{
public:
memoryAllocationError(const string& msg = "") :
baseException(msg)
{}
};
// function argument out of proper range
class rangeError : public baseException
{
public:
rangeError(const string& msg = "") :
baseException(msg)
{}
};
// index out of range
class indexRangeError : public baseException
{
public:
indexRangeError(const string& msg, int i, int size) :
baseException()
{
ostringstream indexErr;
indexErr << msg << " index " << i << " size = " << size << ends;
// indexRangeError can modify msgString, since it is in
// the protected section of baseException
msgString = indexErr.str();
}
};
// attempt to erase from an empty container
class underflowError : public baseException
{
public:
underflowError(const string& msg = "") :
baseException(msg)
{}
};
// attempt to insert into a full container
class overflowError : public baseException
{
public:
overflowError(const string& msg = "") :
baseException(msg)
{}
};
// error in expression evaluation
class expressionError : public baseException
{
public:
expressionError(const string& msg = "") :
baseException(msg)
{}
};
// bad object reference
class referenceError : public baseException
{
public:
referenceError(const string& msg = "") :
baseException(msg)
{}
};
// feature not implemented
class notImplementedError : public baseException
{
public:
notImplementedError(const string& msg = "") :
baseException(msg)
{}
};
// date errors
class dateError : public baseException
{
public:
dateError(const string& first, int v, const string& last) :
baseException()
{
ostringstream dateErr;
dateErr << first << ' ' << v << ' ' << last << ends;
// dateError can modify msgString, since it is in
// the protected section of baseException
msgString = dateErr.str();
}
};
// error in graph class
class graphError : public baseException
{
public:
graphError(const string& msg = "") :
baseException(msg)
{}
};
// file open error
class fileOpenError : public baseException
{
public:
fileOpenError(const string& fname) :
baseException()
{
ostringstream fileErr;
fileErr << "Cannot open "" << fname << """ << ends;
// fileOpenError can modify msgString, since it is in
// the protected section of baseException
msgString = fileErr.str();
}
};
// error in graph class
class fileError : public baseException
{
public:
fileError(const string& msg = "") :
baseException(msg)
{}
};
#endif // __CS255_EXCEPTION_CLASSES__
-----------------------------------------------------------------------------------------
Explanation / Answer
Fixed the bugs in your code.
1. You need write using std::vector afer #include <vector> in order to be able to start using the vector class. That is why you were getting that error
2. You used low and high instead of lowIndex and highIndex resize()
3. You should call the base class constructor using gVector<T>::gVector(int low, int high) : vector<T>(high-low)
Use high- low to find out how many elements are needed.
4. Use this->size() and this->resize()
5. Also had to include the exceptions header file in gvector.cpp
Here is the modiifed gVector.cpp and output. Please rate the answer if it helped. Thank you.
gVector.cpp
//gVector.cpp
#ifndef __G_VECTOR_CPP__
#define __G_VECTOR_CPP__
#include "gVector.h"
#include "CS255_Exceptions.h"
// For all gVector<T> member function definitions, utilize base class vector
template <typename T>
//You must implement utilize vector<T>’s constructor(s)
gVector<T>::gVector(int low, int high) : vector<T>(high-low){
lower = low;
upper = high;
}
//Override vector<T>’s []
//You must implement
template <typename T>
T& gVector<T>::operator[] (int i) {
if ((i < lower) || (i > upper)) {
throw indexRangeError("index range error", i, this->size());
}
else {
return vector<T>::operator[](i - lower);
}
}
//Overload vector<T>’s resize
//You must implement
template <class T>
void gVector<T>::resize(int lowIndex, int highIndex) {
if (!(lowIndex <= highIndex)) {
int temp;
temp = lowIndex;
lowIndex = highIndex;
highIndex = temp;
}
else {
this->resize(highIndex - lowIndex + 1);
lower = lowIndex;
upper = highIndex;
}
}
#endif
output
Encryption Shift:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
FGHIJKLMNOPQRSTUVWXYZ[]^_
Temperature Conversion C - > F:
C F
-10 14.00
-9 15.80
-8 17.60
-7 19.40
-6 21.20
-5 23.00
-4 24.80
-3 26.60
-2 28.40
-1 30.20
0 32.00
1 33.80
2 35.60
3 37.40
4 39.20
5 41.00
6 42.80
7 44.60
8 46.40
9 48.20
10 50.00
11 51.80
12 53.60
13 55.40
14 57.20
15 59.00
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.