【问题标题】:C++ - using return when the function being called on has parameters [closed]C ++ - 当被调用的函数有参数时使用返回[关闭]
【发布时间】:2015-05-23 01:27:41
【问题描述】:

我有这样的功能:

void functiont(int a, int b)
{
        if(playingnumber=="T")
        {
            returntransfer=1;
            struct A
            {
                ~A() // Destructor to run after returning variable
                {
                    void cardselecth(int playingcolorh, int playingnumberh);
                }
            }
            return returntransfer;
        }
}

无论如何,我需要在不调用 int a 或 int b 的情况下获取返回的变量。所以在另一个函数中我写了

newvar=functiont(int a, int b);

它给出了一个编译错误。我不知道该怎么做。我可以写 functiont(a,b);我得到一个错误;我写 functiont(int,int);并得到一个错误。我试过写 functiont();但是它假设函数在同一个文件中,我没有定义它,它不是(我在这里跨文件传输,所以我需要定义任何参数,以便它知道引用另一个文件)。

【问题讨论】:

  • 您的代码根本没有意义。为什么要在函数中间创建一个结构?此外,您不要调用这样的函数:newvar=functiont(int a, int b);
  • 你在析构函数的定义中间有一个函数声明?
  • 这篇文章中的代码毫无意义。投票结束。
  • 结构体定义后缺少分号应该也会出现编译错误
  • 看来你需要阅读基本的 C++ 书籍并学习简单的示例,而不是通过学习 C++。

标签: c++ function parameters return


【解决方案1】:

我不确定你想在那里做什么,但起初这个函数是一个 void 函数,所以它只能返回但不能返回值。因此,您的签名和 void 必须根据您要返回的类型进行更改。 在这种情况下

int functiont(int a, int b)

应该是合适的。

调用该函数时,您必须向它传递两个整数类型的参数。

newvar = function(1, 2);

发布错误也是一个好主意。最好的问候

【讨论】:

    【解决方案2】:

    如果你喜欢返回一个 int,你的函数的返回类型应该是 int 而不是 void。我更正了代码如下。用注释记下更改

    int functiont(int a, int b)
    {
       int returntransfer=0; //change , declaration out of if block
                             // to maintain the scope of variable 
       if(playingnumber=="T")
            {
                returntransfer =1;
    
            }
            return returntransfer;//change
    }
    

    你可以在函数中声明一个结构变量,但这并不意味着仅仅通过声明它就会被调用。

    struct A
    {
        ~A() // Destructor to run after returning variable
        {
           void cardselecth(int playingcolorh, int playingnumberh);
         }
    };//change added ; at end of declaration.
    

    编辑 添加完整程序以回答更多问题:

    #include<iostream>
    #include<string>
    
    int functiont(int a, int b)
    {
       int returntransfer=0; //change , declaration out of if block
                             // to maintain the scope of variable 
       std::string playingnumber ="T"; //Added again for completness
       if(playingnumber=="T")
            {
                returntransfer =1;
    
            }
    
        struct A
    {
        ~A() // Destructor to run after returning variable
        {
           void cardselecth(int playingcolorh, int playingnumberh);
         }
    };//change added ; at end of declaration.
            return returntransfer;//change
    }
    
    int main()
    {
        std::cout<<functiont(2,3);
    }
    

    【讨论】:

    • 我已将结构修复为具有分号。我将函数更改为 int,但是当我尝试从我的代码中调用此特定行时它仍然返回错误:transfer=cardselecth(int playingcolorh, int playingnumberh);它返回的具体错误如下: 在此范围内未声明 int 之前的预期主表达式 int cardselecth 之前的预期主表达式 我知道最后一个错误是什么意思,我认为我不应该得到它。两个 int 值之前可能有哪些字符?
    • @Musicrafter 我用完整编译的执行程序编辑了答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-16
    • 2016-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-21
    相关资源
    最近更新 更多