【问题标题】:Pass by reference with template functions C++使用模板函数 C++ 通过引用传递
【发布时间】:2014-03-16 08:45:42
【问题描述】:

我正在尝试制作一个模板函数并通过引用将两个变量传递给它,每件事听起来都不错,但它从未编译过,错误消息是:-

error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

我尝试了一小部分代码,它给了我同样的错误,请帮忙吗??

这是一部分代码,其他的代码都是这样的:

int size , found = -1 ; 

template<class type> Read_Data( type &key , type &arr)
{ 
    cout << " please enter the size of your set \n " ; 
    cin >> size ;
    arr = new type[size]; 

    cout << " please enter the elements of your set : \n " ;
    for (int i = 0 ; i <size ; i ++ )
    {
        cout << " enter element number " << i << ": "  ;  
        cin >> arr[i] ;
    }
    cout << " please enter the key elemente you want to search for : \n " ;
    cin >> key ; 
}

void main(void)
{ 
    int key , arr ; 
    Read_Data (key, arr);

    /*after these function there is two other functions one to search for key 
    in array "arr" and other to print the key on the screen */ 
}

【问题讨论】:

  • Read_Data 没有返回类型,void main(void) 是非标准的。
  • 那么是什么让您认为它与模板有关?

标签: c++ templates pass-by-reference


【解决方案1】:

您只是缺少一些东西来编译代码(解决您看到的错误)。

基本上在int main()中,需要在实例化模板时指定类型,如:

Read_Data <int> (key, value); 

以便编译器知道你真正想用什么类型来实例化它。

还要注意,数组的类型应该是int *。因此,在模板化函数中,签名必须更改为:

template<class type> Read_Data( type &key , type* &arr)

这是更正后的代码,不会显示您之前看到的错误:

#include<iostream>  

using namespace std;

int size , found = -1 ; 

template<class type> void Read_Data( type &key , type* &arr)
{ 
    cout << " please enter the size of your set \n " ; 
    cin >> size ;
    arr = new type[size]; 
    cout << " please enter the elements of your set : \n " ;

    for (int i = 0 ; i <size ; i ++ )
    {   
        cout << " enter element number " << i << ": "  ;   
        cin >> arr[i] ;
    }   

    cout << " please enter the key elemente you want to search for : \n " ;
    cin >> key;                                                                                                                                         
}

int main()
{   
    int key;
    int * arr;
    Read_Data<int> (key, arr);

    /* after these function there is two other functions one to search for key 
     * in array "arr" and other to print the key on the screen 
     */ 
}   

【讨论】:

    【解决方案2】:

    您需要在函数Read_Data的声明中指定返回值类型。

    几个例子:

    template<class type> void Read_Data( type &key , type &arr)
    template<class type> int  Read_Data( type &key , type &arr)
    template<class type> type Read_Data( type &key , type &arr)
    

    如果您不打算在该函数中返回任何内容,则可能需要第一个示例...

    顺便说一句,您还需要:

    • 在函数main 中,将int arr 更改为int* arr
    • 在函数main 中,将Read_Data 更改为Read_Data&lt;int&gt;
    • 在函数main中,使用完后调用delete[] arr
    • 在函数Read_Data 中,将type &amp;arr 更改为type* &amp;arr

    【讨论】:

    • 无需在调用Read_Data时明确指定类型。类型推断会解决这个问题。
    • @Captain Obvlious:感谢您提供的信息;没有意识到这一点(实际上,我认为默认的返回值类型是int)。无论如何,我认为最好显式声明返回值类型,即使是为了代码可读性。
    • 你在说什么?类型参数未用于指定返回类型。
    • 我尝试了第一个示例并添加了 delete[] arr;到 Read_data() 函数的末尾,我真的不知道这个命令是做什么的,考虑到我将在代码的其他部分使用“arr”
    • @Captain Obvlious:我没这么说。我说过当没有返回值类型时,据我所知,编译器假定int
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-10
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多