好的,这与我之前关于链接列表的问题有关。我已经开始使用向量而不是链表。错误C2512:'character':没有合适的默认构造函数可用 对于线条: 字符pc; 和 character saveCharacter; 我已经有一个带参数的构造函数。我希望能够将这些保存到文件中。默认构造函数将空字符串和两个零保存到文件中。我究竟做错了什么。我的书并没有给我一个明确的解释。
//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();
}
答案 0 :(得分:1)
这些行:
character pc;
character saveCharacter;
需要默认构造函数。
使用std::vector<character>
也可以添加“默认构造函数”要求(因为当你调整向量大小时,元素可以是默认构造的)。
已经使用参数
创建了构造函数
初始化pc时提供参数
character pc("pc", -1, -1);
character saveCharacter("saveChar", -1, -1);
或者编写默认构造函数。
或者您可以将默认值添加到character
构造函数。
character(string newCharacterName = "noname", int newCharacterDamage = -1, int newCharacterStability = -1)
所有这些选项都很简单。
哦,不要将using namespace std
放入标题中 - 这是一个坏习惯。