关于C++primer上例题的疑问

程序员菜鸟 2018-01-17 10:56:12
QueryResult TextQuery::query( const string &sought) const
{
static shared_ptr<set<line_no>> nodata(new set<line_no>);
auto loc = wm.find(sought);
if (loc == wm.end())
return QueryResult (sought, nodata, file);
else
return QueryResult(sought, loc->second, file);
}

在第12章节的文本查询程序 return QueryResult结果语句中报错:
严重性 代码 说明 项目 文件 行 禁止显示状态
错误(活动) E0312 不存在用户定义的从 "const std::string" 到 "QueryResult" 的适当转换 Project19 c:\Users\Administrator\source\repos\Project19\Project19\TextQuery.cpp 26

可是我检查了QueryResult的构造函数,第一个参数是string,
class QueryResult
{
friend ostream & print(ostream &, const QueryResult&);
using line_no = vector<string>::size_type;
public:
QueryResult(string s, shared_ptr<set<line_no>> p, shared_ptr<vector<string>> f) :
sought(s), lines(p), file(f) {}
private:
string sought; //查询单词
shared_ptr<set<line_no>> lines;
shared_ptr<vector<string>> file;
};

想问下各位大佬为什么会出现这样的错误
...全文
345 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
真相重于对错 2018-01-17
  • 打赏
  • 举报
回复
把你的代码贴全!
真相重于对错 2018-01-17
  • 打赏
  • 举报
回复
上边的代码贴错了
#include <string>
#include <iostream>
using namespace std;
class MyClass{
	string _str;
public:
	MyClass(string str):_str(str)
	{};
	string print()
	{
		return _str;
	}

};
MyClass  fun(const string &s)
{
	return MyClass(s);
}
int _tmain(int argc, _TCHAR* argv[])
{
	string* s = new string("foo");
	MyClass m = fun(*s);
	delete s;
	cout<<m.print()<<endl;
	return 0;
}

真相重于对错 2018-01-17
  • 打赏
  • 举报
回复
看错误不是const& string 问题 而且 QueryResult(string s, shared_ptr<set<line_no>> p, shared_ptr<vector<string>> f) : sought(s), lines(p), file(f) {} s 按值传递,会发生复制 简化一下代码就清楚了!
 QueryResult(string s, shared_ptr<set<line_no>> p, shared_ptr<vector<string>> f) :
        sought(s), lines(p), file(f) {}
paschen 版主 2018-01-17
  • 打赏
  • 举报
回复
引用 3 楼 程序员菜鸟的回复:
[quote=引用 1 楼 paschen 的回复:] 构造函数参数需要是const string&,而不能是string&
请问我把构造函数修改成了const,反而报错:参数列表不匹配,可是我检查了每个参数和构造函数都是一样的 这是为什么呢[/quote] 虽然都是string,但非const引用不能绑定到const引用类型的参数,或者构造函数参数不按引用传递,string&改成string
真相重于对错 2018-01-17
  • 打赏
  • 举报
回复
第几章的??
程序员菜鸟 2018-01-17
  • 打赏
  • 举报
回复
引用 1 楼 paschen 的回复:
构造函数参数需要是const string&,而不能是string&
请问我把构造函数修改成了const,反而报错:参数列表不匹配,可是我检查了每个参数和构造函数都是一样的 这是为什么呢
幻夢之葉 2018-01-17
  • 打赏
  • 举报
回复
修改为:QueryResult(const string &s
paschen 版主 2018-01-17
  • 打赏
  • 举报
回复
构造函数参数需要是const string&,而不能是string&
真相重于对错 2018-01-17
  • 打赏
  • 举报
回复
相互引用!所以出错 你没看到书上是类的前置声明吗?
程序员菜鸟 2018-01-17
  • 打赏
  • 举报
回复
引用 11 楼 hdt 的回复:
那个代码没有问题! 你检查一下,你的编译器,和你敲的代码是否有误? 还有我这里是vs2010,不完全支持c++11 using lineno = set<vector<string>::sizetype> 不支持,直接把他改了,编译没有问题
十分感谢,问题已经解决了(虽然不知道为什么这样可以解决) 我的两个类定义在两个不同的h文件里(两个文件互相include),当我把两个类定义放到同一个文件下时,编译器就不报错了
真相重于对错 2018-01-17
  • 打赏
  • 举报
回复
那个代码没有问题! 你检查一下,你的编译器,和你敲的代码是否有误? 还有我这里是vs2010,不完全支持c++11 using lineno = set<vector<string>::sizetype> 不支持,直接把他改了,编译没有问题
程序员菜鸟 2018-01-17
  • 打赏
  • 举报
回复
引用 4 楼 hdt 的回复:
第几章的??
是第五版434页源代码。 不是例题。
程序员菜鸟 2018-01-17
  • 打赏
  • 举报
回复
class QueryResult
{
	friend ostream & print(ostream &, const QueryResult&);
	using line_no = TextQuery::line_no;
public:
	QueryResult(const string  s, shared_ptr<set<line_no>> p, shared_ptr<vector<string>> f) :
		sought(s), lines(p), file(f) {}
private:
	string sought;     //查询单词
	shared_ptr<set<line_no>> lines;
	shared_ptr<vector<string>> file;
};
class TextQuery
{
public:
	using line_no = vector<string>::size_type;
	TextQuery(ifstream &);
	QueryResult query(const string&) const;

private:
	shared_ptr<vector<string>> file;
	map <string, shared_ptr<set<line_no>>>  wm;

};
QueryResult TextQuery::query( const string& sought) const
{
	static  shared_ptr<set<line_no>> nodata(new set<line_no>);
	auto loc = wm.find(sought);

	if (loc == wm.end())
		return QueryResult(sought, nodata, file);
	else
		return QueryResult(sought, loc->second, file);
}}
以上是两个类的定义,和发生错误的函数。 我按照版主说的把构造函数中的参数改成引用或者非引用都是不匹配,也试了把query函数传递的参数sought改成引用或者非引用。结果都是不匹配。

65,189

社区成员

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

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