在完全专用的模板函数中实例化类的对象
Instantiate an Object of a Class in completely specialized template function
我不知道为什么我不能在一个完全专用的函数中实例化B类的对象,尽管我在B类中声明了该函数为朋友。请帮助。我不知道这是不是一个愚蠢的怀疑,但我是第一次学习C++模板。我得到以下错误:
1>c:usersgoogle drivelearnopencvlearningctemplateexample.cpp(12): error C2065: 'B' : undeclared identifier
1>c:usersgoogle drivelearnopencvlearningctemplateexample.cpp(12): error C2062: type 'int' unexpected
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:00.37
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
#include "stdafx.h"
using namespace std;
template<typename V>
void DoSomething(V v) //Generic Function
{
B<char> s;
s.data=1;
};
template<>
void DoSomething<int>(int cv) //Specialized Function
{
B<int> b1l; // not able to instantiate an object of class B
};
template<typename T> //Template class B
class B
{
int data;
template<class X1>
friend void DoSomething<X1>(X1);
};
int main(int argc,char *argv[])
{
int x=12;
DoSomething(x);
return 0;
}
定义时
template<typename V>
void DoSomething(V v) //Generic Function
{
B<char> s;
s.data=1;
};
B
尚未定义,因此会出现错误。没有什么是一些重新订购无法修复的:
using namespace std;
template<typename T> //Template class B
class B
{
int data;
template<class X1>
friend void DoSomething(X1);
};
template<typename V>
void DoSomething(V v) //Generic Function
{
B<char> s;
s.data=1;
};
template<>
void DoSomething<int>(int cv) //Specialized Function
{
B<int> b1l; // not able to instantiate an object of class B
};
相关文章:
- 为什么我不能引用指向实例化对象的函数的指针?
- 为什么在使用指针时不采用类成员的默认值,而不是直接实例化对象时?
- 实例化对象并调用方法,使用单行语法在 C# 或 C++ 中返回值?
- 为什么我不能更改实例化对象内部的向量?
- 为共享指针C++单独实例化对象
- 错误 C2280 / 在 std::vector 中实例化对象时复制构造函数出现问题?
- C++:我可以在 void 函数中实例化对象吗?
- 为什么当我在构造函数中创建线程时,实例化对象和对象的指针的行为不同
- 是否可以由于以下原因在堆中实例化对象
- 一类应管理其实例化对象
- 实例化对象时如何修复分段故障错误
- QML实例化C 对象.我如何访问他们的方法
- 如何在 c++ 中使用枚举参数实例化对象
- C 实例化对象,在没有指针的情况下,其他类构造函数中没有默认构造函数
- 用push_back()实例化对象时,如何将指针传递给对象的构造函数
- 我如何在C 类的内部实例化对象
- 两者之间的最佳方法是什么:实例化对象或使用指针
- 从标头中的类实例化对象
- 我能否获得一个C++编译器在编译时实例化对象
- 访问不带实例化对象的非静态成员函数