【问题标题】:c++ classes not working for mec++ 类不适合我
【发布时间】:2015-10-20 08:26:22
【问题描述】:

我已经编写了这个程序,但它不起作用。它给出了一个错误,即 xy 未声明,并且在 17 行之前的 int 预期主表达式。

#include<iostream>
using namespace std;

class shapes
{
    int width, height;
public:
    int getvalue();
    void decideshape(int l, int b);
};

main()
{
    cout<<"to find what type of shape you have input the measurements"<<endl;
    shapes toy;
    toy.getvalue();
    toy.decideshape();
}

int shapes::getvalue()
{
    int l, b;
    cout<<"length = ";
    cin>>l;
    cout<<"breath = ";
    cin>>b;
}

void shapes::decideshape(x, y)
{
    if(x==y)
        cout<<"This is square"<<endl;
    else
        cout<<"This is rectangle"<<endl;
}

我应该如何从函数 getvalue 中返回 2 个值

【问题讨论】:

  • 另外你定义了getvalue()的返回类型,所以你需要返回一个整数,即使它是return 0;

标签: c++ class variables scope


【解决方案1】:
  1. 参数必须具有 C++ 中的类型。将shapes::decideshape 的定义写为

    void shapes::decideshape(int x, int y)
    
  2. 您不会从 shapes::getvalue 返回值。

  3. 您向shapes::decideshape 传递的参数太少(实际上没有)。预计将提供两个ints。

  4. 您需要告诉编译器函数返回的内容是显式。将int 返回值添加到main

【讨论】:

  • 非常感谢,我在getvalue中调用了decisionshapes。
  • 我应该如何从函数 getvalue 中返回 2 个值
  • @Dementor 是同一类型的吗?什么情况?如果您还有其他问题,请在单独的线程中提问。另外,首先展示你的方法。如果它不起作用,我们会为您提供帮助。
  • 同类型,我想将长度和呼吸从 getvalues() 返回到 main 或直接返回到 decisionshape() 函数
  • @Dementor 您可以声明/定义void shapes::getvalue(int&amp; length, int&amp; breath),然后通过传递的引用修改变量。但是,最好对每个值使用单独的函数。
【解决方案2】:

您在参数列表中缺少xy 的类型:

void shapes::decideshape(int x, int y)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-17
    • 1970-01-01
    • 2011-08-11
    • 2011-01-05
    • 2011-12-23
    • 2012-04-01
    相关资源
    最近更新 更多