Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 使用ref类时发生C2512错误_.net_C++ Cli - Fatal编程技术网

.net 使用ref类时发生C2512错误

.net 使用ref类时发生C2512错误,.net,c++-cli,.net,C++ Cli,这肯定是一个已经出现了很多-但我认为这个版本是有点不同。我的代码中出现以下错误 错误C2512:“数据类型::日期时间”:没有合适的默认构造函数可用。此错误最近在从.NET 2升级到.NET 4.6的过程中出现 本质上,我们有一个参考类,如下所示: public ref class DateTime : DataType { public: DateTime(); //just highlighting that the constructor is av

这肯定是一个已经出现了很多-但我认为这个版本是有点不同。我的代码中出现以下错误
错误C2512:“数据类型::日期时间”:没有合适的默认构造函数可用
。此错误最近在从.NET 2升级到.NET 4.6的过程中出现

本质上,我们有一个参考类,如下所示:

public ref class DateTime : DataType {
    public:
        DateTime();
        //just highlighting that the constructor is available in the class and hasn't been missed
}
此类继承自具有静态构造函数的接口
数据类型
,如下所示:

public interface class DataType {
    static DataType() {
    }
}
然后这些都被绑定到另一个类中,这就是我们得到错误的地方

public ref class DateCounter {
    static DateTime dateTime;
}
现在-我已经用以下方法修复了错误

public ref class DateCounter {
    static DateTime dateTime = new DateTime();
}
这看起来是在强迫它使用这个构造函数——但是由于这种设置在应用程序中使用得很多,所以要完成所有这些并修改它们是一项艰巨的任务

我只是想知道是否有人知道一个更优雅的解决方案,或者至少可以给出一个原因,说明为什么在.NET的两个版本之间会发生变化

编辑- 所以我建立了一个小项目,看起来它也在做同样的事情。项目中有三个文件- TestClass.h:

public ref class TestClass : TestInterface {
    protected:
        static int staticItem = 0;
        int holdInt;
    public:
        virtual void Clear();
        TestClass();
        static TestClass();
        TestClass(int takeIn);
        TestClass(TestClass% value);
        TestClass% operator= (TestClass% input);
};
TestClassMethods.h:

TestClass::TestClass() {

}

TestClass::TestClass(int takeIn) {
    this->holdInt = takeIn;
}

TestClass::TestClass(TestClass% value) {
    *this = value;
}

void TestClass::Clear() {
    this->holdInt = 0;
}

TestClass% TestClass::operator= (TestClass% toAssign) {
    this->holdInt = toAssign.holdInt;

    return *this;
}
和ClassLibrary1.cpp

namespace TestNamespace {
    ref class TestClass;

    public interface class TestInterface {
        void Clear();
    };

    public ref class Counter {
        static TestClass counterVariable;
    };
}

这些复制了我正在处理的应用程序中代码定义的设置方式,应该会产生问题

我认为这里的问题是调用DateTime的默认(无参数)析构函数,这是一种值类型。值类型的默认构造函数是隐式定义的,并将此类型的实例设置为其默认值

因此,要消除此错误消息,最简单的解决方法是使用一个参数化构造函数,例如“new DateTime(0)”,它生成与默认构造函数相同的值。另一个值得尝试的选项是使用C++/CLI的特殊“堆栈语义”语法,它透明地完成所有构造/销毁工作。为此,将DeaTime实例声明为一个本地C++变量:

DateTime vDateTime;
请注意,在这种情况下不能指定空括号。C++编译器将把它识别为默认构造函数调用。参数化声明如下所示-现在需要括号:

DateTime vDateTime (0);

这不是C++。它看起来像微软的C++或CX或其他东西(重新命名的C++ + CLI?)。是的,这是正确的——它是C++ CLI——我将更新TAGI有一个类似的概念,但是我被告知CLR不支持静态接口定义。对于类来说,实现任何静态构造函数都没有真正的约束。但您可能只需要这样:
staticdatetime^DateTime或搜索“值类型”()。暂时取消删除我的类似问题:。下面有一些有用的注释。在修复之前,您的示例在.NET4.6中运行良好。你能举例说明这个问题吗?