C++...方形系列(每次输出相同)?

标签 c++ class function

每当我更改 s1.sqseries 中的值时,输出永远不会改变。它始终是 687194768。为什么会这样?

 #include<iostream>
 #include<cmath>
 #include<string>
 #include<iomanip>
 using namespace std;
 class square
 {
int a,b;
 public:
void sqseries(int x,int y)
{
    x=a;
    y=b;

    int c;
   for(c=a;c<=b;c++)
  {

   cout<<c*c<<endl;
  }
}
};
 int main ()
 {
  square s1;
  s1.sqseries(5,8);            /////////right here///////
  return 0;
  }

最佳答案

你需要:

void sqseries(int x,int y)
{
    a=x;
    b=y;
    ...
}

请注意,在您当前的代码中,您只需将 xy 重新分配给 ab因此,您的函数永远不会使用作​​为函数参数传递的值,而只是用 ab 的值覆盖它们。

关于C++...方形系列(每次输出相同)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9292797/

相关文章:

c++ - 删除全局隐式函数 - 避免模棱两可的运算符

c# - C# 如何返回结构体

c++ - ostream& 不命名类型错误。我在这里做错了什么?

C: 如何用更新的 GtkWidget 对齐属性替换 GtkAlignment 函数

c++ - 设计模式 : uniform interface for partially supported derived classes

c++ - 当 2 个线程共享同一个物理内核时,具有错误共享的 volatile 增量在发布时比在调试时运行得更慢

jquery - CSS 内容类而不是 attr

c++ - Def文件和cpp文件编译

javascript - 将代码包装在一个函数中。菜鸟在这里

c++ - 为什么具有非常量值的数组定义没有编译错误?