reference to vector???

Started by
11 comments, last by Eskapade 14 years, 6 months ago
I am trying to teach myself C++ and picked up Beginning C++ Game Programming. I was playing around with the reference to a vector in my quest to make a text based RPG and keep getting [Linker error] undefined reference to `refToElement(std::vector<std::string, std::allocator<std::string> >&, int)' from compiling the following code. I am sure there are better ways to do everything I am doing but right now I am just trying to get a feel for how everything works. I also would like any ideas on taking something out of the "store" once the character buys it. Any help would be appreciated. Thank you!


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;



void displaystr(vector<string>& str);
void displayinv(vector<string>& vec);
     string& reftoinventory(vector<string>& vec, int i);
       string& refToElement(vector<string>& str, int i);
 
     vector<string>::const_iterator iter;
     vector<string>::iterator myiterator;
     
     string shield = "shield            $150";
     string sword;
int main()
{
    
     vector<string> inventory;
     
     vector<string> str;
    
    inventory.push_back(sword);
    str.push_back(shield);
    str.push_back("good sword         $100");
    str.push_back(".boots               $50");
    str.push_back(".hat                 $75");
   string& store1 = refToElement(str,0);
    int gold = 500;
    int buyme;
    int choice;
  do{  
    cout<<"Your gold:  $" <<gold <<endl;
    cout<<"What do you want to do?\n";
    cout<<"1. Look at inventory.\n";
    cout<<"2. See what is for sale.\n";
    cout<<"3. Buy something.\n";
    cout<<"4. Sell something.\n";
    cout<<"5. leave!\n";
    cin>>choice;
    
    switch (choice)
           {
            case 1:
                displayinv(inventory);
                 break;
            case 2:
                displaystr(str);
                 break;            
             case 3:
                   displaystr(str);
                  cout<<"What do you want to buy?\n\n";
                  cin >> buyme;
           
                  if (buyme == 1)    
                                    {
                                    str.erase(str.begin()); 
                                    gold -= 150;
                                    inventory.push_back("shield");
                                    displaystr(str);
                                    break;
                                    }  
                     if (buyme == 2)
                                    {
                                     str.erase(str.begin() +1);
                                     gold -= 100;
                                     inventory.push_back("good sword");                                   
                                    
                                    }
                        if (buyme == 3)
                                      {
                                      str.erase(str.begin() +2);
                                      gold -= 50;
                                      inventory.push_back("boots");
                                      }  
                        }  
            }while (choice !=5);
            cin.ignore(cin.rdbuf()->in_avail() +2);    
 

return 0;
}
  
                 
/*string shop()
{ for (int i=0; i < store.size(); ++i)
      cout << store << endl;}
      */

void displayinv(vector<string>& vec)
{for (iter=vec.begin(); iter != vec.end(); ++iter)
     cout << *iter << endl;}
void displaystr(vector<string>& str)
{for (iter=str.begin(); iter != str.end(); ++iter)
     cout << *iter << endl;}

[Edited by - jpetrie on October 15, 2009 11:42:55 AM]
Advertisement
You have to define refToElement somewhere in your program.
okay I got it now. I did not realize that refToElement was suppose to be a function. Thanks! Any ideas on removing something from the "store"?
First off paste your code in SOURCE tags next time [ source ] [ /source ]

Here is a great site on std guides, specifically look at the STL containers, and at vector for your issue.
okay I will check out that link thank you. How do I post it is source code. Sorry really new to all this
It seems like you don't know the difference between if(a == 1) and if(a = 1), you make the mistake multiple times in your code.

if(buyme = 1) will always be true, because you ASSIGN the value 1 to buyme, then buyme is validated as 1 != 0, therefore true. Same for buyme = 2, 3. Better fix that! :)

Also, no semicolons after if()!

if (buyme= 1);{	str.erase(str.begin());	gold -= 150;	inventory.push_back("shield");	displaystr(str);	break;}


That code is fucked up in too many ways. I think you should read the explanations more carefully and at a slower pace, STL containers are the least of the problem in that code :S
Quote:Original post by jgrudier
okay I got it now. I did not realize that refToElement was suppose to be a function. Thanks! Any ideas on removing something from the "store"?


Take several steps back. You clearly don't understand everything from previous lessons as you ought to. For example:

Quote:I did not realize that refToElement was suppose to be a function.


If you were learning the language properly, you would have known that it was supposed to be a function as soon as you saw the declaration (string& refToElement(vector<string>& str, int i);).
Thank you everyone for your replies and yes I still don't understand a lot of it. I am trying the sink or swim approach right now and I am slowly sinking.
Quote:Original post by Crazyfool
You have to define refToElement somewhere in your program.


very helpful! Thank you

Quote:Original post by Zahlman
If you were learning the language properly, you would have known that it was supposed to be a function as soon as you saw the declaration (string& refToElement(vector<string>& str, int i);).


True

Quote:Original post by Eskapade
It seems like you don't know the difference between if(a == 1) and if(a = 1), you make the mistake multiple times in your code.

if(buyme = 1) will always be true, because you ASSIGN the value 1 to buyme, then buyme is validated as 1 != 0, therefore true. Same for buyme = 2, 3. Better fix that! :)

Also, no semicolons after if()!


Also very helpful!

Quote:Original post by Eskapade
That code is fucked up in too many ways. I think you should read the explanations more carefully and at a slower pace, STL containers are the least of the problem in that code :S


Made me feel very discouraged and want to quit. Was it really necessary to bash me. I have only been trying to learn C++ for a week now and was happy that my code even compiled. I thought this was a forum for beginners and that beginners of any level would be welcome and helped. Which I need a lot of at the moment. I am grateful of any constructive criticism and will not make the same mistakes again that were pointed out but telling me the "code is fucked up in too many ways" does not help me improve. Anyone have any other suggestions as to how to learn I would gladly accept them.

[Edited by - jgrudier on October 15, 2009 12:16:47 PM]
Quote:
That code is fucked up in too many ways. I think you should read the explanations more carefully and at a slower pace, STL containers are the least of the problem in that code :S

This is completely unhelpful. If you are going to be critical, make that criticism constructive. Telling somebody that their work is "fucked up" is flat out inappropriate, and being so harsh without providing any help or advice beyond (more or less) "get better" is extremely out of line for this forum. Don't do it again.

jgrudier: next time please use the "report this post" button at the bottom of his post rather than bringing issues like this up in-thread.

This topic is closed to new replies.

Advertisement