I'm trying to build 2 classes, one that creates a "function" object (Y = MX + B) and another that creates a Graph object, takes the function, and plots every Y value along the graph by inserting x values from -10 to 10 into the function. I have a loop which should take every Y value and place it along the graph, so long as it fits within the boundaries (the 2D vector is 21 elements tall and 21 elements wide). For some reason, when I try to actually replace the '-' char (that I have throughout the empty graph) with the '*' char, I get that error above.
Below is my Graph.cpp file.
// EDIT: Here is a MRE
#include <iostream>
#include <vector>
using namespace std;
class Graph
{
public:
Graph();
void displayGraph();
Graph editGraph();
private:
vector<vector<char>> yaxis;
vector<char> line;
vector<char> axis;
};
Graph::Graph()
{
axis = {'_','_','_','_','_','_','_','_','_','_','|','_','_','_','_','_','_','_','_','_','_',};
line = {'-','-','-','-','-','-','-','-','-','-','|','-','-','-','-','-','-','-','-','-','-'};
yaxis = {line,line,line,line,line,line,line,line,line,line,axis,line,line,line,line,line,line,line,line,line,line};
}
void Graph::displayGraph()
{
int i = 0;
do
{
int z = 0;
do
{
cout << yaxis[i][z];
z++;
}while(z < 20);
cout << endl;
i++;
}while (i < yaxis.size());
}
Graph Graph::editGraph()
{
Graph g1;
int i = 0;
int x = -10;
int m1, b1;
m1 = 2;
b1 = 3;
do
{
int yVal = (m1 * x) + b1;
if((yVal + 10) >= 0 && (yVal + 10) <= 21)
{
g1.yaxis[(yVal+10)][i] = '*';
}
x++;
i++;
} while (i < 21);
return g1;
}
int main()
{
Graph g1;
g1 = g1.editGraph();
g1.displayGraph();
}
I tried placing breakpoints along the loop in the editGraph function, but for I just can't make sense of this specific error and what it means for my code. Is the problem the manner by which I'm inserting the new char? should I be using .at()? I've looked at other questions here with the same error, but they don't seem to be using vectors like I am.