2

I am trying to use a string to hold a player name. when i type

#include <string>

this works correctly, and the VS2010 even autofills the text. Then i try to use a string, but i get an identifier not found:

#include <string.h>

class Player{
    string name;
int playerIndex;
    int position;
public:
    Player(string name, int index, int pos);
    void move();

}; 

On another note, the same thing (or similer is happening with vector)

#include <vector>

vector<cell> vBoard;

error: Vector is not a template

KevinCameron1337
  • 517
  • 2
  • 8
  • 16

2 Answers2

3

Everything in the C++ library is in the namespace std, so that it doesn't pollute the global namespace. You you need to qualify the names:

std::string name;
std::vector<cell> board;

You're also using the wrong header name; you want <string> not <string.h>.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • first thing i tried... didnt work. – KevinCameron1337 Aug 03 '13 at 17:55
  • @Special--k: How didn't it work? Are you including the right header (as I've just added to the answer)? – Mike Seymour Aug 03 '13 at 17:56
  • More specifically, when i type std::string i get the error "name followed by :: must be a class" – KevinCameron1337 Aug 03 '13 at 17:56
  • @Special--k: Well, it should work if you fix both errors, as demonstrated [here](http://ideone.com/dnvMFI). If your compiler can't find `std::string` when you include `` (**not** ``), then there must be something wrong with your installation. – Mike Seymour Aug 03 '13 at 18:00
  • It was the header. i dont know what the difference is between and but that worked. – KevinCameron1337 Aug 03 '13 at 18:02
  • `` is the header file for the legacy C string library (where strings are represented as NULL-terminated arrays of characters and operated on through a collection of functions in the global namespace). `` moves the contents of `` into the `std` namespace, but essentially retains the old functionality. `` is the header file for the C++ string class. –  Aug 03 '13 at 20:23
1

You are missing the namespace

using namespace std;

or try

std::string
rukamir
  • 46
  • 6