简体   繁体   English

重载,字符串和默认参数

[英]Overloading, strings, and default parameters

Refactoring legacy code, I came across this function (pseudocode): 重构遗留代码,我遇到了以下函数(伪代码):

int getMessage( char * buffer, int size = 300 );

Gee, look at that buffer just waiting to overflow. 哎呀,看着那个缓冲区,等待溢出。 So I came up with a function using std::string, and thought it would be nice to use function overloading: 所以我想出了一个使用std :: string的函数,并认为使用函数重载会很好:

int getMessage( std::string & buffer );

So far, so good. 到现在为止还挺好。 But when I try to call the function with a string: 但是,当我尝试使用字符串调用该函数时:

std::string buffer;
int rc = getMessage( buffer );

I get this error: 我收到此错误:

cannot convert 'std::string' to 'char*' for argument '1' to 'int getMessage(char*, int)'

Obviously, the compiler (GCC 4.1.2) tries hard to convert std::string to char* to satisfy the first function's parameter list (using the default value to satisfy the second parameter), gives up, but doesn't try the second function... 显然,编译器(GCC 4.1.2)尝试将std :: string转换为char *以满足第一个函数的参数列表(使用默认值满足第二个参数),放弃了,但没有尝试第二个功能...

I wouldn't have a problem working around this issue, but I'd like to know why this fails, and whether there would be a way to make it work as intended. 解决这个问题我不会有任何问题,但是我想知道为什么会失败,以及是否有一种方法可以使它按预期工作。

It works as expected on my GCC 4.3.2, maybe you misspelled the name of the overload? 它可以在我的GCC 4.3.2上正常运行,也许您拼错了重载的名称? There's no conversion from std::string to char*, so the compiler shouldn't have any problems choosing the correct overload. 从std :: string到char *没有转换,因此编译器选择正确的重载应该没有任何问题。

$ cat test.cpp
#include <string>
#include <stdio.h>

int getMessage( char * buffer, int size = 300 )
{
printf("1\n");
return 1;
}

int getMessage( std::string & buffer )
{
printf("2\n");
return 2;
}

int main()
{
std::string buffer;
buffer = "Hello";
int rc = getMessage( buffer );
}

$ g++ test.cpp -Wall -pedantic
test.cpp: In function ‘int main()’:
test.cpp:20: warning: unused variable ‘rc’
$ ./a.out 
2
$ $ g++ -v 2>&1|tail -n1
gcc version 4.3.2 (Ubuntu 4.3.2-1ubuntu12) 
$

Hmmm. There is no implicit conversion from std::string to char*, so that can't be your problem. 没有从std :: string到char *的隐式转换,所以这不是您的问题。 Are you sure your new function is visible at the call site? 您确定在呼叫站点上可以看到您的新功能吗?

You said this is pseudo-code. 您说这是伪代码。 Are you leaving something out? 你要丢东西吗? Are these template functions or member functions? 这些是模板函数还是成员函数? Please post more of the code or try to boil it down to a smaller test case. 请发布更多代码或尝试将其简化为较小的测试用例。

My guess is that the overload for the string version of the function isn't visible where you called it. 我的猜测是,函数的字符串版本的重载在您调用它的位置不可见。 Are you sure that it is in the correct header file, and is spelled correctly? 您确定它在正确的头文件中并且拼写正确吗?

Do you have a declaration of `int getMessage( std::string & buffer );' 您是否声明了“ int getMessage(std :: string&buffer);” in scope? 在适用范围? You are hitting this error because the proper function is not being found. 您遇到此错误是因为找不到正确的功能。

As always, once the problem is solved, the solution is painfully trivial and should have been obvious all along. 与往常一样,一旦解决了问题,解决方案就变得微不足道,而且应该一直很明显。

So I came up with a function using std::string... 所以我想出了一个使用std :: string ...的函数

...in my working directory, which compiled just fine, but -I and -L in my makefile were still pointing at the previous version of the library, which was blissfully unaware of the new function. ...在我的工作目录中,该目录编译得很好,但是我的makefile中的-I和-L仍指向该库的先前版本,该库很高兴没有意识到新功能。

Sorry for the bother. 对不起,麻烦了。 I've been an idiot. 我是个白痴。 I hope this doesn't become a habit. 我希望这不会成为习惯。 ;-) ;-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM