Hello. I am writing a program that evaluates expressions in C++ but I keep getti
ID: 3912257 • Letter: H
Question
Hello. I am writing a program that evaluates expressions in C++ but I keep getting errors between my main class and the symboltable.h and .cpp classes. I am hoping another set of eyes can pick up something I haven't been able to for the past 12 or so hours. Thanks
//symboltable.h
class SymbolTable
{
public:
SymbolTable(){}
void insert(string variable, double value);
double lookUp(string variable) const;
void init();
private:
struct Symbol
{
Symbol(string variable, double value)
{
this->variable = variable;
this->value = value;
}
string variable;
double value;
};
vector<Symbol> elements;
};
//symboltable.cpp
#include <string>
#include <vector>
#include "stdafx.h"
using namespace std;
#include "symboltable.h"
void SymbolTable::insert(string variable, double value) {
const Symbol& symbol = Symbol(variable, value);
elements.push_back(symbol);
}
double SymbolTable::lookUp(string variable) const
{
for (int i = 0; i < elements.size(); i++)
if (elements[i].variable == variable)
return elements[i].value;
return -1;
}
void SymbolTable::init(){
if(elements.size()>0){
for(int i = elements.size(); i>0; i--){
elements.pop_back();
}
}
}
//main.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include "stdafx.h"
using namespace std;
#include "expression.h"
#include "subexpression.h"
#include "symboltable.h"
#include "parse.h"
SymbolTable symbolTable;
void parseAssignments(stringstream& cin);
int main()
{
const int SIZE = 256;
Expression* expression;
char paren, comma, line[SIZE];
string program;
ifstream fin("input.txt");
while (true)
{
symbolTable.init();
fin.getline(line, SIZE);
if (!fin) break;
stringstream cin(line, ios_base::in);
//cout << "Enter expression: ";
cin >> paren;
cout << line << "";
try {
expression = SubExpression::parse(cin);
cin >> comma;
parseAssignments(cin);
cout << "Value = " << expression->evaluate() << endl;
}
catch (exception) {
return EXIT_FAILURE;
}
}
return 0;
}
Explanation / Answer
1. In symboltable.h , insert the following lines on top of file. Also it is safe to have #ifndef ...#endif
#include <string>
#include <vector>
using std::string;
using std::vector;
2. In symboltable.cpp , move the using statement after all the #include lines
3. In symboltable.cpp , change insert() function to remove the reference &
Also you can use clear() to remove all eleemnts in vector.
I guess there are errors in other files as well... but the errors in symboltable.h and .cpp should go after these changes. Use the modified files below...
-----------------------
//////////////////////// modified symboltable.h
#ifndef SYMBOLTABLE_H
#define SYMBOLTABLE_H
#include <string>
#include <vector>
using std::string;
using std::vector;
class SymbolTable
{
public:
SymbolTable(){}
void insert(string variable, double value);
double lookUp(string variable) const;
void init();
private:
struct Symbol
{
Symbol(string variable, double value)
{
this->variable = variable;
this->value = value;
}
string variable;
double value;
};
vector<Symbol> elements;
};
#endif
//////////////////////// modified symboltable.cpp
#include <string>
#include <vector>
#include "stdafx.h"
#include "symboltable.h"
using namespace std;
void SymbolTable::insert(string variable, double value) {
Symbol symbol = Symbol(variable, value);
elements.push_back(symbol);
}
double SymbolTable::lookUp(string variable) const
{
for (int i = 0; i < elements.size(); i++)
if (elements[i].variable == variable)
return elements[i].value;
return -1;
}
void SymbolTable::init(){
elements.clear();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.