CRTP 的各种错误 (C++)
我知道我刚刚问了一个关于此的问题,但我不知道我做错了什么。我只重写了一小部分,找不到任何错误(使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要像这样实现您的专用函数:
并添加公共继承:
以便您可以访问
name()
。如果您想要通用实现:
You need to implement your specialized functions as this:
and also add public inheritance:
so you can access
name()
.If you want generic implementations:
如果您在模板定义之外定义类模板的成员,则需要包含模板规范:
您还需要从
Entity
公开继承:If you define members of a class template outside the template definition, then you need to include the template specification:
You also need to inherit publicly from
Entity
: