【问题标题】:How do I use Static Objects and Methods!? C++ Frustration如何使用静态对象和方法!? C++ 挫折
【发布时间】:2013-02-22 23:33:48
【问题描述】:

因此,我已经做了几个小时的我认为会快速简单的项目,但我无法让它工作!这让我很沮丧,哈哈,我必须接近,但也许我不是。

我将在 cmets 中包含我的代码,解释它应该做什么。本质上它使用私有构造函数和析构函数。一个成员整数,然后是一个公共静态函数,用于返回对类中对象的引用 - 以及一个应该显示成员整数并递增它的公共函数。我不断收到范围和初始化错误。

这里有错误:

Singleton.h: In function ‘int main(int, char**)’:
Singleton.h:28:2: error: ‘Singleton::Singleton()’ is private
main.cpp:38:12: error: within this context
Singleton.h:29:2: error: ‘Singleton::~Singleton()’ is private
main.cpp:38:12: error: within this context

任何帮助或建议将不胜感激!

这是我的代码(Singleton.h、Singleton.cpp、main.cpp):

Singleton.h:

#ifndef SINGLETON_H
#define SINGLETON_H
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>

class Singleton {

public:
    static Singleton& instance(); //returns a reference to a Singleton obj
    void sendOutput(); //prints member variable and increments it
private:
    int myInt; //member integer-variable
    Singleton(); //constructor
    ~Singleton(); //destructor

};
#endif

Singleton.cpp:

#include <string>
#include "Singleton.h"

using namespace std;

//Displays to console that the obj was created. Initializes the member variable
Singleton::Singleton(){
    myInt = 0; //member variable
    cout << "Singleton Object Created!" << endl;
}

//destructor - displays to console that the Singleton object was destructed
Singleton::~Singleton(){
   // delete myObj;
    cout << "Singleton Object Destructed!" << endl;
}

//display member variable value and increment
void Singleton::sendOutput(){
    cout << "Request to send data to output to console" << endl;
    cout << "Integer Value: " << myInt << endl;
    myInt++;
}

//REQUIRED: Static method with a reference to an object of this class return type
//create a static Singleton object and return it
Singleton& Singleton::instance(){
    static Singleton myObj;
    return myObj;
}

ma​​in.cpp:

#include "Singleton.h"
#include <string>
#include <iostream>
#include <cstdlib>

using namespace std;

//Another requirement - demo static variables
void static_test(){
    static int a = 1;
    cout << a << endl;
    a++;
}

int main(int argc, char * argv[]){

    static_test();
    static_test();
    static_test();


//messed around with this for awhile - got it to work a few times
//messed it up a few more times O.o 

    Singleton mySingletonObj;
    Singleton& sRef = Singleton::instance();
    //sRef = Singleton::instance();
    sRef.sendOutput();

    return 0;
}

想法/建议/问题/疑虑? 任何事情都将有助于减轻这给我带来的挫败感,哈哈。给我造成这样的问题似乎太简单了。谢谢!

【问题讨论】:

  • myObj 在哪里定义? - 哦,我明白了。不,不要那样做。你不能在静态对象上调用 delete,否则会出现混乱......
  • 我很确定 c-smile 对您的问题是正确的,但总的来说,请输入您得到的实际错误。
  • 我摆脱了 delete myObj;我编辑了我的问题以添加我收到的错误。
  • 为什么需要单例?只需创建一个对象并将其传递到需要的地方。

标签: c++ static static-methods object-reference private-constructor


【解决方案1】:

在该范围内禁止:

单例 mySingletonObj;

应该没问题:

Singleton& sRef = Singleton::instance();

引用不可重新分配:

sRef = Singleton::instance();

应该没问题:

sRef.sendOutput();

另外,删除这个——语言已经指定了对象的生命周期和存储,并负责销毁它:

删除我的对象;

你还应该delete复制类型的能力:

Singleton(); //constructor
Singleton(const Singleton&) = delete; // deleted copy constructor

【讨论】:

    【解决方案2】:

    只需在此处删除delete myObj;

    Singleton::~Singleton(){
        delete myObj;
        cout << "Singleton Object Destructed!" << endl;
    }
    

    它应该可以工作。 myObj 是静态分配的,因此将在进程退出时被运行时删除。

    【讨论】:

    • 我删除了删除 - 这是我的错误:Singleton.h: In function 'int main(int, char**)': Singleton.h:28:2: error: 'Singleton::Singleton ()' 是私有 main.cpp:38:12:错误:在此上下文中 Singleton.h:29:2:错误:'Singleton::~Singleton()' 是私有 main.cpp:38:12:错误:在这个上下文
    【解决方案3】:

    在开始编写程序之前,您首先应该了解该语言的基础知识。从您的代码看起来您​​尝试删除 myObj,它是来自另一个方法的本地静态对象,在析构函数中不可见,更不用说您只能在指针上调用 delete,并且应该只删除由 operator new 初始化的指针。 此外,您的单例应该具有私有或已删除(对于 C++11)复制构造函数和赋值运算符。如果你这样做:“sRef = Singleton::instance();”不会编译(而且它不应该在逻辑上)。除此之外一切都很好。

    【讨论】:

      猜你喜欢
      • 2013-07-18
      • 1970-01-01
      • 1970-01-01
      • 2016-09-13
      • 1970-01-01
      • 2013-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多