0

I have this code, and I have this problems: Error 1 error C2110: '+' : cannot add two pointers 2 IntelliSense: expression must have integral or unscoped enum type

I am trying to GET data to the test.php file, with the m_AccountID variable data, I tryed many things, but I got this two errors.

Any idea to fix it?:)

void ConnectEx::SendDataPHP(){
    WSADATA wsaData;
    if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
        //cout << "WSAStartup failed.\n";
        //system("pause");
        return;
    }
    SOCKET Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    struct hostent *host;
    host = gethostbyname("www.mysite.com");
    SOCKADDR_IN SockAddr;
    SockAddr.sin_port = htons(80);
    SockAddr.sin_family = AF_INET;
    SockAddr.sin_addr.s_addr = *((unsigned long*)host->h_addr);
    //cout << "Connecting...\n";
    if (connect(Socket, (SOCKADDR*)(&SockAddr), sizeof(SockAddr)) != 0){
        //cout << "Could not connect";
        //system("pause");
        return;
    }

    //cout << "Connected.\n";

    send(Socket, "GET /api/test.php?username=" +m_AccountID+ "HTTP/1.1\r\nHost: www.mysite.com\r\nConnection: close\r\n\r\n", strlen("GET /api/test.php?username="+m_AccountID+ "HTTP/1.1\r\nHost: www.mysite.com\r\nConnection: close\r\n\r\n"), 0);
char buffer[10000];
int nDataLength;
while ((nDataLength = recv(Socket, buffer, 10000, 0)) > 0){
    int i = 0;
    while (buffer[i] >= 32 || buffer[i] == '\n' || buffer[i] == '\r') {
        //cout << buffer[i];
        i += 1;
    }
}
closesocket(Socket);
WSACleanup();
//system("pause");
    return;

}

class ConnectEx
{
public:
    void    SendDataPHP();
.
.
.
.
    char    m_AccountID[11];
.
.
.


}; extern ConnectEx gConnectEx;
7
  • change char m_AccountID[11]; to std::string m_AccountID; Commented Jul 3, 2016 at 3:43
  • I tryed before, still the same problems :) Commented Jul 3, 2016 at 3:44
  • put your string like this: std::string( "GET /api/test.php?username=" + m_AccountID + " HTTP/1.1\r\nHost: www.mysite.com\r\nConnection: close\r\n\r\n" ).c_str( ) but only for the first part Commented Jul 3, 2016 at 3:45
  • Same errors + 1 Error 2 error C2228: left of '.c_str' must have class/struct/union Commented Jul 3, 2016 at 3:49
  • I've updated my post. Please also check if the errors actually appear in any of the lines you provided Commented Jul 3, 2016 at 4:08

2 Answers 2

1

Just concatenate it to the string:

send( Socket ,
      std::string( "GET /api/test.php?username=" + m_AccountID + " HTTP/1.1\r\nHost: www.mysite.com\r\nConnection: close\r\n\r\n" ).c_str( ) , 
      strlen("GET /api/test.php?username=" + m_AccountID + " HTTP/1.1\r\nHost: www.mysite.com\r\nConnection: close\r\n\r\n") , 0);

Demo

Also if you just repeat the string afterwards, save it to a variable first so you can reuse that. And i wouldn't recommend using std::strlen since it's old.

Rather do it like this:

std::string m_AccountID;
std::string data;


m_AccountID = "12345";
data        = "GET /api/test.php?username=" + m_AccountID + " HTTP/1.1\r\nHost: www.mysite.com\r\nConnection: close\r\n\r\n"

send( Socket , data.c_str( ) , data.size( ) , 0 );

char m_AccountID[11]; should be std::string m_AccountID;

Your code should be looking like this now:

void ConnectEx::SendDataPHP(){
    WSADATA wsaData;
    if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
        //cout << "WSAStartup failed.\n";
        //system("pause");
        return;
    }
    SOCKET Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    struct hostent *host;
    host = gethostbyname("www.mysite.com");
    SOCKADDR_IN SockAddr;
    SockAddr.sin_port = htons(80);
    SockAddr.sin_family = AF_INET;
    SockAddr.sin_addr.s_addr = *((unsigned long*)host->h_addr);
    //cout << "Connecting...\n";
    if (connect(Socket, (SOCKADDR*)(&SockAddr), sizeof(SockAddr)) != 0){
        //cout << "Could not connect";
        //system("pause");
        return;
    }

    //cout << "Connected.\n";

    send(Socket, std::string( "GET /api/test.php?username=" +m_AccountID+ "HTTP/1.1\r\nHost: www.mysite.com\r\nConnection: close\r\n\r\n" ).c_str( ) , strlen("GET /api/test.php?username="+m_AccountID+ "HTTP/1.1\r\nHost: www.mysite.com\r\nConnection: close\r\n\r\n"), 0);
char buffer[10000];
int nDataLength;
while ((nDataLength = recv(Socket, buffer, 10000, 0)) > 0){
    int i = 0;
    while (buffer[i] >= 32 || buffer[i] == '\n' || buffer[i] == '\r') {
        //cout << buffer[i];
        i += 1;
    }
}
closesocket(Socket);
WSACleanup();
//system("pause");
    return;

}

class ConnectEx
{
public:
    void    SendDataPHP();
.
.
.
.
    std::string m_AccountID;
.
.
.


}; extern ConnectEx gConnectEx;

Make sure you also #include <string>

Sign up to request clarification or add additional context in comments.

5 Comments

Error 1 error C2110: '+' : cannot add two pointers, Thats the 1st error, 2nd: 2 IntelliSense: expression must have integral or unscoped enum type
@Vbnetguy I'm guessing you're using cstrings? you should switch away from them. use normal strings unless you have a good reason
No. I am not using, any another idea? I tryed the same and I get this error, thats why I asked here. I just thinked I made something bad, than I removed the + from the example, because I dont want looks like a retard. :)
@Vbnetguy try send( Socket , data.c_str( ) , data.size( ) , 0 ); or just edit your question and provide lil bit more code
Thanks! I added more code and edited the original post!
0

Now I made it, I used strcat to merge the variable than I put this into the send and its working. Thanks for the help!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.