Ok this is connected to my previous question about linked lists. I've gone ahead with using a vector instead of a linked list. The error C2512: 'character' : no appropriate default constructor available for the lines: character pc; and character saveCharacter; I already have a constructor with parameters. I want to be able to save these to a file. A default constructor saves a blank string and two zeros to the file. What am I doing wrong. The book I have isn't giving me a clear explanation.
//character.h
#pragma once
#include <string>
#include <vector>
#include <ctime>
using namespace std;
class character
{
public:
character(string newCharacterName, int newCharacterDamage, int newCharacterStability);
~character();
void saveGame();
string characterName;
int characterDamage;
int characterStability;
};
class characterTree
{
public:
characterTree();
const character& getRandomStartPC()
{
srand (time(0));
int index = rand() % characters.size();
return characters[index];
}
void generateStartingPC();
private:
vector<character> characters;
};
//character.cpp
#include "character.h"
#include <iostream>
#include <fstream>
#include <ctime>
using namespace std;
void genStartPC();
void runMainMenu();
character pc;
character saveCharacter;
characterTree playerCharacters;
character::character(string newCharacterName, int newCharacterDamage, int newCharacterStability)
: characterName(newCharacterName), characterDamage(newCharacterDamage), characterStability(newCharacterStability)
{
};
character::~character()
{
};
characterTree::characterTree()
{
character character1("a small rusty axe", 2, 4);
characters.push_back(character1);
character character2("a broken table leg", 2, 4);
characters.push_back(character2);
character character3("a cracked tarnised walking stick", 2, 4);
characters.push_back(character3);
character character4("a blunted mace with missing spikes", 2, 4);
characters.push_back(character4);
character character5("a dented tin cup", 2, 4);
characters.push_back(character5);
character character6("a smelly old shoe", 2, 4);
characters.push_back(character6);
character character7("a length of rope", 2, 4);
characters.push_back(character7);
character character8("a broken wine bottle", 2, 4);
characters.push_back(character8);
character character9("a dented candlestick", 2, 4);
characters.push_back(character9);
character character10("a brunt frying pan", 2, 4);
characters.push_back(character10);
}
void characterTree::generateStartingPC()
{
const character & character = playerCharacters.getRandomStartPC();
cout << "\tYou are trapped within " << character.characterName << "." << endl;
cout << "\tIt has a damage rating of " <<
character.characterDamage << " and a stability rating of " <<
character.characterStability << ".";
cout << endl;
}
void character::saveGame()
{
cout << "\tSave code goes here." << endl;
fstream saveFile;
cout << endl;
saveFile.open("save.dat");
if (saveFile.is_open())
for (int i = 0; i < 1; i++)
{
saveFile << saveCharacter.characterName << endl;
saveFile << saveCharacter.characterDamage<< endl;
saveFile << saveCharacter.characterStability << endl;
cout << " Game Saved." << endl;
saveFile.close();
}
else
cout << " Error. Unable to open file.";
runMainMenu();
}
void genStartPC()
{
playerCharacters.generateStartingPC();
}
void saveThisGame()
{
pc.saveGame();
}