【问题标题】:How to pass an array to a function in c如何将数组传递给c中的函数
【发布时间】:2013-10-06 06:24:31
【问题描述】:

我是 C++ 编程新手,刚刚学习了数组。我正在尝试使用数组作为函数的参数,但程序无法编译。更具体地说,这是我的代码:

int main ()
{
int values [10],i;

cout<<"Enter 10 values: "<<endl;

for (i=0; i<10;i++)
{
    cin>>values[i];
}

    // This is the function to which I want to send the array.
getmaxmin (values, 10); 

}

我收到一条错误消息:“函数 main 中未解析的外部符号”。这是什么意思?

谢谢!

【问题讨论】:

    标签: arrays function unresolved-external


    【解决方案1】:

    您是否在使用该函数并定义任何已声明函数之前声明了该函数?

    int function();//declaration
    
    //...
    function();//call
    
    //...
    int function()//definition
    {
        //do stuff
    }
    

    【讨论】:

      【解决方案2】:

      在调用之前首先声明函数,

      int getmaxmin(int values[10]);  //Prototype
      
      
      getmaxmin (values); // Call
      
      int getmaxmin (int values[10])
      {
      
        // Define
      
      }
      

      通过这种方式,您可以在 C++ 中传递数组。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-03-14
        • 2018-11-14
        • 2012-07-25
        • 2011-11-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多