请求非类类型类*的成员
Request for member which is of non-class type Class*
我正在编写两个类,用于计算方程中的表达式,即:方程和表达式。Equation 类包含两个 Expression* 成员变量,需要使用这两个变量来维护公式的右侧和左侧。为了解决它们如何访问每个表达式(字符串)的问题,我为 Exression 类的构造函数创建了以下定义和实现:
#ifndef EXPRESSION_H
#define EXPRESSION_H
#include <string>
using namespace std;
class Expression
{
private:
int l, r;
public:
Expression(string part);
string equationPart;
};
#endif
#include "Expression.h"
#include<cmath>
#include<iostream>
using namespace std;
Expression::Expression(string part)
{
int len = part.length();
equationPart[len];
for(int i = 0; i < len; i++)
{
equationPart[i] = part[i];
}
}
我还为方程类的实现编写了以下内容:
#ifndef EQUATION_H
#define EQUATION_H
#include "Expression.h"
#include <string>
using namespace std;
class Equation
{
public:
Equation(string eq);
~Equation();
int evaluateRHS();
int evaluateLHS();
void instantiateVariable(char name, int value);
private:
Expression* left;
Expression* right;
};
#endif
#include "Equation.h"
#include<cmath>
#include<iostream>
#include<cstdlib>
#include<cstring>
using namespace std;
Equation::Equation(string eq)
{
int length = eq.length();
string l;
string r;
bool valid = false;
short count = 0;
for(int i = 0; i < length; i++)
{
if(eq[i] == '=')
{
valid = true;
for(short j = i+1; j < length; j++)
{
r += eq[j];
}
for(short j = i-1; j > 0; j--)
{
count++;
}
for(short j = 0; j < count; j++)
{
l += eq[j];
}
}
}
if(valid == false)
{
cout << "invalid equation" << endl;
}
else if(valid == true)
{
left = new Expression(l);
right = new Expression(r);
}
}
当我如上所示运行程序时,它会编译;但是当我尝试访问在 Equation 中声明的左右 Expression 对象的字符串成员变量时,如下所示:
void Equation::instantiateVariable(char name, int value)
{
string s1 = left.equationPart;
string s2 = right.equationPart;
short length1 = s1.length();
short length2 = s2.length();
bool found1 = false;
for(short i = 0; i < length1; i++)
{
found1 = true;
if(left.equationPart[i] == name)
{
left.equationPart[i] = itoa(value);
}
}
//and so on...
}
我最终收到编译器错误:
error: request for member 'equationPart' in '((Equation*)this)->Equation::left', which is of non-class type 'Expression*'
此错误仅在 instantiateVariable 函数中就会多次出现。任何帮助或建议将不胜感激。
"left" 是一个指针,使用 "->" 访问。将"left."更改为"left->"
left
和right
都是Expression
指针,因此您需要通过operator->
访问它们,例如:
string s1 = left->equationPart;
相关文章:
- 根据模板类型选择类模板的成员类型?
- 函数模板签名中忽略的成员类型def 的访问说明符
- 在C++如何从数组中提取成员并返回成员类型的数组?
- C++从成员类型中扣除类型的功能模板?
- 无法初始化以 std::byte 作为成员类型的位字段
- 构造函数可以更改默认成员类型吗?
- 使用相同方法但不同成员类型构建类的最佳方法
- 允许通过指向方法的成员参数的指针来推断模板参数所指向的成员类型
- 使用 std::条件根据模板参数选择成员类型
- CRTP:如何推断要用作返回类型的成员类型?
- 对成员类型的成员方法使用 std::result_of<>
- C :可以从类及其受保护的成员类型继承可以继承吗?
- 如何在不指定不必要的模板参数的情况下使用模板类的成员类型
- 基类数据成员类型取决于派生类
- C 多态性:允许模棱两可的成员类型
- 使用使用成员类型别名的构造函数来推论类模板参数
- 获取与在模板参数中传递的函数成员类型相同的类
- 如果静态成员未初始化并且成员类型是类本身,该怎么办?
- boost::bind 无法绑定到纯虚拟基类中定义的非静态函数模板成员类型
- 将联合强制转换为其成员类型之一