简体   繁体   中英

C++/CLI and C# singleton

I have a cpp/cli dll that has a DataMiner class. It is a singleton. All of my cpp/cli code is in the DataMine namespace.

public ref class DataMiner
{
private:
    DataMiner();
    void(*stdout_callback) (const char* cb);
    static DataMiner dm_instance;
public:
    static property DataMiner^ Instance { DataMiner^ get() { return %dm_instance; } }
    void setCB(System::IntPtr cb);
    void status_print(std::string s);
    void K_Means(array<float>^ data);
};

I also have a cpp/cli class that I use to do some math:

public ref class KMeans
{
private:
    int _N;
    int _K;
    int* _kMembership;
    float _kSSE;
    float* _sse;
    float* _individualSE;
    float* _data;
    float* _centroids;
    float** _distances;
    KMeans() {};
    //void reformClusters();
public:
    KMeans(float* data, int N, int K);
    KMeans(float* data, float* centroids, int N, int K);
    ~KMeans();
};

On the C# side, I have the following (correctly working) lines:

DataMiner dm = DataMiner.Instance;
dm.setCB(Marshal.GetFunctionPointerForDelegate(MainWindow.Instance.statusWindow.dz));

However, when I try this same line on the cpp/cli side KMeans constructor, it does not work:

KMeans(float* data, int N, int K)
{
    // (some code)
    DataMiner dm = DataMiner.Instance;
    // (some more code)
}

The actual error messages I get are:

DataMine::DataMiner : illegal use of this type as an expression left of '.Instance' must have class/struct/union
'DataMine::DataMiner::DataMiner' : candidate function(s) not accessible

My C++/CLI is a little rusty, but I think you want to use

DataMiner dm = DataMiner::Instance;

Because the :: operator is used for static members.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM