未找到匹配令牌(比较两个立方体)

#include 
using namespace std;
class Cube
{
private:
    int m_L;
    int m_W;
    int m_H;

    
public:
    void setL(int l)
    {
        m_L = l;
    }
    int getL()
    {
        return m_L;
    }
    void setW(int w)
    {
        m_W = w;
    }
    int getW()
    {
        return m_W;
    }
    void setH(int h)
    {
        m_H = h;
    }
    int getH()
    {
        return m_H;
    }
    int calculateS()
    {
        return 2 * m_L * m_W + 2 * m_H * m_W + 2 * m_L * m_H;
    }
    int calculateV()
    {
        return m_L * m_W * m_H;
    }

    bool isSameByClass(Cube& c)
    {
        if (m_L == c.getL() && m_W == c.getW() && m_H == c.getH())
        {
            return true;
        }
        return false;
    }
};
//bool isName是一个全局函数(类外的)
bool isSame(Cube& c1, Cube& c2)//&是引用符号
{
    if(c1.getL() ==c2.getL()&&c1.getW()==c2.getW()&&c1.getH() ==c2.getH())//&&是并且的意思
    {
        return true;
    }
    return false;
}

int main()
{
    Cube c1;
    c1.setL(10);
    c1.setW(10);
    c1.setH(10);

    cout << "c1的面积为:" << c1.calculateS() << endl;
    cout << "c1的体积为:" << c1.calculateV() << endl;

    Cube c2;
    c2.setL(10);
    c2.setW(10);
    c2.setH(10);
    cout << "c2的面积为:" << c2.calculateS() << endl;
    cout << "c2的体积为:" << c2.calculateV() << endl;

    bool ret = isSame(c1, c2);
    if (ret)
    {
        cout << "c1和c2是相等的" << endl;
    }
    else
    {
        cout << "c1和c2是不相等的" << endl;
    }//利用全局函数判断
    ret = c1.isSameByClass(c2);//利用成员函数判断
    if (ret)
    {
        cout << "成员函数判断:c1和c2是相等的" << endl;
    }
    else
    {
        cout << "成员函数判断:c1和c2是不相等的" << endl;
    return 0;
}

else
    {
        cout << "成员函数判断:c1和c2是不相等的" << endl;
这里少了一个后花括号


修改后就可以运行了

img

参考GPT和自己的思路:

在这段代码中,出现了未找到匹配令牌的错误,因为在class Cube中的成员函数calculateS() 和calculateV() 中出现了不正确的乘法运算符。乘法运算符应该使用*而不是*。正确的代码如下:

int calculateS()
{
    return 2 * m_L * m_W + 2 * m_H * m_W + 2 * m_L * m_H;
}
int calculateV()
{
    return m_L * m_W * m_H;
}

另外,代码中定义了一个全局函数isSame(),它实现了判断两个立方体是否相等的功能,而class Cube中也定义了一个成员函数isSameByClass(),两者的实现思路是一样的。在使用时,最好统一使用其中一种即可,避免重复的代码。