从另一个类的构造函数调用方法C++
问题描述:
我需要从另一个类的构造函数中的一个类调用一个方法。我不知道如何在没有得到“未在此范围内声明”错误的情况下执行此操作。注意我只是在学习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);
}
答
解决办法有两个:
- 您使用全局变量 - 喜欢你
Variable::evaluate()
例子。你当然可以在“variable.cpp”中添加你的Variable::Variable()
而不是标题。或者你可以把extern SymbolTable symbolTable
放到文件“variable.h”中。 - 你在引用传递到
symbolTable
到构造函数(也许存储的Variable
对象内 - 这样,符号表并不需要是一个全局变量,在所有
顺便说一句,这是通常被认为是不好的风格,在头文件之前添加using namespace std
答
extern SymbolTable symbolTable;
需要进入每个需要symbolTable的人员包含的头文件。然后,在variable.cpp,你需要有SymbolTable symbolTable;
答
您需要在构造函数中实例化第二个类,这会使其及其成员仅在第一个类的构造函数中可用,或者在全局名称空间。例如:
MyFooClass CFoo;
MyBarClass CBar;
MyFooClass::MyFooClass()
{
CBar = new MyBarClass();
CBar.BarClassMemberFunction();
}
所以我试图把extern SymbolTable symbolTable;到头文件,现在我得到一个'SysmbolTable'没有命名类型错误,我仍然'符号'没有在这个范围错误声明。 – user1677657 2013-03-08 20:02:17
是的,你需要包括“symoltable.h”以及。 – 2013-03-08 20:08:40
也许我不理解正确,但我想我已经做了包括“symbolTable.h”在variable.cpp – user1677657 2013-03-08 20:30:46