尝试编译下面包含的程序,出现以下错误。为了成功编译,应在程序中进行哪些修改?
看起来GCC 4.9.2不能将某些移动构造函数实现为“ = default”。
引起问题的行是:
AssetLinkData::AssetLinkData(AssetLinkData&& other) noexcept = default;
编译错误:失败:obj / browser / asset_link_data.o g ++ -MMD -MF(我删除了更多标志)
asset_link_data.cc:错误:功能
'password_manager :: AssetLinkData :: AssetLinkData(password_manager :: AssetLinkData &&)'
默认情况下,其重新声明带有异常说明
与隐式声明不同
'password_manager :: AssetLinkData :: AssetLinkData(password_manager :: AssetLinkData &&)'
AssetLinkData :: AssetLinkData(AssetLinkData && other)noexcept =
默认;
源代码:
#include "asset_link_data.h"
#include <algorithm>
#include <utility>
#include "base/json/json_reader.h"
#include "base/json/json_value_converter.h"
#include "base/values.h"
namespace password_manager { namespace {
constexpr char kGetLoginsRelation[] =
"delegate_permission/common.get_login_creds"; constexpr char kWebNamespace[] = "web";
} // namespace
AssetLinkData::AssetLinkData() = default;
AssetLinkData::AssetLinkData(AssetLinkData&& other) noexcept = default;
AssetLinkData::~AssetLinkData() = default; AssetLinkData& AssetLinkData::operator=(AssetLinkData&& other) = default;
} // namespace password_manager
我在下面添加了asset_link_data.h的内容:
#ifndef COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_SITE_AFFILIATION_ASSET_LINK_DATA_H_
#define COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_SITE_AFFILIATION_ASSET_LINK_DATA_H_
#include <string>
#include <vector>
#include "base/macros.h"
#include "url/gurl.h"
namespace password_manager {
// The class parses an asset link file. The spec for the format is
// https://github.com/google/digitalassetlinks/blob/master/well-known/details.md
// The class cares only about two types of statements:
// - includes. Those are just a reference to a file to be loaded and parsed.
// - "get_login_creds" permission to a web page. That means that the target is
// allowed to get the credentials saved for the source.
// Only HTTPS URLs are taken into account.
class AssetLinkData {
public:
AssetLinkData();
AssetLinkData(AssetLinkData&& other) noexcept;
~AssetLinkData();
AssetLinkData& operator=(AssetLinkData&& other);
bool Parse(const std::string& data);
const std::vector<GURL>& includes() const { return includes_; }
const std::vector<GURL>& targets() const { return targets_; }
private:
std::vector<GURL> includes_;
std::vector<GURL> targets_;
DISALLOW_COPY_AND_ASSIGN(AssetLinkData);
};
} // namespace password_manager
#endif // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_SITE_AFFILIATION_ASSET_LINK_DATA_H_
最佳答案
错误消息非常清楚:
在其重新声明中默认具有与隐式声明不同的异常规范
您已经要求编译器继续进行,并按照定义的方式定义了构造函数,但是您还要求构造函数为noexcept
...,并且默认创建的构造函数不是noexcept
要解决此问题,您需要删除noexcept
说明符:
AssetLinkData::AssetLinkData(AssetLinkData&& other) = default;
或者,如果您想要一个no-except指定符,则需要以老式方式手动将其删除:
AssetLinkData::AssetLinkData(AssetLinkData&& other) noexcept
: includes_(std::move(other.includes_))
, targets_ (std::move(other.targets_ ))
{}