PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > String assignment operator
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
String assignment operator

Réponse
 
LinkBack Outils de la discussion
Vieux 22/02/2008, 02h43   #1
InsainFreak101@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut String assignment operator

I'm making a program and everything works except for a couple of
assignment operators for a string object.

here is the snippet of code:

name.assign(theItem.getName);

I've also tried useing the "=" operator, but it doesn't compile.
If anyone has any suggestions, I would appreciate the

Thank you
  Réponse avec citation
Vieux 22/02/2008, 03h30   #2
Kai-Uwe Bux
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: String assignment operator

InsainFreak101@gmail.com wrote:

> I'm making a program and everything works except for a couple of
> assignment operators for a string object.
>
> here is the snippet of code:
>
> name.assign(theItem.getName);
>
> I've also tried useing the "=" operator, but it doesn't compile.
> If anyone has any suggestions, I would appreciate the


Just guessing, since you did not post much code:

name = theItem.getName();


Best

Kai-Uwe Bux

  Réponse avec citation
Vieux 22/02/2008, 04h06   #3
InsainFreak101@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: String assignment operator


if it s, here is the whole class:
//I marked the faulty lines

#include <iostream>

#include <string>

#include <vector>

#include "item.h"

using namespace std;



class inventory

{

public:

void getInventory();

void addItem(item addItem, inventory inventory1);

void subtractItem(item subItem);

bool checkInventory();

void Equip(weapon Item, inventory inventory1);

void Unequip();

private:

vector<item> m_inventory;

vector<weapon> m_equiped;

};

//defining Inventory.getinventory function

void inventory::getInventory()

{

string name;

item theItem;

cout<<"here are your items:"<<endl;

for (int i=0; i<m_inventory.size(); ++i)

{

theItem = m_inventory[i];

name.clear();

name.assign(theItem.getName); //the assignment operator on
this line dosn't work

cout<<name<<endl;

}

cout<<"here are you equiped items:"<<endl;

for (int j=0; j<m_equiped.size(); ++j)

{

theItem = m_inventory[j];

name.clear();

name.assign(theItem.getName); //the assignment operator on
this line dosn't work

cout<<name<<endl;

}

}

//defining Inventory.addInventory function

void inventory::addItem(item addItem, inventory inventory1)

{

if (inventory1.checkInventory())

{

m_inventory.push_back(addItem);

}

else

cout<<"your inventory is full, discard some Items,";

cout<<" please discard some items and try again."<<endl;



}

//defining Inventory.subtractInventory function

void inventory::subtractItem(item subItem)

{

int i=0;

string name;

string compare;

name.assign(subItem.getName); //the assignment operator on this
line dosn't work

compare.assign(m_inventory[0].getName); //the assignment operator
on this line dosn't work

while (name!=compare)

{

++i;

compare.assign(m_inventory[i].getName); //the assignment
operator on this line dosn't work.

}

m_inventory.erase((m_inventory.begin()+i));

}

//defining Inventory.checkInventory function

bool inventory::checkInventory()

{

int i=0;

bool answer;



i = m_inventory.size();



if (i<10)

{

answer = true;

return answer;

}

if (i>=10)

{

answer = false;

return answer;

}

}



void inventory::Equip(weapon Item, inventory inventory1)

{

if (m_equiped.size()>0)

inventory1.Unequip();



m_equiped.push_back(Item);

}



void inventory::Unequip()

{

item Item;

Item = m_equiped[0];

m_equiped.erase((m_equiped.begin()+0));

}

  Réponse avec citation
Vieux 25/02/2008, 11h16   #4
Jim Langston
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: String assignment operator

InsainFreak101@gmail.com wrote:
> I'm making a program and everything works except for a couple of
> assignment operators for a string object.
>
> here is the snippet of code:
>
> name.assign(theItem.getName);
>
> I've also tried useing the "=" operator, but it doesn't compile.
> If anyone has any suggestions, I would appreciate the


Looking at the code you snipped I dont' see any getName other than that
like, but I presume it's supposed to be a fuction. So would be:
name.assign(theItem.getName());
or
name = the Item.getName();

It is hard to say, however, since you are not showing the declaration of the
class item, I just presume it has a method (function) called getName that
either returns a string or a const reference to a string. If it doesn't,
show more code.


--
Jim Langston
tazmaster@rocketmail.com


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 03h13.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,12927 seconds with 12 queries