在另一个函数中使用一个函数的变量

问题描述 投票:0回答:3

我有两个功能,如下。我想做的事情很简单。要使用
Toh2(int dskToMv,int cLocation, int tmpLocation, int fLocation) 的变量并在 Toh 函数中打印它们,如下所示:

cout<<" Move one disk from the "<< orpeg << " to the "
    << depeg<< cLocation << "->" << fLocation <<  <<endl;

这是两个函数:

    void ToH2(int dskToMv, int cLocation, int tmpLocation, int fLocation)
{
//function to print the numbers
     if( dskToMv != 0 ) 
    {
        ToH2( dskToMv-1, cLocation, fLocation, tmpLocation );

  //The below print is what I want to use in the Toh2 function
 // cout << cLocation << "->" << fLocation << endl;
                    ToH2( dskToMv-1, tmpLocation, cLocation, fLocation );
    }
}

void ToH(int dskToMv, string orpeg, string expeg, string depeg) 
{


    if( dskToMv != 0 ) 
    {
        ToH( dskToMv-1, orpeg, depeg, expeg );//original peg(A),extra peg(B),destination peg(C)

    //print in this cout the two additional variables from ToH2
        cout<<" Move one disk from the "<< orpeg << " to the " << depeg  <<endl;
        ToH(dskToMv-1, expeg, orpeg, depeg );
    }


}



       int main()
        {


   int c;
cout << "Enter the number of disks: ";
cin >> c;

ToH(c, "original peg ", "extra peg", "destination peg");

return 0;
        }

输入3时输出
将一个磁盘从原始桩移动到目标桩

将一个圆盘从原来的钉子移到额外的钉子上

将一个磁盘从目标桩移至额外的桩

将一个磁盘从原始桩移动到目标桩

将一个圆盘从多余的钉子移到原来的钉子上

将一个磁盘从额外的钉子移至目标钉子

将一个磁盘从原始桩移动到目标桩

我想做的事:
将一个磁盘从原始桩移动到目标桩 1->3

将一个圆盘从原来的钉子移到额外的钉子 1->2

将一个磁盘从目标桩移动到额外的桩 3->2

将一个磁盘从原始桩移动到目标桩 1->3

将一个圆盘从多余的钉子移至原来的钉子 2->1

将一个磁盘从额外的钉子移至目标钉子 2->3

将一个磁盘从原始桩移动到目标桩 1->3

c++
3个回答
3
投票

您明智的选择是:

  1. 将第一个函数的局部变量传递给第二个函数。这通常是独立函数的通信方式。如果需要,重载第二个函数。
  2. 创建一个封装这两个函数功能的对象。共享变量成为成员变量。

其他(此处不太合适)方法是 lambda 函数和

std::bind

不太明智的选择是将共享变量设置为全局变量。不推荐。


1
投票

你想要做的事情是不可能的,也没有多大意义。

函数的参数仅在调用该函数期间存在。
如果您希望另一个函数使用它们的值,您需要让第一个函数调用该函数并以某种方式将这些值传递给它。
特别是,无法找到“如果我调用不同的函数,相应参数将具有的值”。

将功能合二为一。

void ToH(int dskToMv, int cLocation, string orpeg, int tmpLocation, string expeg, int fLocation, string depeg)
{
     if( dskToMv != 0 ) 
    {
        ToH( dskToMv-1, cLocation, orpeg, fLocation, depeg, tmpLocation, expeg);
        cout<<" Move one disk from the "<< orpeg << " to the " << depeg
            << " " << cLocation << " -> " << fLocation << endl;
        ToH( dskToMv-1, tmpLocation, expeg, cLocation, orpeg, fLocation, depeg);
    }
}

int main()
{
    int c;
    cout << "Enter the number of disks: ";
    cin >> c;
    ToH(c, 1, "original peg ", 2, "extra peg", 3, "destination peg");
}

或者,你可以使用桌子

void ToH(int dskToMv, int cLocation, int tmpLocation, int fLocation)
{
    static std::string names[] = {"original peg", "extra peg", "destination peg"};
    if( dskToMv != 0 ) 
    {
        ToH( dskToMv-1, cLocation, fLocation, tmpLocation);
        cout<<" Move one disk from the "<< names[cLocation - 1] << " to the " << names[fLocation - 1]
            << " " << cLocation << "-> " << fLocation << endl;
        ToH( dskToMv-1, tmpLocation, cLocation, fLocation);
    }
}


int main()
{
    int c;
    cout << "Enter the number of disks: ";
    cin >> c;
    ToH(c, 1, 2, 3);
}

0
投票

声明命名空间:

namespace mySpace {
       int dskToMv;
       // other variable
}

并从任何函数访问变量,如下所示:

mySpace.dskToMv =20;
// .....
cout<<mySpace.dskToMv;

这是在函数之间共享变量的通用方法。不管怎样,看来你实现了河内的塔楼。好吧,你的代码在逻辑上不正确。

© www.soinside.com 2019 - 2024. All rights reserved.