Thread: viewing a text string for a member of a class....

  1. #16
    plzduntlakliekthiskthx
    Join Date
    Oct 2002
    Posts
    138
    I already tried making mobName a string, but i got the same error, + the binary << error.... I tried using strcpy( mobName, currentMob.c_str( ) ); and got this error....

    --------------------Configuration: rpgfight - Win32 Debug--------------------
    Compiling...
    rpgfight.cpp
    C:\PROGRAM FILES\PROGRAMMING\MICROSOFT VISUAL STUDIO\MYPROJECTS\rpgfight\rpgfight.cpp(269) : error C2039: 'cstr' : is not a member of 'cMob'
    C:\PROGRAM FILES\PROGRAMMING\MICROSOFT VISUAL STUDIO\MYPROJECTS\rpgfight\rpgfight.cpp(32) : see declaration of 'cMob'
    Error executing cl.exe.

    rpgfight.exe - 1 error(s), 0 warning(s)

  2. #17
    plzduntlakliekthiskthx
    Join Date
    Oct 2002
    Posts
    138
    It all comes down to this error right here. Whenever I try to put any string in a variable (either using string x, or char x[]) and try to do cout << x; I get this error...

    Code:
    --------------------Configuration: rpgfight - Win32 Debug--------------------
    Compiling...
    rpgfight.cpp
    C:\PROGRAM FILES\PROGRAMMING\MICROSOFT VISUAL STUDIO\MYPROJECTS\rpgfight\rpgfight.cpp(275) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class st
    d::allocator<char> >' (or there is no acceptable conversion)
    Error executing cl.exe.
    
    rpgfight.exe - 1 error(s), 0 warning(s)
    I am using VC++ 6.0, using namespace std; And I have the following include files....
    #include <iostream>
    #include <cstdlib>
    #include <iomanip>
    #include <cstring>
    #include <windows.h> //For Colors....

  3. #18
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Hi,

    It you list an error with your code, it's helpful if you show the code and indicate where the error is occurring with a comment in your code. It seems like an obvious thing to do.

    " error C2039: 'cstr' : is not a member of 'cMob'"

    That seems like a pretty obvious error message,

    strcpy( mobName, currentMob.c_str( ) );

    "c_str" and "cstr" are not the same.
    Last edited by 7stud; 04-13-2003 at 04:02 PM.

  4. #19
    plzduntlakliekthiskthx
    Join Date
    Oct 2002
    Posts
    138
    oh, I am sorry... I thought maybe it was a typo so I tried c_str and cstr.... I am thinking maybe it is my compiler, so I am downloading the SP5 pack right now... I saw someone post a similar message on another board when I searched on google, and in their situation the code worked with someone who has SP5 installed... If anyone has SP5 or another compiler and could compile this for me I would be very grateful!
    Code:
    //rpgfight.cpp
    //An RPG-Style fighting game....
    //If you have any comments or suggestions please email me at [email protected]
    
    #include <iostream>
    #include <cstdlib>
    #include <iomanip>
    #include <cstring>
    #include <windows.h> //For Colors.... 
    
    
    using namespace std;
    
    //declare functions and variables...
    void menu();
    void yourStatus();
    void buyPotions();
    void buyArmor();
    void buyWeapons();
    void fight();
    void usePotion();
    void attack();
    void getName();
    char fightSelect;
    char menuSelect;
    int mobsKilled = 0;
    
    
    
    HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );//This stuff is for colors
    WORD wOldColorAttrs;
    
    //class of monsters
    class cMob
    {
    public:
    	int attack;
    	int itsAttackPower;
    	int itsHP;
    	string itsName;
    	
    };
    cMob currentMob;
    cMob rabbit;
    cMob rat;
    cMob sTroll;
    cMob lTroll;
    cMob dragon;
    cMob boss1;
    
    
    
    
    //class for the user
    class you
    {
    public:
    	int yourHP;
    	
    };
    
    
    
    
    
    //class of weapons
    class cWeapons
    {
    public:
    	int itsDmg;
    
    };
    
    //Use Potion *************************************
    void usePotion()
    {
    	system("CLS");
    }
    
    
    
    //FIGHT ******************************************
    void fight()
    {
    	system("CLS");
    
    	
    	//monsters damages
    	rabbit.itsAttackPower= 1;
    	rat.itsAttackPower = 3;
    	sTroll.itsAttackPower = 6;
    	lTroll.itsAttackPower = 8;
    	dragon.itsAttackPower = 12;
    	boss1.itsAttackPower = 30;
    	//monsters Health
    	rabbit.itsHP = 4;
    	rat.itsHP = 7;
    	sTroll.itsHP = 15;
    	lTroll.itsHP = 25;
    	dragon.itsHP = 40;
    	boss1.itsHP = 80;
    	//monsters name
    	currentMob.itsName;
    	rabbit.itsName = "Rabbit";
    	rat.itsName = "Rat";
    	sTroll.itsName = "Small Troll";
    	lTroll.itsName = "Large Troll";
    	dragon.itsName = "Dragon";
    	boss1.itsName = "The Boss";
    
    	
    	//declare weapons
    	cWeapons fists;
    	cWeapons butterKnife;
        cWeapons dagger;
    	cWeapons shortsword;
    	cWeapons longblade;
    	cWeapons mythrilLongblade;
    	cWeapons bow;
    	cWeapons crossbow;
    	//weapons damages
    	fists.itsDmg = 1;
    	butterKnife.itsDmg = 2; 
    	dagger.itsDmg = 4;
    	shortsword.itsDmg = 8;
    	longblade.itsDmg = 15;
    	mythrilLongblade.itsDmg = 25;
    	bow.itsDmg = 10;
    	crossbow.itsDmg = 10;
    	
    
    	
    	//Statements to determine which monster the user fights....
    	if(mobsKilled < 3)
    	{
    		currentMob = rabbit;
    		
    	}
    	else if(mobsKilled > 3 && mobsKilled < 6)
    	{
    		currentMob=rat;
    	}
    	else if(mobsKilled > 6 && mobsKilled < 9)
    	{
    		currentMob = sTroll;
    
    	}
    	else if(mobsKilled > 9 && mobsKilled < 12)
    	{
    		currentMob = lTroll;
    	}
    	else if(mobsKilled > 12 && mobsKilled < 15)
    	{
    		currentMob = boss1;
    	}
    	
    
    		
    		
    	cout << "A: Attack" << setw(40) << "Monster" << endl
    		<< "P: Use Potion" << setw(40) << "Monster HP:" << endl 
             << "R: Run" << endl << endl
    		 << "Enter the appropriate letter:";
    	cin >> fightSelect;
    	
    	switch(fightSelect)
    	{
    	case 'a': attack();
    		break;
    	case 'A': attack();
    		break;
    	case 'p': usePotion();
    		break;
    	case 'P': usePotion();
    		break;
    	case 'r': menu();
    		break;
    	case 'R': menu();
    		break;
    	default: cout << "Please enter a valid input:";
    		break;
    	};
        
        
    	//(rand()%10);
    
    }
    
    //Buy Weapon ******************************************
    void buyWeapons()
    {
    	system("CLS");
    	cout << "Buy Weapons" << endl << endl;
    }
    
    //Buy Armor ******************************************
    void buyArmor()
    {
    	system("CLS");
    	cout << "Buy Armor" << endl << endl;
    }
    
    //Buy Potions ******************************************
    void buyPotions()
    {
    	system("CLS");
    	cout << "Buy Potions" << endl << endl;
    }
    
    //Your Status ******************************************
    void yourStatus()
    {
    	system("CLS");
    	cout << "Your Status" << endl << endl;
    }
    
    //MENU *******************************************
    void menu()
    {
    	system("CLS");
    	
    
    	
    	SetConsoleTextAttribute ( h, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY ); //Sets the new text color
    	
    	cout << "F: Fight"
    		 << endl;
    	cout << "W: Buy Weapos"
    		 << endl;
    	cout << "A: Buy Armor"
    		 << endl;
    	cout << "P: Buy Potions"
    	     << endl;
    	cout << "S: Your Status"
    		 << endl << endl;
    	cout << "Enter the appropriate letter:";
    	cin  >> menuSelect;
    	switch(menuSelect)
    	{
    		case 'f': fight();
    			break;
    		case 'w': buyWeapons(); 
    			break;
    		case 'a': buyArmor();
    			break;
    		case 'p': buyPotions();
    			break;
    		case 's': yourStatus(); 
    			break;
    		case 'F': fight();
    			break;
    		case 'W': buyWeapons();
    			break;
    		case 'A': buyArmor();
    			break;
    		case 'P': buyPotions();
    			break;
    		case 'S': yourStatus();
    			break;
    		default: cout << "Please enter a valid input:";
    			break;
    	}
    	
    		
    }
    
    //Attack *****************************************
    void attack()
    {
    	         
    	
    	system("CLS"); //Clear screen
    	SetConsoleTextAttribute ( h, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED); //Sets the new text color
    
    	cout << "Select a weapon to use:" << setw(40) << currentMob.itsName << endl << setw(67) << currentMob.itsHP << endl << endl
    		 << "F: Your Fists" << endl
    		 << "B: Butter Knife" << endl
    		 << "D: Dagger" << endl
    		 << "S: Shortsword" << endl
    		 << "L: Longblade" << endl
    		 << "M: Mythril Longblade" << endl
    		 << "B: Bow" << endl
    		 << "C: Crossbow" << endl;
    }
    
    
    
    //MAIN *******************************************
    int main()
    {
    	
    	menu();
    	
    
    	
    	
    
    
    	return 0;
    }

  5. #20
    Registered User
    Join Date
    Feb 2003
    Posts
    162
    VC++ 6 doesn't like the outputting...gives the binary error when outputting....Dev C++ ran it fine when I #include <string>

  6. #21
    plzduntlakliekthiskthx
    Join Date
    Oct 2002
    Posts
    138
    well darn... I hope the service pack helps, otherwise I'll download dev-c++. Thank you all so much for the replies!!!!!!

  7. #22
    Registered User
    Join Date
    Feb 2003
    Posts
    162
    Code:
    void attack()
    {
    	         
    	char b[40];
    	strcpy (b, currentMob.itsName.c_str());
    	system("CLS"); //Clear screen
    	SetConsoleTextAttribute ( h, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED); //Sets the new text color
    
    	cout << "Select a weapon to use:" << setw(40) << b << endl << setw(67) << currentMob.itsHP << endl << endl
    		 << "F: Your Fists" << endl
    		 << "B: Butter Knife" << endl
    		 << "D: Dagger" << endl
    		 << "S: Shortsword" << endl
    		 << "L: Longblade" << endl
    		 << "M: Mythril Longblade" << endl
    		 << "B: Bow" << endl
    		 << "C: Crossbow" << endl;
    }
    compiles fine in VC++

  8. #23
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Actually the reason you were gettin the strcpy error is that what I meant was that you could use mobName as a string, and use the statement mobName = currentMob.itsName;, OR keep mobName as a char array and use strcpy.

    Guess I wasn't clear enough.

    Also, you forgot to include <string>. <cstring> is not the proper header file for std::string.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  9. #24
    plzduntlakliekthiskthx
    Join Date
    Oct 2002
    Posts
    138
    OMG!! I thought cstring and string were the same!! lol I added <string> and it works now... Thank you SO much!!!

  10. #25
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    "it's helpful if you show the code and indicate where the error is occurring with a comment in your code. It seems like an obvious thing to do."

    And when you posted your code, did you do that?
    Last edited by 7stud; 04-13-2003 at 08:15 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  5. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:45 PM