我很难理解C++模板模板参数的语法。我理解为什么它们是有用的,根据的出色描述,我只是发现它们的语法很难理解。取自上述网站的两个示例(还有其他示例):
template <typename T, template <typename> class Cont>
class Stack;
和
template <template <typename,typename> class Cont>
class Wrapper3;
显然,如果不了解这种语法背后的原理,就不可能对这些声明进行泛化。记忆更难,而且似乎也没有多大帮助。
编辑:我意识到我对一个问题的尝
首先,请随意为这个问题建议更好的标题。
考虑下列方案:
#include <numeric>
namespace N { class C {}; }
int operator+( int i, N::C ) { return i+1; }
int main() {
N::C a[10];
std::accumulate( a, a+10, 0 );
}
g++ 5.4.0:成功编译(参见实时演示 )
clang++ 3.8.0 (参见实时演示 )
错误:
In file included from source_file.cpp:3:
/usr/include
考虑以下C++程序:
#include <iostream>
using namespace std;
template<typename T>
class example
{
public:
void function (T a)
{
std::cout<<a.size ();
}
};
int main() {
example<string> a; // this doesn't
string b = "a";
//example<int
我陷入了一个奇怪的问题,就是将Java泛型转换为等效的C++模板。现在,我在Java中定义了抽象类如下:
template <class T, class K>
public abstract class A<K extends FirstClass<someclass>, T extends SecondClass<? extends anotherclass>> {
现在,在java中,我知道类被分配给?在编译时,扩展了anotherClass。但是,在C++中,我无法找到实现这一目标的方法。有什么帮助吗?
我是C++的新手。在我的学习阶段,我遇到了以下问题。我正在尝试从一个类模板Queue派生一个类stack。编译器在stack的构造函数中引发以下错误
..\src\TrainingDay2.cpp:44:3: error: 'b' was not declared in this scope
b=a;
请帮助找出根本原因。
#include <iostream>
using std::cout;
using std::endl;
template<class T> class Queue //Base class
{
private:
T
我有一个从C++移植到Java的应用程序。有一段C++代码,我觉得很奇怪。
typedef std::string ArgName;
typedef std::map< ArgName, AnyData > ArgumentMap;
class Arguments : public ArgumentMap
{
public:
// Very important note: When read finds a numeric/set argument,
// it sets anyData.kind to Int. But STILL, it fills a
前几天,我发现这是可能的:
template <class T> struct base {};
struct derived: base<int> {};
int main()
{
// The base class template is accessible here
typename derived::base<double> x;
// from the comments, even this works
typename derived::derived::base<double>::base<
不幸的是,我主要在Java上下文中学习了类设计和设计模式;因此,有时我很难将熟悉的模式转换为C++。
假设我们希望有一个基类,它的功能由一个子类扩展。在Java中,我们会这样做:
public class BaseClass<T> {
//T is used here
protected int foo = 0;
}
public class DerivedClass<T> extends BaseClass<T> {
public void incr_foo() {
++foo;
}
我目前正在学习模板如何在C++中工作。特别是,我正在查看类模板中的单个成员函数模板。为了理解我的意思,代码如下所示。
// foo.h
template<typename A>
class foo {
template<typename B>
void boo(B);
};
// foo.cpp
template<typename A>
void foo<A>::boo(B value) {} // compiler error: 'Unknown' type name B
// or if I try thi
我正在编写一个矩阵操作库recently.And,我想在我的程序中重载操作符,但是当我使用朋友函数访问我定义的模板类中的私有变量时发生了错误。(只有当我重载运算符'+‘时,它才会报告错误,而操作的重载函数'-’不报告错误。)我定义的模板类如下:
template<class T>
class Matrix
{
int col;
int row;
T *matrix;
public:
Matrix(T *m, int row1, int col1):matrix(m), row(row1), col(col1){}
我有一个非常依赖于模板的C++库(.lib)。我正在为这个库创建一个C++包装器,以便在.dll外部公开它的函数。这些公开的函数将分别由Java和C#库通过JNA/JNI和PInvoke使用。
我从了解到,不可能导出模板。
有没有人对包装C++模板的最佳方法有很好的指导,这样既能保留它们的灵活性,又能将其导出?
例如,典型的C++函数如下所示:
template <class A, class B>
inline
A
do_something(A first, A last,
B result )
{
/* implementation *
我知道在C++中,泛型实际上并不存在,但是您可以使用template来模拟它。当您构建代码时,编译器会对代码进行预处理,并生成一个新代码,替换为对象声明中指定的实际值的泛型值,然后是真正编译的新代码。例如,假设类A如下所示:
template<class T>
class A
{
T f();
};
然后在代码中的其他地方,我们有A<int> a;。实际编译的代码是:
class A
{
//Replaces T by int in the pre-processing
int f();
};
在这整个介绍之后,让我们讲到重点。
我的问题是:
代码:
#include <memory>
struct Data;
std::unique_ptr<Data> make_me();
int main()
{
std::unique_ptr<Data> m = make_me();
return 0;
}
当然哪一个失败了:
In file included from <source>:1:
In file included from /opt/compiler-explorer/gcc-7.1.0/lib/gcc/x86_64-linux-gnu/7.1.0/../../
每当我尝试编译时,都会收到此错误消息
Error 6 error LNK2019: unresolved external symbol "void __cdecl MergeSort(class LinkedList<int> &)" (?MergeSort@@YAXAAV?$LinkedList@H@@@Z) referenced in function _main C:\Users\FOla Yinka\Documents\Visual Studio 2012\Projects\C++\C++\linkedlist.obj C++
Err