Jump to content
  • Advertisement
Sign in to follow this  
jgrudier

reference to vector???

This topic is 3169 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
Advertisement
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"?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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);).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!