简体   繁体   中英

Calling a method from a constructor in another class c++

I need to call a method from one class in the constructor of another class. I am not sure how to do this without getting a "was not declared in this scope" error. Note I am just learning C++.

See the comments in symboltable.cpp for what I am trying to accomplish here. I am not looking for anyone to do it for me. I could use an example or pointed in the right direction so I can figure this out.

symboltable.h code:

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 code:

#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 code:

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 code:

#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);
}

extern SymbolTable symbolTable; needs to go into the header file that is included by everyone who needs symbolTable. Then, in variable.cpp, you need to have SymbolTable symbolTable;

There are two solutions:

  1. You use a global variable - like your Variable::evaluate() example. You can of course add your Variable::Variable() as a function in "variable.cpp" instead of the header. Or you can just put a extern SymbolTable symbolTable to the file "variable.h".
  2. You pass in a reference to symbolTable into the constructor (and perhaps store that inside the Variable object - that way, symbolTable doesn't need to be a global variable at all.

By the way, it's generally considered bad style to add using namespace std before header files.

You need to instantiate the second class, either within the constructor, which will make it and its members available only within the constructor of the first class, or in the global namespace. For example:

MyFooClass CFoo;
MyBarClass CBar;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM