gpt4 book ai didi

C++ Primer 第 9 章无法编译 : `useConvs.cc:50:19: error: call of overloaded ‘stod(std::string&)’ is ambiguous`

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:45:31 25 4
gpt4 key购买 nike

我买了 C++ Primer 第 5 版并下载了 the accompanying zip file with sources 。该 zip 文件是为 GCC 版本 4.7.0 开发的

我在 Linux、Ubuntu 13.10 和 GCC 版本 4.8.1 上

作为对我的系统设置和下载内容的完整性检查,我尝试通过在解压 zip 文件的文件夹中键入 make 来编译示例。除第 9 章 外,所有示例均已编译。我检查了 zip 存档中的 CompilerNotes.pdf,但它没有提到这个特定的错误消息。

我的问题是

我怎样才能修复 zip 文件中的第 9 章源代码,使代码按预期编译并且代码仍然合理地与本书保持一致?

编译错误是:

g++ -std=c++0x -I.. -I..\7 -c useConvs.cc -o useConvs.o
useConvs.cc: In function ‘int main()’:
useConvs.cc:50:19: error: call of overloaded ‘stod(std::string&)’ is ambiguous
double d = stod(s); // converts the string s to floating-point
^
useConvs.cc:50:19: note: candidates are:
In file included from useConvs.cc:33:0:
../Version_test.h:86:8: note: double stod(const string&, std::size_t*)
double stod(const std::string &s, std::size_t * = 0)
^
In file included from /usr/include/c++/4.8/string:52:0,
from /usr/include/c++/4.8/bits/locale_classes.h:40,
from /usr/include/c++/4.8/bits/ios_base.h:41,
from /usr/include/c++/4.8/ios:42,
from /usr/include/c++/4.8/ostream:38,
from /usr/include/c++/4.8/iostream:39,
from ../Version_test.h:70,
from useConvs.cc:33:
/usr/include/c++/4.8/bits/basic_string.h:2853:3: note: double std::stod(const string&, std::size_t*)

第 55 行重复同样的错误。第 50 行显示:

        double d = stod(s);   // converts the string s to floating-point

源码:

/*
* This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
* Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
* copyright and warranty notices given in that book:
*
* "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
*
*
* "The authors and publisher have taken care in the preparation of this book,
* but make no expressed or implied warranty of any kind and assume no
* responsibility for errors or omissions. No liability is assumed for
* incidental or consequential damages in connection with or arising out of the
* use of the information or programs contained herein."
*
* Permission is granted for this code to be used for educational purposes in
* association with the book, given proper citation if and when posted or
* reproduced.Any commercial use of this code requires the explicit written
* permission of the publisher, Addison-Wesley Professional, a division of
* Pearson Education, Inc. Send your request for permission, stating clearly
* what code you would like to use, and in what specific way, to the following
* address:
*
* Pearson Education, Inc.
* Rights and Permissions Department
* One Lake Street
* Upper Saddle River, NJ 07458
* Fax: (201) 236-3290
*/

// Version_test.h contains definitions for to_string and stod
// if the compiler does not yet define these functions,
// this code will use the definitions we provide
#include "Version_test.h"

#include <string>
using std::string;
#ifdef STRING_NUMERIC_CONVS
using std::to_string; using std::stod;
#endif

#include <iostream>
using std::cout; using std::endl;

int main()
{
int i = 42;
// converts the int i to its character representation
string s = to_string(i);

double d = stod(s); // converts the string s to floating-point
cout << "i = " << i << " s = " << s << " d is: " << d << endl;

// convert the first substring in s that starts with a digit, d = 3.14
string s2 = "pi = 3.14";
d = stod(s2.substr(s2.find_first_of("+-.0123456789")));

cout << "d = " << d << " s = " << s << " s2 is: " << s2 << endl;

return 0;
}

最佳答案

// Version_test.h contains definitions for to_string and stod
// if the compiler does not yet define these functions,
// this code will use the definitions we provide
#include "Version_test.h"

有问题的代码假定编译器没有这些函数的定义,并在该 header 中提供了它自己的定义。但似乎在您的特定情况下,编译器确实提供了该功能,并且它与 zip 中提供的功能冲突。

非限定查找找到::strod,ADL 找到::std::strod,具有完全相同的签名,编译器无法确定更好的重载解析候选者。我的猜测是,简单的解决方案是删除该 header ,或者如果您的实现不提供 to_string 然后注释掉 strod

关于C++ Primer 第 9 章无法编译 : `useConvs.cc:50:19: error: call of overloaded ‘stod(std::string&)’ is ambiguous`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20292164/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com