问题标签 [class-template]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
6 回答
15347 浏览

c++ - c++中类模板内的模板类

这里的菜鸟仍在尝试使用模板。试图写一个消息处理类模板

所有定义在一个头文件中。我已经建立并测试了我的课程,一切都很好。现在,我正在尝试这样做:

但是,这一行在“>”标记之前给了我一个错误的表达式类型的错误。

我添加了必要的头文件来定义 MessageType 是什么。我以前用过这个函数很多次,只是没有在这个上下文中。

我怀疑编译器不喜欢模板函数在未定义的类模板(未专门化?)中完全定义(专门化?)的事实。我并没有完全理解是什么让模板“专业化”。大多数解释都集中在“完整”或“部分”的概念上,而不是首先使其专业化的原因。

抱歉,如果您想查看更多代码。我在工作中没有互联网接入,这就是我做这件事的地方,所以我必须把所有东西都放在我的心理“便签本”中,然后把它带回家。

0 投票
2 回答
792 浏览

c++ - 如何使用此特定容器在 C++ 中创建二维数组

我正在尝试将 a
int a[][]
从 Java 移植到 C++。我将此类用作整数的容器ArrayRef,因为它处理引用,并且项目广泛使用它。在我声明的 AbstractReader 类中

const ArrayRef<int> START_END_PATTERN_;
const ArrayRef<int> MIDDLE_PATTERN_;
const ArrayRef<ArrayRef<int> > L_PATTERNS_;
const ArrayRef<ArrayRef<int> > L_AND_G_PATTERNS_;

static int START_END_PATTERN[];
static int MIDDLE_PATTERN[];
static int L_PATTERNS[10][4];
static int L_AND_G_PATTERNS[20][4];
注意后面的下划线以区分这两个变量。

我不确定如何初始化二维 ArrayRef。我在这里发布的内容会出现段错误,因为这些 ArrayRef 正在堆栈上分配。任何人有一个聪明的方法来做到这一点?

我实际上设法让它工作的唯一方法是使用ArrayRef< Ref<ArrayRef<int> > >ArrayRef 从 Counted 继承,它基本上是一个允许在 C++ 中进行引用计数的类。但是为了访问元素,我不得不做一些类似 *(foo[i])[j] 的事情,这比 foo[i][j] 稍微糟糕一些。

0 投票
4 回答
5000 浏览

c++ - Template class + virtual function = must implement?

This code:

is easily compiled without any complaints, as long as I never call a.DoSomething().

However, if I define DoSomething as a virtual function, I will get a compile error saying that B doesn't declare SomeFunction. I can somewhat see why it happens (DoSomething should now have an entry in the vtable), but I can't help feeling that it's not really obligated. Plus it sucks.

Is there any way to overcome this?

EDIT 2: Okay. I hope this time it makes sence: Let's say I am doing intrusive ref count, so all entities must inherit from base class Object. How can I suuport primitive types too? I can define:

so I can use Primitive<int>, Primitive<char>... But how about Primitive<float>? It seems like a problem, because floats don't have a %= operator. But actually, it isn't, since I'll never call operator %= on Primitive<float>. That's one of the deliberate features of templates.

If, for some reason, I would define operator %= as virtual. Or, if i'll pre-export Primitive<float> from a dll to avoid link errors, the compiler will complain even if I never call operator %= on a Primitive<float>. If it would just have fill in a dummy value for operator %= in Primitive<float>'s vtable (that raises an exception?), everything would have been fine.

0 投票
1 回答
680 浏览

templates - 在模板(非成员)函数中使用模板类中的 typedef

以下无法编译(无论如何,在 Linux 上使用 gcc 4.2.1):

问题在于这一行:

...并且编译器提出了这个抱怨:

foo.c:在函数'void templated()'中:

foo.c:22:错误:预期的 `;' 在“酒吧”之前</p>

通常在没有声明类型时会看到这一点,但据我所知,Foo< T >::FooType 在 templated() 中应该是完全有效的。

0 投票
1 回答
1165 浏览

c# - 更改新窗口和用户控件 (WPF) 的默认 Visual Studio 模板

我在哪里可以自定义 Visual Studio 放入特定类型的新类中的内容?我想在 WPF 模板中添加一行UserControlWindow因此创建新窗口时的代码如下所示:

而不是现在生成的:

编辑: DataContext="{Binding RelativeSource={RelativeSource Self}}"到 XAML 模板。

0 投票
1 回答
997 浏览

c++ - C++ - 使用类模板时出错

在文件 main.cpp...

在文件 pqueue.h ...

在文件 pqueue.cpp

错误 C2061:语法错误:标识符'arcComp'

0 投票
1 回答
1277 浏览

c++ - 如何初始化模板类型变量?

哪一个更好?

编辑:T是原始类型。

0 投票
1 回答
1114 浏览

java - Basic Java/Android class templates ( )

I know this is a very basic question, but I haven't been able to find a way to word it to find the answer either here or through Google.

In the following code:

What does the <'?> mean for the adapter view class template? My guess is that it means the parameter can be an AdapterView holding any type, but I'm not sure and figured I'd ask the community.

Thanks

0 投票
1 回答
356 浏览

c++ - 模板类和运算符重载

如何重载如下模板类?

我收到如下错误:

0 投票
2 回答
3931 浏览

c++ - 如何避免友元函数模板类内定义重定义错误?

考虑这段代码:

它现在编译得很好(ideone)。但是,如果我取消注释 的定义B,则会出现以下错误(ideone):

我知道(好吧,我想我知道)它给出这个错误的原因。

所以我的问题是:

如何避免友元函数模板类内定义重定义错误?

只要我在类中提供主模板(不是特化)的定义,我就会得到这个错误。以这种方式定义主模板还有另一个问题:它使类模板的所有实例化的所有f函数模板实例化,我也想避免这种情况。我想交一个朋友,但不是一个朋友,如果和不一样。同时,我也想提供类内部的定义。是否可以?friendBasef<T>Base<T>f<U>Base<T>UT