- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
简单的案例。我不太明白为什么括号对于调用显式实例化模板的默认构造函数是必需的。而且,为什么调用显式实例化模板的非默认 ctor 会给我“不完整类型”错误?
非常感谢!
// X.h
template <const int MODE>
class X{
public:
X() = default;
X(int& a) {++a;}
// define X here
};
// declare the explicit specialization
template <> class X<1>;
// Then define the default behaviors of X.
// X.cpp
#include "X.h"
template <>
class X<1>{
public:
X() = default;
X(int& a) {--a;}
// define X<1>
};
// Then specialize behavior.
// main.cpp
#include "X.h"
int main(){
X<2> x_2; // fine, instantiates the default version
X<1> x_1(); // Edit: A function declaration, as pointed out in the comment.
X<1> x_1_1; // error: aggregate ‘X<1> x_1_1’ has incomplete type and cannot be defined
int a = 0;
X<1> x_1_2(a); // error: variable ‘X<1> x_1_2’ has initializer but incomplete type
}
最佳答案
正如其他人指出的那样X<1> x_1();
只是一个函数声明,所以它实际上并没有实例化 X<1>
类型的对象.对于 incomplete type
错误:您必须将 X<1>
的整个声明进入头文件(不仅仅是前向声明,就像你现在所做的那样)。您可以将实现 放在cpp 文件中,但是任何使用类型为X<1>
的对象(不仅是指向对象的指针)的人都可以。 (在这种情况下:main
)必须知道它有多大以及它提供什么方法。
您的困惑可能部分源于您在示例中使用特化的方式:在您的特化中,与通用模板唯一不同的是其中一个构造函数的定义(所有签名保持不变)。所以你可能认为编译器可以自己解决这个问题。事实上,它不可能这样做,因为您的专用类可能看起来与非专用模板完全不同(具有不同的构造函数、不同的成员/成员函数)。像这样:
template <int I>
struct X {
bool hello(int x, int y);
};
template<>
struct X<1> {
int goodbye(std::string x);
};
如果编译器只看到template<> struct X<1>;
相反,它应该如何找出这个类的接口(interface)?
关于c++ - 创建显式专用模板类对象会产生 "object has initializer but incomplete type"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26343111/
import numpy as np from matplotlib import pyplot as plt import scipy.io.wavfile as wav from numpy.li
我正在尝试编译以下内容(在 MSVC 中): #define TRAP (errorCode = (errorCode != 0) ? errorCode :) int someFunction(
下面的代码究竟是如何工作的? #include template T x = T{}; void foo() { class Test { public: T
我不知道如何解释为什么创建成员有效 inner在类模板中 OuterTempl而在未模板类中这样做是非法的 Outer . // Non-template version struct Outer {
我正在查看 tensorflow 日志并发现以下行: 4 ops no flops stats due to incomplete shapes. Consider passing run_meta
我正在尝试使用Visual Studio 2019的内置编译器读取C++中的文件。在发现一些示例之后,我尝试这样做: #include using namespace std; int main()
我是一个相对较新的 C 程序员,所以请容忍我的无知:-)我正在尝试为 valgrind 编译提供一个自定义工具。该工具最初是大约 8 年前编写的,基于更旧版本的 valgrind。原始版本的 valg
我正在 recv while 循环中将一些数据读入缓冲区。 缓冲区前面有内容,我需要查看这些内容并获取剩余的字节。因此,我使用指针在缓冲区中导航以获取所需的字符,以便将剩余字节复制到另一个缓冲区中。
在下面的代码中,我想使用默认构造函数{.data = value},因为我希望我的类是POD。我不明白我在编译时收到的错误消息(llvm 或 gnu,c++11): #include class a
我有一个类应该有同一个类的私有(private)成员,例如: class A { private: A member; } 但它告诉我 member 是不完整的类型。为什么?如
Get-Process s* | where {s$_.Path} | dir | sort LastWriteTime | Format-Table fullname
我正在使用 RStudio,并尝试在我当前的项目中使用 packrat。我单击“在此项目中使用 packrat”复选框并按“确定”,我从控制台获得以下输出: > packrat::init()
我想使用 API 检索所有未删除且不完整的订单的列表。即类似于您可以在 Bigcommerce 管理页面上查看的订单列表。我知道我可以将 is_deleted 标志设置为 false 来过滤这些,但我
这个问题和看到的类似here . 我有大量大型 CSV,我正在通过函数连续加载和解析它们。其中许多 CSV 没有问题,但是当我尝试使用 read.csv() 加载它们时,有几个会导致问题。 我已将其中
我正在尝试将库移植到 Mac OS X。编译器报告了不完整的类型错误。具体来说:字段的类型不完整'header_t []。但是,当我查看源代码时,header_t 是在 packet_state_t
如果我有定义: typedef struct y_t *Y; 和 typedef struct x_t *X; struct x_t { Y *b; Y a; int s
这个问题已经有答案了: C programming decoupling interface from implementation with struct forward declaration (
当我尝试在 java 应用程序中执行密码查询时,遇到了一个奇怪的问题。result.dumpToString()- 方法向我显示了正确的结果。但是当我尝试迭代时,最后一个节点总是丢失(对于每个执行的查
我不确定为什么会收到此错误...“错误:字段‘config’的类型不完整”。我尝试做前向声明并使用#include 包含 header ...我只是想在 fInstance 中包含 fConfig..
我尝试的一切都给我 Incomplete(Size(1))。我现在最好的猜测是: named!(my_u64(&str) -> u64, map_res!(recognize!(nom::di
我是一名优秀的程序员,十分优秀!