0

I am trying to make a Client-Server program that would let me write to someone. Everything worked right when I made it server-side. Now that I want to implement the same function I made for the server-side in the Client's, Visual Studio says I can't because he doesn't recognize 'string'. I tried with and without the includes of string, but it won't work either way.

Here's what the code look like for the Client :

#pragma once
//#include <winSock2.h>
//#include <Windows.h>
#include "ClientNetwork.h"
#include "NetworkData.h"
#include <string>
#include <string.h>

class ClientGame
{
public:
    ClientGame();
    ~ClientGame(void);

    ClientNetwork* network;

    void sendActionPackets();

    char network_data[MAX_PACKET_SIZE];

    void update();

    void sendMessagePacket(string message);
};

Visual Studio gives me "syntax error : identifier 'string'. Yet, I clearly included it in all the ways possible up there.

Also, I have the same code in the same project but for the Server-side of my program. Here it is :

#pragma once
#include "ServerNetwork.h"
#include "NetworkData.h"

class ServerGame
{
public:
    ServerGame(void);
    ~ServerGame(void);

    void update();

    void receiveFromClients();

    void sendActionPackets();

    void sendMessagePacket(string message);

private:

    //IDs for the clients connection for table in ServerNetwork
    static unsigned int client_id;

    //The ServerNetwork object
    ServerNetwork* network;

    //data buffer
    char network_data[MAX_PACKET_SIZE];
};

And it works even without the #include string / string.h. Does someone know why it does that?

**EDIT I should've specified, I did try to scope it into std this way "std::string", too. But yet, it won't work.

**Looks like it's "Solved" Removing the "string.h" include but keeping the simple "string" and adding the "std::" scope solved it. I'm still pretty confused as to why there is no problem in my server-sided version. I didn't have to include nor to scope it.. Anyway, I should be alright! Thnanks! :)

ECHO
  • 13
  • 2
  • 4
    Left out the namespace. It is `std::string`. – user4581301 Jul 28 '15 at 16:56
  • Rather than specifying `std::string` over and over, and if you can be certain you won't have your own string hiding somewhere, you can save your self a bit of trouble and stick `using std::string;` up at the top of the CPP files under the include statements. Do not put it above the include statements because god only knows what could break if some header already uses string. – user4581301 Jul 28 '15 at 16:59
  • I should've specified, I did try to scope it into std this way "std::string", too. But yet, it won't work :( – ECHO Jul 28 '15 at 17:01
  • @ECHO First, don't `#include `. Your file uses no functions that requires this header. Second, are you showing us a real compiler error, or an "Intellisense" error? Compiler errors from Visual Studio have a `Cxxxx` number associated with them, and not just text. – PaulMcKenzie Jul 28 '15 at 17:07
  • @PaulMcKenzie Here's the error : ...\clientgame.h(23): error C2039: 'string' : is not a member of 'std' – ECHO Jul 28 '15 at 17:09
  • Time to dig a little deeper. Have you used this install of Visual studio before? If not, make a new project that does nothing but declare a string and see if that compiles. – user4581301 Jul 28 '15 at 17:12
  • In servergame.h I can see that error, no include of string, but not in clientgame.h. – user4581301 Jul 28 '15 at 17:13
  • @user4581301 That is actually why I was confused.. It actually works in servergame.h... But not in clientgame.h! Still found a fix tho! (See main post), Thank you! – ECHO Jul 28 '15 at 17:15
  • In that case, servergame is included after another header that does include string. It's considered good form to ensure that headers include everything they need to stand alone. Prevents "It worked here, but not there bugs." Basically all an include does is paste the included file into the the including file in place of the include statement. If anything collides with or supplants a previous definition, bad things happen. Something in string.h got in a fight with the previous definition of std::string in string. If you look, you can probably find what it was. – user4581301 Jul 28 '15 at 17:41

1 Answers1

1

You did not qualify the namespace. string resides in the std namespace so to use it you need to either fully qualify it(std::string), bring in the name with a using directive(using std::string) or you can import the full namespace(using namespace std;). You want to avoid the last option if at all possible.

Community
  • 1
  • 1
NathanOliver
  • 171,901
  • 28
  • 288
  • 402