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.
}

variable.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;
};

variable.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

次の 2 つの解決策があります。

  1. あなたのVariable::evaluate()例のように、グローバル変数を使用します。Variable::Variable()もちろん、ヘッダーの代わりに「variable.cpp」に関数として追加できます。またはextern SymbolTable symbolTable、ファイル「variable.h」に a を追加することもできます。
  2. への参照をsymbolTableコンストラクターに渡します (そして、おそらくそれをVariableオブジェクト内に格納します。そうすれば、symbolTable はグローバル変数である必要はまったくありません。

ちなみに、using namespace stdヘッダーファイルの前に追加するのは一般的に悪いスタイルと考えられています。

于 2013-03-08T19:00:58.613 に答える
1

extern SymbolTable symbolTable;symbolTable を必要とするすべての人がインクルードするヘッダー ファイルに入る必要があります。次に、variable.cpp に次のものが必要です。SymbolTable symbolTable;

于 2013-03-08T18:59:30.687 に答える
0

コンストラクター内で 2 番目のクラスをインスタンス化する必要があります。これにより、2 番目のクラスとそのメンバーは、最初のクラスのコンストラクター内またはグローバル名前空間内でのみ使用可能になります。例えば:

MyFooClass CFoo;
MyBarClass CBar;

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