"No match for 'operator<<'" and "Expected primary-expression before '>>' token"

This is a discussion on "No match for 'operator<<'" and "Expected primary-expression before '>>' token" within the C++ Programming forums, part of the General Programming Boards category; In a program that I'm working on, I'm having problems with the 'save' and 'load' functions. Save: Code: int save() ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Location
    Free Country, USA
    Posts
    105

    "No match for 'operator<<'" and "Expected primary-expression before '>>' token"

    In a program that I'm working on, I'm having problems with the 'save' and 'load' functions.

    Save:
    Code:
    int save()
    {
        string filename;
        do
        {
           cout<<"Save game?\n";
           getline( cin, choice);
        } while (tolower(choice[0]) != 'y' && tolower(choice[0]) != 'n');
        if (tolower(choice[0] == 'y'))
        {
                          cout<<"Save filename?\n";
                          getline( cin, filename);
                          filename=filename+".rpg";
                          std::ofstream output(filename.c_str());
                          output <<player.startpoint<<' '<<player.name<<' '<<player.hp<<' '<<player.weapondesig<<' '<<player.attack<<' '<<player.maxhp<<' '<<player.gold<<' '<<player.frags<<' '<<player.defense<<' '<<bow<<' '<<handgrenade<<' '<<broom<<' '<<rope<<' '<<grapple<<' '<<stairboots<<' '<<goldplate<<' '<<goldgoblet<<' '<<goldbowl<<' '<<goldflatware<<' '<<rockbuster<<' '<<bluessword<<' '<<smsand<<' '<<lgsand<<' '<<bacon<<' '<<rbeef<<' '<<bbqpork<<' '<<bbqbeef<<' '<<chicken<<' '<<steak<<' '<<apple<<' '<<pear<<' '<<pineapple<<' '<<peach<<' '<<papaya<<' '<<cheese<<' '<<candy<<' '<<player.beaten<<' '<<broomequip<<' '<<rockbusterequip<<' '<<bluesswordequip<<' '<<bowgive<<' '<<player.taken<<' '<<computer<<' '<<motherboard<<' '<<floppydrive<<' '<<harddisk<<' '<<compucase<<' '<<os<<' '<<rps<<' '<<calculate<<std::endl;
                          output.close();
                          cout<<"Game Saved.\n";
        }
        else
        {
        }
        system("pause");
        invalid=true;
    }
    Error is as follows in title of message.
    Load: (with only significant part of code)
    Code:
    int load()
    {
        string filename;
        cout<<"Load Filename?\n";
        cin.ignore();
        getline( cin, filename);
        filename=filename+".rpg";
        std::ifstream input(filename.c_str());
        input >>player.startpoint>>player.name>>player.hp>>player.weapondesig>>player.attack>>player.maxhp>>player.gold>>player.frags>>>>player.defense>>bow>>handgrenade>>broom>>rope>>grapple>>stairboots>>goldplate>>goldgoblet>>goldbowl>>goldflatware>>rockbuster>>bluessword>>smsand>>lgsand>>bacon>>rbeef>>bbqpork>>bbqbeef>>chicken>>steak>>apple>>pear>>pineapple>>peach>>papaya>>cheese>>candy>>player.beaten>>broomequip>>rockbusterequip>>bluesswordequip>>bowgive>>player.taken>>computer>>motherboard>>floppydrive>>harddisk>>compucase>>os>>rps>>calculate;
        input.close();
        ...
    }
    Error:
    In function 'int load()':
    Expected primary-expression before '>>' token
    Other programs with similar load-save code have not had this problem. All variables in the code are valid.

    This attempted to compile in Dev-C++ on a system running Windows XP SP2.
    Hatman Approves!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Looking for this:
    Code:
    ld>>player.frags>>>>player.d

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Location
    Free Country, USA
    Posts
    105
    Ok, that fixed the load error, and replaced it with:
    No match for 'operator>>'
    followed by a really long incomprehensible function name.
    Hatman Approves!

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    >> followed by a really long incomprehensible function name.

    Post the code for the entire class, and the updated function as well.

    [off topic]
    quzah! Long time no see. How have you been?
    [/off topic]
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  5. #5
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204
    That sometimes means that you're missing a header file, I believe. fstream? string? iostream?

  6. #6
    Registered User
    Join Date
    Nov 2007
    Location
    Free Country, USA
    Posts
    105
    Code:
    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    #include <string.h>
    #include <fstream>
    Those are the header files I'm including, and similar file operation programs compiled without incident.
    The "incomprehensible function name" is also followed by a large amount of suggestions for functions that the compiler thinks I meant. This is for both the 'load' and 'save' functions.
    The lines having errors are the 'output <<' for 'save' and the 'input>>' for 'load.'
    Hatman Approves!

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    But you probably need to show us player itself.

    The likely scenario is that you have something like this:
    Code:
    class Player
    {
    public:
        enum playerenum { one, two, three };
    
        playerenum something;
    };
    
    player p;
    
    cout << p.something;
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Nov 2007
    Location
    Free Country, USA
    Posts
    105
    Player is an object of a class I call character, which inherits from a class lifeform.
    This is the code for all lifeform and all classes that inherit from it.
    Code:
    class lifeform
    {
          public:
                 int hp;
                 int dmg;
                 int block;
                 int attack;
                 int maxhit;
                 int defense;
                 int maxdef;
                 int contact;
                 int choice;
                 weapons weapon;
                 string name;
                 int attackroll()
                 {
                     dmg=(rand()%maxhit)+1;
                 }
                 int defenseroll()
                 {
                     block=(rand()%maxdef)+1;
                 }             
    };
    class character;
    class enemy : public lifeform
    {
          public:
                 enemy(int hitpoint, int atak, int defence, int weak, int tyyp, string enemyname)
                 {
                           hp=hitpoint;
                           attack=atak;
                           defense=defence;
                           weakness=weak;
                           type=tyyp;
                           name=enemyname;
                 }
                 enemy(void);
                 int weakness;//1. Low attack  2. Mid-range  3. High  4. Low+Mid-range  5. Mid-range+High  6. All attacks
                 int dodge;
                 int type;//1:Monster  2:Human
                 int dodgeroll()
                 {
                     dodge=(rand()%maxdef)+1;
                 }
                 int blockroll(character* pchar)
                 {
                     if (block>=pchar->dmg)
                     {
                                  //Roll for initiative!
                                  defenseroll();
                                  if (block>=pchar->dmg)
                                  {
                                                         //Super-Critical!
                                                         cout<<"Your attack fully blocked!\n";
                                                         dmg=pchar->dmg;
                                                         pchar->dmg=0;
                                                         pchar->hp=pchar->hp-dmg;
                                                         cout<<"Your HP-"<<dmg<<"!\n";
                                  }
                                  else
                                  {
                                      cout<<"Your attack partially blocked! Damage reduced!\n";
                                      pchar->dmg=pchar->dmg-block;
                                  }
                     }     
                     else
                     {
                         cout<<""<<name<<"'s Block failed!\n";
                     }
                     hp=hp-pchar->dmg;
                     cout<<""<<name<<"'s HP-"<<pchar->dmg<<"!\n";
                 }
                 int dodgeroll(character* pchar)
                 {
                     if (dodge>=pchar->dmg)
                     {
                                  //Roll for initiative!
                                  dodgeroll();
                                  if (dodge>=pchar->dmg)
                                  {
                                                         //Super-Critical!
                                                         cout<<"Your attack fully dodged!\n";
                                                         pchar->dmg=0;
                                  }
                                  else
                                  {
                                      cout<<"Your attack partially dodged! Damage reduced!\n";
                                      pchar->dmg=pchar->dmg-dodge;
                                  }
                     }     
                     else
                     {
                         cout<<"Dodge failed!\n";
                     }
                     hp=hp-pchar->dmg;
                     cout<<""<<name<<"'s HP-"<<pchar->dmg<<"!\n";
                 }
    };
    
    class character : public lifeform
    {
          public:
                 int taken;
                 int maxhp;
                 int beaten;
                 int gold;
                 int frags;
                 int exp;
                 int startpoint;
                 int weapondesig;
                 int defenseroll(int weaponbonus)
                 {
                     maxdef=weaponbonus+defense;
                     block=(rand()%maxdef)+1;
                 }
                 int attackroll(int weaponbonus)
                 {
                     maxhit=weaponbonus+attack;
                     dmg=(rand()%maxhit)+1;
                 }
                 int blockroll(enemy* pen)
                 {
                     if (block>=pen->dmg)
                     {
                                  //Roll for initiative!
                                  defenseroll(weapon.defensebonus);
                                  if (block>=pen->dmg)
                                  {
                                                         //Super-Critical!
                                                         cout<<""<<pen->name<<"'s attack fully blocked!\n";
                                                         dmg=pen->dmg;
                                                         pen->dmg=0;
                                                         pen->hp=pen->hp-dmg;
                                                         cout<<""<<pen->name<<"'s HP-"<<dmg<<"!\n";
                                  }
                                  else
                                  {
                                      cout<<""<<pen->name<<"'s attack partially blocked! Damage reduced!\n";
                                      pen->dmg=pen->dmg-block;
                                  }
                     }     
                     else
                     {
                         cout<<"Block failed!\n";
                     }
                     hp=hp-pen->dmg;
                     cout<<"HP-"<<pen->dmg<<"!\n";
                 }
    };
    While we're on the subject, I've had problems with the forward declaration of the character class, Dev-C++ complains with:
    101 invalid use of undefined type `struct character'
    78 forward declaration of `struct character'
    Hatman Approves!

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    May I suggest that you do not use the method of misspelling words to make them different, but have a systematic approach of how you name member or argument variables. (e.g. hp = argHp, attack = argAttack or some such).
    Code:
                           hp=hitpoint;
                           attack=atak;
                           defense=defence;
                           weakness=weak;
                           type=tyyp;
                           name=enemyname;
    What type is "bow" in your output list?

    As to the forward declarations:
    If you forward declare a struct, you mustn't use any of the contents within the struct. You can have pointers or references to the struct, but do not USE the content. In this case, it means moving some of your functions out of the class declaration into the main code. Which is probably not a bad idea anyways, as the functions are quite large.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Nov 2007
    Location
    Free Country, USA
    Posts
    105
    The 'bow' is currently a bool, but I intend to actually turn it into a legit 'weapons' object.

    I've gotten a suggestion to place the classes and class properties into their own header files and put the member functions into identically named .cpp source files, then link everything togeter, to help fix the forward declaration problem, as well as clean up my code. (This game's source code has OVER 4000 lines of code, not including whitespace.)
    Hatman Approves!

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21