C++ 使用模板函数C+通过引用传递+;
我试图创建一个模板函数并通过引用将两个变量传递给它,每件事听起来都不错,但它从未编译过,错误消息是:-C++ 使用模板函数C+通过引用传递+;,c++,templates,pass-by-reference,C++,Templates,Pass By Reference,我试图创建一个模板函数并通过引用将两个变量传递给它,每件事听起来都不错,但它从未编译过,错误消息是:- error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 我尝试了一小部分的代码,它给了我同样的错误,请任何帮助 这是代码的一部分,所有其他代码都是这样的: int size , found = -1 ; template<class type> Read
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 */
}
int size,found=-1;
模板读取数据(类型和键、类型和arr)
{
大小;
arr=新类型[尺寸];
cout您需要在函数声明中指定返回值类型Read\u 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)
模板无效读取数据(类型和键、类型和arr)
模板内部读取数据(类型和键、类型和arr)
模板类型读取数据(类型和键、类型和arr)
第一个示例可能是您不打算在该函数中返回任何内容时所需要的
顺便说一句,您还需要:
- 在函数
main
中,将int-arr
更改为int*arr
- 在函数
main
中,将Read\u Data
更改为Read\u Data
- 在函数
main
中,使用完后调用delete[]arr
- 在函数
Read_Data
中,将type&arr
更改为type*&arr
您只是缺少了一些东西来编译代码(解决您看到的错误)
基本上,在int main()
中,实例化模板时需要指定类型,如下所示:
Read_Data <int> (key, value);
这是更正后的代码,不会显示您以前看到的错误:
#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
*/
}
#包括
使用名称空间std;
int size,found=-1;
模板无效读取数据(类型和键、类型*&arr)
{
大小;
arr=新类型[尺寸];
coutRead\u Data
没有返回类型,并且void main(void)
是非标准的。那么,你怎么会认为它与模板有关呢?不需要在调用Read\u Data
时显式指定类型。类型推断会解决这个问题。@Captain Obvlious:谢谢你的信息;我不知道(实际上,我认为默认的返回值类型是int
)。无论如何,我认为最好显式声明返回值类型,即使是为了代码可读性。你在说什么?type参数不是用来指定返回类型的。我尝试了第一个示例,并添加了delete[]arr;到Read_data()函数的结尾,我真的不知道这个命令做什么,考虑到我将在函数的其他部分使用“arr”code@CaptainObvlious:我没有这么说。我说的是,当没有返回值类型时,据我所知,编译器假定int
。
#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
*/
}