简体   繁体   English

无法专门化功能模板

[英]Failed to specialize function template

This is homework, although it's already submitted with a different approach. 这是家庭作业,虽然已经提交了不同的方法。

I'm getting the following from Visual Studio 2008 我从Visual Studio 2008获得以下内容

error C2893: Failed to specialize function template 'void std::sort(_RanIt,_RanIt,_Pr)'

The code is as follows 代码如下

main.cpp
    Database<> db; 
    db.loadDatabase();
    db.sortDatabase(sort_by_title());  

Database.cpp
void Database<C>::sortDatabase(const sort_by &s) { 
    std::sort(db_.begin(), db_.end(), s); 
}

And the function objects are defined as 并且函数对象被定义为

struct sort_by : public std::binary_function<const Media *, const Media *, bool> { 
    virtual bool operator()(const Media *l, const Media *r) const = 0;
};

struct sort_by_title : public sort_by {
    bool operator()(const Media *l, const Media *r) const { ... }
};
...

What's the cure here? 这里有什么治疗方法?

[Edit] Sorry, maybe I should have made the inheritance clear [编辑]对不起,也许我应该明确继承

template <typename C = std::vector<Media *> >
class Database : public IDatabase<C>

[/Edit] [/编辑]

[Edit2] [EDIT2]
After the suggestion from Toolbox (which seemed very reasonable) I ended up with the following error message 在Toolbox的建议(看起来非常合理)后,我最终得到以下错误消息

error C2664: 'Database<>::sortMedia' : cannot convert parameter 1 from 'sort_by_title' to 'const sort_by &'

main.cpp is still the same, but with some slight modifications to the functor hierarchy and source files. main.cpp仍然是相同的,但对functor层次结构和源文件稍作修改。 Forward declarations and such did not work so I had to put the definitions in separate files. 前向声明等不起作用,所以我不得不将定义放在单独的文件中。

Search.h
struct sort_by_impl {
    virtual bool operator()(const Media *l, const Media *r) const = 0;
};
struct sort_by : public std::binary_function<const Media *, const Media *, bool> { 
    sort_by_impl *sbp;
    bool operator()(const Media *l, const Media *r) const {
        return (*sbp)(l, r);
    }
};

IDatabase.h
struct sort_by_title : public sort_by_impl {
    bool operator()(const Media *l, const Media *r) const {
        return (l->getTitle() < r->getTitle());
    }
};

I'm really not grokking this, what am I missing here? 我真的不喜欢这个,我在这里错过了什么? Some conversion operation, or what? 有些转换操作,还是什么?
[/Edit2] [/ EDIT2]

[Edit3] [EDIT3]
Last and final edit, I hope. 我希望上次和最后的编辑。 I actually got this working after debugging and rewriting some of the code. 实际上我在调试并重写一些代码后得到了这个工作。 This is what I ended up with, and it's the best I could do 这就是我最终得到的结果,这是我能做的最好的事情

class sort_by : public std::binary_function<const Media *, const Media *, bool> { 
public:
    sort_by(sort_by_impl *sbp) : sbp_(sbp) {};
    bool operator()(const Media *l, const Media *r) const {
        return (*sbp_)(l, r);
    }
private:
    sort_by_impl *sbp_;
};

main.cpp
    db.sortDatabase(&sort_by_title());

Database.cpp
void Database<C>::sortDatabase(const sort_by &s) { 
    std::sort(db_.begin(), db_.end(), s);

This seems to work, both in a separate project (spending the better part of this day messing with this) and in my actual project which I submitted some days ago. 这似乎是有效的,无论是在一个单独的项目中(花费今天更好的部分搞乱这个),还是在我几天前提交的实际项目中。
Thank you very much for your time and help! 非常感谢您的时间和帮助!
[/Edit3] [/ EDIT3]

I'm not sure this is what's causing the problem, as it has nothing to do with specializing std::sort , but in sortDatabase you shouldn't be passing in a functor that's meant to behave polymorphically. 我不确定这是导致问题的原因,因为它与专门化std::sort无关,但是在sortDatabase你不应该传入一个意味着多态行为的sortDatabase函数。 The reason is that std::sort accepts your function object by value, which means it gets copied as a sort_by object, not whatever it actually is (ie you have a slicing problem). 原因是std::sort按值接受你的函数对象,这意味着它被复制为sort_by对象,而不是它实际上是什么(即你有切片问题)。

If you want the function object to have a virtual operator() , the function object should hold a pointer to the polymorphic class like so: 如果希望函数对象具有虚拟operator() ,则函数对象应该保存指向多态类的指针,如下所示:

struct sort_by : public std::binary_function<const Media*, const Media*, bool> {
    bool operator()(const Media *l, const Media *r) const
    {
        return (*p_impl)(l, r);
    }

    sort_by_impl* p_impl;
};

Then, sort_by_impl can be your abstract base class from which specific sorting function objects derive and override. 然后, sort_by_impl可以是您的抽象基类,特定的排序函数对象从该基类派生和覆盖。 Hope that helps. 希望有所帮助。

EDIT 编辑

Based on the new error message, if I had to guess, you're trying to do something like this inside sortMedia : 基于新的错误消息,如果我不得不猜测,你正试图在sortMedia做这样的事情:

Database<std::vector<Media*> > db; // initialized elsewhere...

sort_by_title my_sort;
db.sortDatabase(my_sort);

The problem is that my_sort is of type sort_by_title , which is a derived form of sort_by_impl - not of type sort_by . 的问题是, my_sort是类型的sort_by_title ,这是一个派生形式sort_by_impl -的类型不是 sort_by That means you actually want to pass my_sort to be the sbp pointer in a sort_by object, which is the actual function object you'll use. 这意味着您实际上想要将my_sort传递给sort_by对象中的sbp指针,该对象是您将使用的实际函数对象。 To illustrate: 为了显示:

Database<std::vector<Media*> > db; // initialized elsewhere...

sort_by my_sort_fn;
my_sort_fn.sbp = new sort_by_title;
db.sortDatabase(my_sort_fn);

delete my_sort_fn.sbp;

The code isn't exception safe, by the way; 顺便说一下,代码不是例外安全的; consider replacing sbp with a reference-counting smart pointer. 考虑用引用计数智能指针替换sbp。 Or even easier, just declare the sort_by_title on the stack and pass in its address. 或者更简单,只需在堆栈上声明sort_by_title并传入其地址即可。 Just be careful not to let it be destroyed before it's used. 注意不要在使用之前将其销毁。 :) :)

Hopefully that helps. 希望这会有所帮助。 Let me know how it turns out! 让我知道结果如何!

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

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