[英]Request for member which is of non-class type Class*
我正在編寫兩個用於計算方程中表達式的類,即:Equation和Expression。 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];
}
}
我還為Equation類的實現編寫了以下內容:
#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);
}
}
當我按上面所示運行程序時,它會編譯; 但是當我嘗試訪問方程式中聲明的左右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
和right
都是Expression
指針,因此您需要通過operator->
訪問它們,例如:
string s1 = left->equationPart;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.