CRTP 的各种错误 (C++)

发布于 2024-12-22 00:16:38 字数 1976 浏览 0 评论 0原文

我知道我刚刚问了一个关于此的问题,但我不知道我做错了什么。我只重写了一小部分,找不到任何错误(使用 C++父函数返回子函数作为参考)

我的代码:

#include <iostream>
#include <stdlib.h>
#include <cstring>

using namespace std;

template<class Derived>
class Entity {
    private:
        string _name;
    public:
        const string& name() const;
        Derived& name( const string& );
        Derived* This() { return static_cast<Derived*>(this); }
};

class Client : Entity<Client> {
    private:
        long int _range;
    public:
        const long int& range() const;
        Client& range( const long int& );
};

const string& Entity::name() const {
    return _name;
}

Derived& Entity::name(const string& name) {
    _name = name;
    return *This();
}

const long int& Client::range() const {
    return _range;
}

Client& Client::range( const long int& range ) {
    _range = range;
    return *this;
}

int main() {
    Client ().name("Buck").range(50);
    return 0;
}

结果:

untitled:25: error: ‘template<class Derived> class Entity’ used without template parameters
untitled:25: error: non-member function ‘const std::string& name()’ cannot have cv-qualifier
untitled: In function ‘const std::string& name()’:
untitled:26: error: ‘_name’ was not declared in this scope
untitled: At global scope:
untitled:29: error: expected constructor, destructor, or type conversion before ‘&’ token
untitled: In function ‘int main()’:
untitled:13: error: ‘Derived& Entity<Derived>::name(const std::string&) [with Derived = Client]’ is inaccessible
untitled:44: error: within this context
untitled:44: error: ‘Entity<Client>’ is not an accessible base of ‘Client’

我将非常感谢您的答案(我的无能可能是由于睡眠不足造成的:D)

I know I've just asked a question about this, but I cannot figure out what I'm doing wrong. I've rewritten just the small part and cannot find any errors (used C++ function in parent return child as reference)

My code:

#include <iostream>
#include <stdlib.h>
#include <cstring>

using namespace std;

template<class Derived>
class Entity {
    private:
        string _name;
    public:
        const string& name() const;
        Derived& name( const string& );
        Derived* This() { return static_cast<Derived*>(this); }
};

class Client : Entity<Client> {
    private:
        long int _range;
    public:
        const long int& range() const;
        Client& range( const long int& );
};

const string& Entity::name() const {
    return _name;
}

Derived& Entity::name(const string& name) {
    _name = name;
    return *This();
}

const long int& Client::range() const {
    return _range;
}

Client& Client::range( const long int& range ) {
    _range = range;
    return *this;
}

int main() {
    Client ().name("Buck").range(50);
    return 0;
}

The result:

untitled:25: error: ‘template<class Derived> class Entity’ used without template parameters
untitled:25: error: non-member function ‘const std::string& name()’ cannot have cv-qualifier
untitled: In function ‘const std::string& name()’:
untitled:26: error: ‘_name’ was not declared in this scope
untitled: At global scope:
untitled:29: error: expected constructor, destructor, or type conversion before ‘&’ token
untitled: In function ‘int main()’:
untitled:13: error: ‘Derived& Entity<Derived>::name(const std::string&) [with Derived = Client]’ is inaccessible
untitled:44: error: within this context
untitled:44: error: ‘Entity<Client>’ is not an accessible base of ‘Client’

I'd be very grateful for answers (my incompetence might be due to sleep deprivation though :D)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

合久必婚 2024-12-29 00:16:38

您需要像这样实现您的专用函数:

template<>
const string& Entity<Client>::name() const {
    return _name;
}

template<>
Client& Entity<Client>::name(const string& name) {
    _name = name;
    return *This();
}

并添加公共继承:

class Client : public Entity<Client>

以便您可以访问name()

如果您想要通用实现:

template<class x>
const string& Entity<x>::name() const {
    return _name;
}

template<class x>
x& Entity<x>::name(const string& name) {
    _name = name;
    return *This();
}

You need to implement your specialized functions as this:

template<>
const string& Entity<Client>::name() const {
    return _name;
}

template<>
Client& Entity<Client>::name(const string& name) {
    _name = name;
    return *This();
}

and also add public inheritance:

class Client : public Entity<Client>

so you can access name().

If you want generic implementations:

template<class x>
const string& Entity<x>::name() const {
    return _name;
}

template<class x>
x& Entity<x>::name(const string& name) {
    _name = name;
    return *This();
}
音盲 2024-12-29 00:16:38

如果您在模板定义之外定义类模板的成员,则需要包含模板规范:

template <class Derived>
const string& Entity<Derived>::name() const {
    return _name;
}

template <class Derived>
Derived& Entity<Derived>::name(const string& name) {
    _name = name;
    return *This();
}

您还需要从 Entity 公开继承:

class Client : public Entity<Client> {
    // stuff
};

If you define members of a class template outside the template definition, then you need to include the template specification:

template <class Derived>
const string& Entity<Derived>::name() const {
    return _name;
}

template <class Derived>
Derived& Entity<Derived>::name(const string& name) {
    _name = name;
    return *This();
}

You also need to inherit publicly from Entity:

class Client : public Entity<Client> {
    // stuff
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文