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