0

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();
}
4

1 回答 1

1

那些行:

character pc;
character saveCharacter;

需要默认构造函数。

usingstd::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标题 - 这是一个坏习惯。

于 2013-06-27T18:59:53.373 回答