没有可行的转换从'bool'到'std :: string' [英] No viable conversion from 'bool' to 'std::string'

查看:408
本文介绍了没有可行的转换从'bool'到'std :: string'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码看起来像这样:

I have some code that looks like this:

static std::string Foo(const std::string& blah)
{
   if ( someWierdEdgeCase() ){
      return false;  // <-- this line has a compiler error
   }

   std::string resourcePath;
   resourcePath.append("/assets/");
   return resourcePath; 
}  

它用于编译正常,但后来升级到Xcode 5.1它不再编译。我得到的错误是以下一个:
从'bool'到'std :: string'的可行转换

It used to compile fine, but then I upgraded to Xcode 5.1, and now it no longer compiles. The error I get is the following one: No viable conversion from 'bool' to 'std::string'

我想知道我应该替换'return false',以便这样工作的方式,它曾经。

I would like to know what I should replace 'return false' with so that this works the way it used to.

通常,我会乐意返回非false以外的东西,显然这不是好的行为。但是不知何故这个代码用于编译和运行,我想知道它在做什么,如果这个代码库的一些其他部分是依赖什么是当然是奇怪的行为。

Normally, I would be happy to return something other than false, and clearly that is not good behavior. But somehow this code used to compile and run, and I want to understand what it was doing, in case some other part of this codebase is relying on what is certainly weird behavior.

推荐答案

这:

  return false;  // <-- this line has a compiler error

没有标准的方法来转换bool std :: string(请更正我,如果有或在gcc(旧的XCode mac编译器)中有特殊的东西)。这意味着你的代码库用于包含将bool转换为字符串的显式代码。

There is no standard way to convert bool to std::string (please correct me if there is or was something special in gcc (the old XCode mac compiler)). This means that your code base used to contain explicit code to convert the bool to string.

如果不是现在编译,这表明这个转换已从你的代码库中删除。

If it is not compiling now this suggests this conversion was removed from your code base.

几个人建议的替代品。但我怀疑这些都会工作。因为旧代码对它如何工作有一个依赖。因此,任何具体的推荐将取决于如何解决旧代码编译和当它返回 someWierdEdgeCase()是真的。基本上你需要复制这个行为。

A couple of people of suggested alternatives. But I doubt any of these are going to work. As the old code had a depedency on how it used to work. So making any specific recomendation will depend on working out how the old code compiled and what it returned when someWierdEdgeCase() is true. Basically you need to replicate this behavior.

否则你需要捕获代码中的所有用例,并改变行为以匹配新的行为。在这种情况下,我将更改函数的名称。重新编译并查看代码中断位置,并检查每个位置的行为,并确保其行为相同。

Otherwise you need to hunt down all used cases in your code and change the behavior to match the new behavior. In this case I would change the name of the function. Re-Compile and see where the code breaks and check the behavior at each location and make sure it behaves the same.

这篇关于没有可行的转换从'bool'到'std :: string'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆