So I am trying to print to both the console and a text file using many classes.
class Assignment {
static std::ostream &_rout;
};
//Method to print to a text file
void print(std::ostream& os1, std::ostream& os2, const std::string& str)
{
os1 << str;
os2 << str;
}
std::ofstream file("Game.txt");
std::ostream &Assignment::_rout(file);
Then use that print method anywhere I want to print(cout) in the same class.
But I know I needed to change it up cause I can't do that for multiple classes. So I created a fileoutput class:
fileOutput.h
class fileOutput {
string filename;
static fileOutput *instance;
fileOutput();
public:
static fileOutput *getInstance() {
if (!instance)
{
instance = new fileOutput();
}
return instance;
};
void toPrint(string value);
};
filoutput.cpp
#include <fstream>
#include <cstddef>
#include "fileOutput.h"
fileOutput::fileOutput() {
ofstream myfile;
myfile.open("output.txt", ios::out | ios::app);
}
void fileOutput::toPrint(string value) {
cout << value;
ofstream myfile;
myfile.open("output.txt", ios::out | ios::app);
myfile << value;
myfile.close();
}
When I try to create an instance of fileOuput in my Deck Class and use the toPrint method:
#include "fileOutput.h"
using namespace std;
void Deck::chooseHand() {
fileOutput *print = print->getInstance();
int count = 0;
vector<Card> hand; //Create a new smaller deck(hand)
while (count < MAXHANDSIZE)
{
int cardno;
//This line underneath
print->toPrint("Please select a Card:(1-20) ");
hand.push_back(cardDeck[cardno - 1]);
count++;//increment count
}
curr_size = MAXHANDSIZE;
cardDeck.clear();// Get rid of the rest of the deck
cardDeck = hand; //hand becomes the new deck
}
I seem to get this error:
1>Deck.obj : error LNK2001: unresolved external symbol "private: static class fileOutput * fileOutput::instance" (?instance@fileOutput@@0PEAV1@EA)
1>C:\Users\hamza\Desktop\Assignment (2)\Assignment\x64\Debug\Assignment.exe : fatal error LNK1120: 1 unresolved externals
1>Done building project "Assignment.vcxproj" -- FAILED.