从另一个类c ++中的构造函数调用方法

时间:2013-03-08 18:54:36

标签: c++

我需要在另一个类的构造函数中调用一个类的方法。我不知道如何在没有得到“未在此范围内声明”错误的情况下执行此操作。注意我只是在学习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);
}

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

有两种解决方案:

  1. 您使用全局变量 - 就像您的Variable::evaluate()示例一样。您当然可以将Variable::Variable()作为函数添加到“variable.cpp”而不是标题中。或者你可以将extern SymbolTable symbolTable放到文件“variable.h”。
  2. 您将对symbolTable的引用传递给构造函数(并且可能将其存储在Variable对象中 - 这样,symbolTable根本不需要是全局变量。
  3. 顺便说一句,在头文件之前添加using namespace std通常被认为是不好的风格。

答案 2 :(得分:0)

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

MyFooClass CFoo;
MyBarClass CBar;

MyFooClass::MyFooClass()
{
  CBar = new MyBarClass();
  CBar.BarClassMemberFunction();
}