3

I am making a text based adventure game called Magick.
In this game I have a class labeled damageSpell it looks like this

class damageSpell { 
    public:
        int damage;
        SubClasses type;
        int manaCost;
        std::string spellDescription; 
};

I used this class as the type for a vector like so

std::vector<damageSpell> damageSpells

Later on, I attempted to add an element into my damageSpells vector, by using the insert function on vector.

damageSpell fireball;

user.damageSpells.insert(user.damageSpells.begin(), 0, fireball);

Then attempted to print it out

std::cout << user.damageSpells[0];

Upon doing this I received this error

magick1.cpp:252:15: error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream}’ and ‘damageSpell’)

I am new to C++ and have no idea what this means or how I should go about fixing it, any and all help will be appreciated.

Aracthor
  • 5,757
  • 6
  • 31
  • 59
  • [Operator Overloading](http://stackoverflow.com/questions/4421706/operator-overloading), will be informative, particularly the selected answer's section on "Bitshift Operators". – WhozCraig Oct 22 '16 at 06:54
  • Output with the `<<` operator is not something that happens automatically. Instead there are special overloads of the `<<` operator that handles the output for each standard type. If you want to output your own structure with the output operator you need to write your own custom `operator<<` function. Just about all [good beginners books](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) will teach you how to do it. – Some programmer dude Oct 22 '16 at 06:55
  • Do you think putting something in a vector makes it unprintable? Maybe forget the vector and try printing a `damageSpell` object? – juanchopanza Oct 22 '16 at 08:53

2 Answers2

3

user.damageSpells[0] is an instance of your class spellDamage. And your compiler doesn't have any idea of how to print it. You have to define a function that will be called on << operator and will print it out.

This operator overloading can be defined like this:

std::ostream& operator<<(std::ostream& stream, const damageSpell& damageSpellToPrint)
{
    // Your code here
}

It will be called eachtime you use << operator between a std::ostream (like std::cout) and an instance of your class. For instance, the following code will directly call your operator function, passing std::cout as stream parameter and user.damageSpells[0] as damageSpellToPrint parameter:

std::cout << user.damageSpells[0];

I suggest you this post or this documentation that will help you to understand operator overloading concepts in C++.

Community
  • 1
  • 1
Aracthor
  • 5,757
  • 6
  • 31
  • 59
  • Okay so I'm starting to get this, I understand that because its a class I created, I have to make my own operator, but once its made...how would I access it? What code would I use once its done to use the new operator – Alice The Hatter Oct 22 '16 at 07:07
  • @Alice Your operator will be called each time you use `<<` between any `std::ostream` and an instance of your class. – Aracthor Oct 22 '16 at 07:10
  • Okay..getting it. So what code would go inside of that function above though? Or what kind of code, rather. – Alice The Hatter Oct 22 '16 at 07:12
  • @Alice I cannot answer that directly, as I don't know how you want to print your class. Just use the `stream` parameter as `std::cout`, and then define how you want to print a spell. For instance if you want to print damage, manacost and description, all separated by a comma, use `stream << damageSpellToPrint.damage << ", " << damageSpellToPrint.manaCost << ", " << damageSpellToPrint.spellDescription;`. Just don't forget to return `stream` at the end of the function. – Aracthor Oct 22 '16 at 07:16
  • Okay, done everything you've said, seems like I understand it perfectly...but... what would be the syntax of using my new operator. Would it be something like `std::ostream << user.damageSpells[0];` – Alice The Hatter Oct 22 '16 at 07:23
  • @Alice If you still have questions, you didn't understand it perfectly... And as I said, there is no special syntax of calling it. As `std::cout` is an instance of `std::ostream`, a simple `std::cout << user.damageSpells[0]` will call your operator if it has the good prototype. – Aracthor Oct 22 '16 at 07:26
  • I understand i now. Thank you – Alice The Hatter Oct 22 '16 at 07:30
0

the thing your doing is printing entire object, but << operator is not defined to print any object, that you have to do yourself. try this, std::cout<<user.damageSpells[0].damage<<user.damageSpells[0].manaCost

and so on...... and for subclass too