与模板返回函数的返回错误
Compile error with template return type for a function
我在.hpp中具有此声明的模板类:
template<class FriendClass> class SocialConnection{
typedef std::set<FriendClass> FriendSet;
FriendSet _socialFriends;
public:
virtual const FriendSet& getFriends();
和.cpp:
const SocialConnection::FriendSet& SocialConnection::getFriends() {
return _socialFriends;
}
编译器给我一个集合声明的错误: Expected a class or namespace
为行const SocialConnection::FriendSet& SocialConnection::getFriends()
我一直在搜索为什么两个小时,没有任何结果。我无法在实现中使用模板类的名称?我该怎么做?我丢失了什么语法?
- 您的getfriends定义中的班级名称缺少模板参数。
-
您无法真正将模板代码放在CPP文件中,并期望它编译。它是一个模板,因此在使用何处将其实例化为一种类型。因此,您需要将其放在标题中。
模板&lt;typename f>
const typename socialconnection&lt;f> :: Friendset&amp;社会共同&lt;f> :: getfriends(){ 返回_社交朋友;}
正确的定义很长:
template<typename FriendClass>
const typename SocialConnection<FriendClass>::FriendSet&
SocialConnection<FriendClass>::getFriends()
{
return _socialFriends;
}
@pwned所说的话;在实例化时需要可见,因此将其放入标题中。请参阅此问题以进行解释。
还要注意返回类型之前的typename
- 它是必要的,因为FriendSet
是依赖的名称。此问题深入解释。
相关文章:
- (C++)分析树以计算返回错误值的简单算术表达式
- glad 导致 glfwSwapBuffers 返回错误消息
- 我的模板类方法返回错误类型?
- 共享内存:MapViewOfFile 返回错误 5
- 使用 std::forward_list 返回错误的队列实现
- FFMPEG:sws_scale返回错误:切片参数 0, 2160 无效
- __int64 CString 返回错误的值 - C++ MFC
- C++ OpenGL 中,绘制形状返回错误
- Python os.system() 返回错误值
- 函数从指针 c++ 中获取错误的值并返回错误的答案
- LibClang clang_getArgType() 返回错误的类型
- 除法函数返回错误的值
- 当命令失败时,gzip 会在 C++ 中返回错误代码吗?
- 矩阵逆变器返回错误的反转
- openCL-创建子缓冲区返回错误代码13
- Qt SQL LIKE语句返回错误
- GetLastError()从FtpPutFile()返回错误6
- PfCreateInterface返回错误120(未实现)
- 为什么 Pa_GetStreamTime() 返回错误?
- G++ 在尝试使用 G++ 使用 openCV 进行编译后返回错误