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();
}
user2114611
  • 23
  • 1
  • 4
  • 4
    Well, yes, you need a default constructor in order to do `character pc;`. – chris Jun 27 '13 at 18:49
  • possible duplicate of [At what condition is the default constructor generated?](http://stackoverflow.com/questions/10638527/at-what-condition-is-the-default-constructor-generated) – Some programmer dude Jun 27 '13 at 18:52
  • Your code looks fine to me, and it compiles without problems on gcc 4.2.1. Try posting some code which reproduces the error you're seeing. – Frerich Raabe Jun 27 '13 at 18:52
  • 1
    @FrerichRaabe, Whatever bug is in GCC 4.2.1, it's been fixed. `character` has no default constructor. – chris Jun 27 '13 at 18:54
  • @chris: Why would `character` need a default constructor? Certainly not for `std::vector`, because it [doesn't need it](http://stackoverflow.com/questions/2376989/why-dont-stdvectors-elements-need-a-default-constructor). Argh, ignore me - I didn't scroll down! I didn't even realize that you could scroll the code snippet... – Frerich Raabe Jun 27 '13 at 18:57
  • @FrerichRaabe: Read this line `character pc;` and think about it. – SigTerm Jun 27 '13 at 19:00
  • @SigTerm: Yes of course *that* requires a default ctor, but you don't see that on my screen. This is what I see: http://postimg.org/image/6bwgi0xp5/ - note how there are no scroll bars, this must be some OS X feature I wasn't aware of. The scroll bars only appear once I start scrolling (and disappear when I stop scrolling) - and the snippet I see seems reasonably complete and it compiles. :-} – Frerich Raabe Jun 27 '13 at 19:03
  • @FrerichRaabe: This might be a bug. If this is stackoverflow bug, and not an OSX feature, you might want to report it. Just saying. – SigTerm Jun 27 '13 at 19:06
  • @SigTerm: I just checked, I have this on all web pages (and even in both Chrome and Safari). I guess it just shows how much experience I have with OS X... usually I do notice that I can scroll because the bottom-most line is truncated, but in this case: it just fit. :-} – Frerich Raabe Jun 27 '13 at 19:17
  • @FrerichRaabe, You're forgiven for coming to that conclusion, though notice the obvious lack of horizontal one with a cutoff. – chris Jun 27 '13 at 19:17
  • @chris: Indeed, I didn't notice that. I just did Ctrl+A, pasted it into vim and ran it - because I couldn't spot a reason for why a default constructor should be required. How embarrasing... :-] – Frerich Raabe Jun 27 '13 at 19:19

1 Answers1

1

Those lines:

character pc;
character saveCharacter;

Require default constructor.

using std::vector<character> can also add "default constructor" requirement (because when you resize vector, elements can be default-constructed).

already made constructor with parameters

Either provide parameters when initializing pc

character pc("pc", -1, -1);
character saveCharacter("saveChar", -1, -1);

Or write default constructor. Or you could add default values to character constructor.

character(string newCharacterName = "noname", int newCharacterDamage = -1, int newCharacterStability = -1)

All those options are easy enough.

Oh, and don't put using namespace std into headers - it is a bad habit.

SigTerm
  • 26,089
  • 6
  • 66
  • 115
  • Adding default values to the character constructor still produces the error that a default constructor is required. Adding parameters to character pc and savecharacter just send those parameters to the file and not the parameters generated when generateStartingPC is run. Creating a default constructor just sends an empty string and two zeros to the file. – user2114611 Jun 28 '13 at 06:16
  • @user2114611:Look I won't fix your code for you. "Creating a default constructor just sends" Default constructor just constructs variable and doesn't send anything anywhere. If your code doesn't behave the way you want, it means there's a bug and you should fix it. Because you don't change values of `pc` anywhere, then of course your program will save default value to file when you call `saveThisGame`. If you're just started learning C++ and are struggling with this problem, try something simpler. – SigTerm Jun 28 '13 at 10:31
  • I never said fix my code for me and yes I am going to try something simpler. I've had some success with setAll getAll functions. I was looking for guidance about what I was doing wrong not have someone do it for me. – user2114611 Jun 28 '13 at 21:27