相关疑难解决方法(0)

C++ Singleton实现 - 静态问题

自从我用C++编程以来已经有一段时间了.我试图实现一个单例类,但我得到一个未解析的外部符号.你们能指出解决这个问题吗?提前致谢!

class Singleton
{
    Singleton(){}
    Singleton(const Singleton & o){}
    static Singleton * theInstance;

public:
    static Singleton getInstance()
    {
        if(!theInstance)
            Singleton::theInstance = new Singleton();

        return * theInstance;
    }
};
Run Code Online (Sandbox Code Playgroud)

错误:

错误3错误LNK1120:1个未解析的外部

错误2错误LNK2001:未解析的外部符号 "private: static class Singleton * Singleton::theInstance" (?theInstance@Singleton@@0PAV1@A)

c++ linker singleton static linker-errors

1
推荐指数
1
解决办法
5921
查看次数

C ++单例和新运算符

嗨,我有一个代码片段,它是一个单例类(我相信),它以以前从未见过的方式使用'new'和其他一些作用域和静态限定符。因为我不知道它叫什么,所以找不到有关它的用途或目的的任何信息。代码如下:

在myClass.h文件中

class myClass
{
private:
    static myClass sm_myClass;
    myClass();
public:
    static void Create(void);
};
Run Code Online (Sandbox Code Playgroud)

在myClass.cpp中,我有

#include "myClass.h"
#include <new>

myClass* p_myClass = NULL;

myClass myClass::sm_myClass;

myClass::myClass()
{
}

void myClass::Create(void)
{
    p_myClass = &sm_myClass;

    new (p_myClass) myClass();
}
Run Code Online (Sandbox Code Playgroud)

我的问题是:

1-.cpp文件的第6行(myClass myClass :: sm_myClass;)发生了什么

2-.cpp文件的第16行(new(p_myClass)myClass();)发生了什么

3-以这种方式使用类的总体目的是什么?

我真的希望这个问题不再重复。我搜索了一段时间,但不知道要搜索什么词。谢谢你的帮助。

c++ singleton scope class

1
推荐指数
1
解决办法
461
查看次数

C++ 单例延迟初始化实现和链接似乎冲突

C++ 单例设计模式我遇到这个问题并了解到在 C++ 中有两种实现单例模式的方法。

1)在堆中分配单个实例并在instance()调用中返回它

2)在instance()调用中返回一个静态实例,这也称为延迟初始化实现。

但我认为第二个,即延迟初始化实现,是错误的,原因如下。从instance()调用返回的静态对象具有内部链接,并且在不同的翻译单元中具有唯一的副本。因此,如果用户修改单例,它不会反映在任何其他翻译单元中。

但有很多说法表明第二种实现是正确的,我是否遗漏了什么?

c++ singleton linkage

1
推荐指数
1
解决办法
808
查看次数

在 C++ 中调用单例对象的正确方法

我已经根据此处发布的命中构建了一个单例类。

我用一个getMessage()函数扩展了它,该函数将检索内部字典消息 - 字典只需要在整个应用程序上加载一次,这就是单例的原因。

我的代码:

单例.hpp

class Singleton {

public:

    static Singleton& getInstance();
    std::string getMessage(std::string code);

private:

    Singleton() {}; 
    Singleton(Singleton const&) = delete;
    void operator=(Singleton const&) = delete;
};
Run Code Online (Sandbox Code Playgroud)

单例.cpp

Singleton& Singleton::getInstance()
{
    static Singleton instance;
    return instance;
}


std::string Singleton::getMessage(std::string code)
{
    /// Do something
    return "Code example.";
}
Run Code Online (Sandbox Code Playgroud)

和主要代码:

主程序

int main()
{
        Singleton* my_singleton;
        my_singleton = Singleton::getInstance(); **<-- ERROR HERE**

        cout << my_singleton->getMessage("a"); << endl

}
Run Code Online (Sandbox Code Playgroud)

主要是给我一个错误: Cannot convert 'Singleton' to 'Singleton*' in …

c++ singleton

0
推荐指数
1
解决办法
3472
查看次数

为什么析构函数不接受C++中的参数?

最近,我遇到了一个示例,我的析构函数需要一个参数.

我正在开发一个内部管理内存的C包,并跟踪自己的参数分配和释放.我不想打破这个.

我编写了一个C代码,它初始化了我自己的数据结构并在最后释放它们.当我决定迁移到C++时,我意识到分配数据结构并释放它们应该放在构造函数和析构函数中.所以我将这些函数更改为构造函数和析构函数.我现在的问题是我需要使用一个参数传递给析构函数以释放分配的数据; 该参数不在我自己的代码中,而且是C,我不想搞砸.

我的问题是双重的:

为什么C++首先决定不接受析构函数中的参数?我有什么选择?(我可以保存一个指向该参数的指针,或者以某种方式在我的析构函数中调用该函数,但它似乎不是C++编程的好习惯)

更新:添加一些代码这是我想象的类:

class paru_symbolic/* paru_symbolic*/
{
    public:
        paru_symbolic ( cholmod_sparse *A, cholmod_common *cc ); // constructor
        ~paru_symbolic (cholmod_common *cc ); // destructor

        // -------------------------------------------------------------------------
        // row-form of the input matrix and its permutations
        // -----------------------------------------------------------------------
              //--- other stuff
                     ...
};
Run Code Online (Sandbox Code Playgroud)

这是我当前的C构造函数:

#include "Parallel_LU.hpp"
paru_symbolic *paru_sym_analyse
(
 // inputs, not modified
   cholmod_sparse *A,
 // workspace and parameters
   cholmod_common *cc ){   

    DEBUGLEVEL(0);
...
   aParent = (Int*) paru_alloc (m+nf, sizeof(Int),cc);

...
}
Run Code Online (Sandbox Code Playgroud)

和析构函数:

void paru_freesym (paru_symbolic **LUsym_handle,
            // …
Run Code Online (Sandbox Code Playgroud)

c++ memory-management

0
推荐指数
1
解决办法
575
查看次数

c ++ 11 Singleton正在创建新实例

这是我的单身人士

class MySingleton {
private:
    int myNumber;
public:
    static MySingleton &get() {
        static MySingleton instance;
        return instance;
    }

    int getMyNumber() const {
        return myNumber;
    }

    void setMyNumber(int myNumber) {
        MySingleton::myNumber = myNumber;
    }
};
Run Code Online (Sandbox Code Playgroud)

这是测试它的代码

MySingleton t1 = MySingleton::get();
t1.setMyNumber(6);
printf("t1: %d \n", t1.getMyNumber());

MySingleton t2 = MySingleton::get();
printf("t2: %d \n", t2.getMyNumber());
Run Code Online (Sandbox Code Playgroud)

现在我得到的输出是

t1: 6 
t2: 0 
Run Code Online (Sandbox Code Playgroud)

但预计结果将是

t1: 6 
t2: 6 
Run Code Online (Sandbox Code Playgroud)

它看起来像是MySingleton::get()在创建一个新实例,当然它不应该做.

c++ c++11

0
推荐指数
1
解决办法
114
查看次数

从类成员返回智能指针的正确方法?

我正在尝试在person类中编写单例模式,这使我能够为该类创建一个实例,并且我可以在我的程序中的任何位置使用它.

以下是班级:

// The declaration
class Person {
    static unique_ptr<Person> instance;
    Person() = default;
    Person(Person&) = delete;
    Person& operator=(const Person&) = delete;
    ~Person() = default;
public:
    static unique_ptr<Person> getInstance();
};

// The implementation   
unique_ptr<Person> instance = NULL;
unique_ptr<Person> Person::getInstance() {
    if (instance == NULL) {
        instance = unique_ptr<Person>(new Person());
    }
    return instance;
}
Run Code Online (Sandbox Code Playgroud)

但它给我这个错误的问题: Error C2280 'std::unique_ptr<Person,std::default_delete<_Ty>>::unique_ptr(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)': attempting to reference a deleted function

不幸的是,我不明白这个问题,我不知道如何解决?

c++ singleton smart-pointers c++11

0
推荐指数
1
解决办法
249
查看次数

从具有正向声明的类中向具有朋友功能的已声明单身类转发

我希望这是一个连贯的问题...我有一个单例类定义,例如:

#include A.h

class Singleton
{
public:
   // If I change this to a return by pointer, it works fine. But reference gives me problems.
   static Singleton &getInstance();
   Singleton(Singleton const&) = delete;
   void operator=(Singleton const&) = delete;
   ~Singleton();

   friend void A::friendMethodinA();
private:
   Singleton();
   void methodOnlyNeededByA();
}
Run Code Online (Sandbox Code Playgroud)

类的定义是:

Singleton &Singleton::getInstance()
{
   static Singleton instance;
   return instance;
}

Singleton::~Singleton() {}

Singleton::Singleton() {}

void Singleton::methodOnlyNeededByA() { // method body. }
Run Code Online (Sandbox Code Playgroud)

我的A类声明是:

#pragma once

class Singleton;

class A
{
public:
   void friendMethodinA();
private:
   // …
Run Code Online (Sandbox Code Playgroud)

c++ singleton return friend-function c++11

0
推荐指数
1
解决办法
62
查看次数