これは、リンクされたリストに関する以前の質問に関連しています。リンクされたリストの代わりにベクトルを使用することにしました。エラー C2512: 'character': 行に使用できる適切な既定のコンストラクターがありません: character pc; および文字 saveCharacter; 私はすでにパラメータを持つコンストラクタを持っています。これらをファイルに保存できるようにしたい。デフォルトのコンストラクターは、空白文字列と 2 つのゼロをファイルに保存します。私は何を間違っていますか。私が持っている本では、明確な説明がありません。
//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();
}