这个模板怎么错了?

chenhaifeng88888 2009-07-11 07:46:40



else return m;

..\BasicFunction\ExternalSort.cpp:271: error: no matching function for call to `generateRuns(const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, int&, const bio_analysis::SearchEngineType&)'






#include <iostream>

using namespace std;

template<typename T>
T _m(int x)
{
int k = 1;
double m = 10.001;
if(x == 1) return k;
else return m;

}

int main() {
_m<int>(1);
_m<double>(2);
}


**** Build of configuration Debug for project ResultParser ****

**** Internal Builder is used for build ****
g++ -O0 -g3 -Wall -c -fmessage-length=0 -oBasicFunction\ExternalSort.o ..\BasicFunction\ExternalSort.cpp
In file included from ..\BasicFunction\../bio_analysis_sdk.h:7,
from ..\BasicFunction\ExternalSort.h:4,
from ..\BasicFunction\ExternalSort.cpp:2:
..\BasicFunction\../../include/sdk.h: In member function `void proteomics_sdk::CSpectrum::Create_Hash_Table()':
..\BasicFunction\../../include/sdk.h:1002: warning: converting to `size_t' from `double'
..\BasicFunction\../../include/sdk.h: In member function `void proteomics_sdk::CSimpleMatchResult::track(const proteomics_sdk::CSpectrum&, const proteomics_sdk::CPeptideResult&, size_t, proteomics_sdk::CCondition&)':
..\BasicFunction\../../include/sdk.h:1068: warning: comparison between signed and unsigned integer expressions
..\BasicFunction\ExternalSort.cpp: In function `int generateRuns(std::string, int, const bio_analysis::SearchEngineType&)':
..\BasicFunction\ExternalSort.cpp:91: error: no matching function for call to `GetKey(bio_analysis::CMatchSpectraInfo&, const bio_analysis::SearchEngineType&)'
..\BasicFunction\ExternalSort.cpp:134: error: no matching function for call to `GetKey(bio_analysis::CMatchSpectraInfo&, const bio_analysis::SearchEngineType&)'
..\BasicFunction\ExternalSort.cpp: In function `void kwaymerge(int, int, int, const bio_analysis::SearchEngineType&)':
..\BasicFunction\ExternalSort.cpp:191: error: no matching function for call to `GetKey(bio_analysis::CMatchSpectraInfo&, const bio_analysis::SearchEngineType&)'
..\BasicFunction\ExternalSort.cpp:207: error: no matching function for call to `GetKey(bio_analysis::CMatchSpectraInfo&, const bio_analysis::SearchEngineType&)'
..\BasicFunction\ExternalSort.cpp: In function `int Merge(int, int, const bio_analysis::SearchEngineType&)':
..\BasicFunction\ExternalSort.cpp:240: error: no matching function for call to `kwaymerge(const int&, int&, int&, const bio_analysis::SearchEngineType&)'
..\BasicFunction\ExternalSort.cpp:248: error: no matching function for call to `kwaymerge(int&, int&, int&, const bio_analysis::SearchEngineType&)'
..\BasicFunction\ExternalSort.cpp: In function `void ExternalSort(const std::string&, size_t, const std::string&, const bio_analysis::SearchEngineType&, const int&)':
..\BasicFunction\ExternalSort.cpp:271: error: no matching function for call to `generateRuns(const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, int&, const bio_analysis::SearchEngineType&)'
Build error occurred, build is stopped
Time consumed: 2297 ms.

...全文
46 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Walf_ghoul 2009-07-11
  • 打赏
  • 举报
回复
可以尝试下5楼的做法。。。但是感觉这个模板局限性很大。。
pengzhixi 2009-07-11
  • 打赏
  • 举报
回复
template<typename T>
T _m(int x)
看你的错误信息,不是出在这个模版,这个模版只是产生了一个警告信息。
yshuise 2009-07-11
  • 打赏
  • 举报
回复
#include <iostream>

using namespace std;

template<typename T>
T _m(int x)
{
int k = 1;
double m = 10.001;
if(x == 1) return k;
else return m;

}

你这样写的局限行太大。没有利用函数模板的自己的推导功能,而且使用不方便。


owldestiny 2009-07-11
  • 打赏
  • 举报
回复
是类型转换的问题,这样写在Dev中就可以编译通过了
#include <iostream>

using namespace std;

template<typename T>
T _m(int x)
{
int k = 1;
double m = 10.001;
T temp;
if(x == 1) temp =static_cast<T>(k);
else temp = static_cast<T>(m);
return temp;

}

int main() {
cout<<_m<int>(1)<<endl;
cout<<_m<double>(2)<<endl;
system("PAUSE");
}
ToBeTough 2009-07-11
  • 打赏
  • 举报
回复
DEV c++中,这个能执行
#include <iostream>
using namespace std;
template<typename T>
T _m(int x)
{
int k = 1;
double m = 10.001;
if(x == 1) return k;
else return m;
}
int main() {
_m<double>(2);
return 0;
}
这个不能:
#include <iostream>
using namespace std;
template<typename T>
T _m(int x)
{
int k = 1;
double m = 10.001;
if(x == 1) return k;
else return m;
}
int main() {
_m<int>(1);
return 0;
}
为什么?
大前置 2009-07-11
  • 打赏
  • 举报
回复
我在VC6.0下编译没有问题
老邓 2009-07-11
  • 打赏
  • 举报
回复
if(x == 1) return k;
else return m;
这个判断里的两个return类型不同,所以只能有一个为T类型,当然编译不通过了。
老邓 2009-07-11
  • 打赏
  • 举报
回复
T _m(int x)
必须返回T的类型

64,662

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧