سؤال

So I've been working on my what I thought would be quick and easy project for a couple hours now and I can't get it to work! It's making me frustrated lol I've got to be close, but maybe I'm not.

I'll include my code with comments explaining what it should be doing. Essentially its using a private constructor and destructor. A member integer and then a public static function to return a reference to an object in the class - and a public function that should display the member integer and increment it. I keep getting scope and initialization errors.

HERES THE ERRORS:

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

Any help or suggestions will be greatly appreciated!

Heres my code (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;
}

main.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;
}

Thought/Suggestions/Questions/Concerns? Anything will help relieve the frustration this has been causing me lol. It seems too simple to be causing me such a problem. Thanks!

هل كانت مفيدة؟

المحلول

Forbidden in that scope:

Singleton mySingletonObj;

Should be OK:

Singleton& sRef = Singleton::instance();

References are not reassignable:

sRef = Singleton::instance();

Should be OK:

sRef.sendOutput();

Also, remove this -- the language already specifies the lifetime and storage of the object, and takes care of destructing it:

delete myObj;

You should also delete the ability to copy the type:

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

نصائح أخرى

Just remove delete myObj; here:

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

and it should work. myObj is statically allocated so will be deleted by runtime on process exit.

First of all you should understand basics of the language before yo start writing programs on it. From your code looks like you try to delete myObj, which is local static object from another method and not visible in destructor, not to mention that you can call delete only on pointers and should delete only pointers initialized by operator new. Also your singleton should have private or deleted (for C++11) copy constructor and assignment operator. If you do that line: "sRef = Singleton::instance();" will not compile (and it should not logically). Other than that everything is fine.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top