0

我需要在另一个类的构造函数中调用一个类的方法。我不确定如何在没有“未在此范围内声明”错误的情况下执行此操作。注意我只是在学习 C++。

请参阅 symboltable.cpp 中的评论,了解我在这里想要完成的工作。我不是在寻找任何人为我做这件事。我可以举一个例子或指出正确的方向,这样我就可以解决这个问题。

symboltable.h 代码:

class SymbolTable
{
public:
    SymbolTable() {}
    void insert(string variable, double value);
    void insert(string variable); // added for additional insert method
    double lookUp(string variable) const;
    void init(); // Added as mentioned in the conference area.
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 <algorithm>

using namespace std;

#include "symboltable.h"

/* Implementing the "unreferenced variable" warning.
 * Modify the symbol table by adding another insert method
 * that supplies only the variable name.
 * This method should be called when the variable name
 * is encountered while building the arithmetic expression tree.
 * It would be called in the constructor of the Variable class.
 * The existing insert method, which is called when an assignment is encountered,
 * would first check to see whether it is already in the symbol table.
 * If it is not, then it is unreferenced.
 */

void SymbolTable::insert(string variable, double value)
{
    /* This existing insert method, which is called when an assignment is encountered,
     * first needs to check to see whether it is already in the symbol table.
     * If it is not, then it is unreferenced.
     * */

    //Need to check if variable is in the expression need to find out how the         expression is stored!

if (find(elements.begin(), elements.end(), variable)) {
    const Symbol& symbol = Symbol(variable, value);
        elements.push_back(symbol);
    } else
        throw string("Error: Test for output");
}

/* Adding another insert method that supplies only the variable name.
 * This method should be called when the variable name is encountered
 * while building the arithmetic expression tree.
 * It should be called in the constructor of the Variable class.
 */
void SymbolTable::insert(string variable)
{
    const Symbol& symbol = Symbol(variable, symbolTable.lookUp(variable));
    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;
        else
        throw "Error: Uninitialized Variable " + variable;
    return -1;
}

void SymbolTable::init() {
elements.clear(); // Clears the map, removes all elements.
}

变量.h 代码:

class Variable: public Operand
{
public:
    Variable(string name)  //constructor
    {
        // how do i call symbolTable.insert(name); here
        // without getting 'symboleTable' was not declared in this scope error

        this->name = name;
    }
    double evaluate();
private:
    string name;
};

变量.cpp 代码:

#include <string>
#include <strstream>
#include <vector>
using namespace std;

#include "expression.h"
#include "operand.h"
#include "variable.h"
#include "symboltable.h"

extern SymbolTable symbolTable;

double Variable::evaluate() {
    return symbolTable.lookUp(name);
}
4

3 回答 3

1

extern SymbolTable symbolTable;需要进入需要符号表的每个人都包含的头文件。然后,在 variable.cpp 中,你需要有SymbolTable symbolTable;

于 2013-03-08T18:59:30.687 回答
1

有两种解决方案:

  1. 您使用全局变量 - 就像您的Variable::evaluate()示例一样。您当然可以Variable::Variable()在“variable.cpp”而不是标题中添加您的函数。或者你可以把一个extern SymbolTable symbolTable放到文件“variable.h”中。
  2. 您将引用传递给symbolTable构造函数(并且可能将其存储在Variable对象中 - 这样,symbolTable 根本不需要是全局变量。

顺便说一句,using namespace std在头文件之前添加通常被认为是不好的样式。

于 2013-03-08T19:00:58.613 回答
0

您需要在构造函数中实例化第二个类,这将使其及其成员仅在第一个类的构造函数中可用,或者在全局命名空间中。例如:

MyFooClass CFoo;
MyBarClass CBar;

MyFooClass::MyFooClass()
{
  CBar = new MyBarClass();
  CBar.BarClassMemberFunction();
}
于 2013-03-08T19:02:02.980 回答