0

I get multiple errors when I try to compile parse, including errors about use of undeclared identifier. Why am I getting this error, even though I declared my variables and assigned them values?

Here is my parse code:

// TODO

const char* copy_line = line;
if (copy_line != NULL)
{
    char* method_str = "";
    int method_length = strlen(method_str);
    strtok(method_str, copy_line);
    char* request_str = "";
    int request_length = strlen(request_str);
    strtok(request_str, copy_line);
    char* http_ver_str = "";
    int http_ver_str_length = strlen(http_ver_str);
    strtok(http_ver_str, copy_line);
}
if (request_str != NULL)
{
   char* path = "";
   strtok(path, copy_line);
   char* query_str = "";
   int query_length = strlen(query);
   strtok(query_str, copy_line);
}
char SP = " ";
for (int i = 0; i < method_length; i++)
{
    if (method_str[i] = SP)
    {
        error(400);
        return false;
    }
}
for (int c = 0; c < request_length; c++)
{
    if (request_str[c] = SP)
    {
        error(400);
        return false;
    }
    if (request_str[m] = quote)
    {
        error(400);
        return false;
    }
}
for (int j = 0; j < http_ver_str_length; j++)
{
    if (http_ver_str[j] = SP)
    {
        error(400);
        return false;
    }
}
if (path[0] != '/')
{
    error(400);
    return false;
}
char quote = '"';
for (int k = 0; k < query_length; k++)
{
    if (query_str[k] = quote)
    {
        error(400);
        return false;
    }
}
if (method_str != "GET")
{
    error(405);
    return false;
}
if (request_str[0] != '/')
{
    error(501);
    return false;
}
if (http_ver_str != "HTTP/1.1")
{
    error(505);
    return false;
}
abs_path = path;
query = query_str;
return true;

Here are all of the errors:

server.c:731:13: error: unused variable 'method_length' [-Werror,-Wunused-variable]

        int method_length = strlen(method_str);
        ^

server.c:734:13: error: unused variable 'request_length' [-Werror,-Wunused-variable]

        int request_length = strlen(request_str);
        ^

server.c:737:13: error: unused variable 'http_ver_str_length' [-Werror,-Wunused-variable]
        int http_ver_str_length = strlen(http_ver_str);
        ^

server.c:740:9: error: use of undeclared identifier 'request_str'
    if (request_str != NULL)
    ^

server.c:745:12: error: unused variable 'query_length' [-Werror,-Wunused-variable]

       int query_length = strlen(query);
       ^

server.c:748:10: error: incompatible pointer to integer conversion initializing 'char' with an expression of type 'char [2]' [-Werror,-Wint-conversion]

    char SP = " ";
     ^    ~~~

server.c:749:25: error: use of undeclared identifier 'method_length'

    for (int i = 0; i < method_length; i++)
                    ^

 server.c:751:13: error: use of undeclared identifier 'method_str'

        if (method_str[i] = SP)
        ^

server.c:757:25: error: use of undeclared identifier 'request_length'

    for (int c = 0; c < request_length; c++)
                    ^

server.c:759:13: error: use of undeclared identifier 'request_str'

        if (request_str[c] = SP)
        ^
server.c:764:13: error: use of undeclared identifier 'request_str'

        if (request_str[m] = quote)
        ^

server.c:764:25: error: use of undeclared identifier 'm'

        if (request_str[m] = quote)
                    ^

server.c:764:30: error: use of undeclared identifier 'quote'

        if (request_str[m] = quote)
                         ^

server.c:770:25: error: use of undeclared identifier 'http_ver_str_length'

    for (int j = 0; j < http_ver_str_length; j++)
                    ^

server.c:772:13: error: use of undeclared identifier 'http_ver_str'

        if (http_ver_str[j] = SP)
        ^

server.c:778:9: error: use of undeclared identifier 'path'

    if (path[0] != '/')
    ^

server.c:784:25: error: use of undeclared identifier 'query_length'

    for (int k = 0; k < query_length; k++)
                    ^

server.c:786:13: error: use of undeclared identifier 'query_str'

        if (query_str[k] = quote)
        ^

server.c:792:9: error: use of undeclared identifier 'method_str'

    if (method_str != "GET")

Thanks in advance!

1

use of undeclared identifier means that you're trying to use a variable that was not previously defined inside the scope you're working in.

So variable scope. You should read about that!

I'll tell you a very very very simplified idea of it:

The variable exists inside the pair of curly braces { } in which you declared it.

So if you declare the variables inside the if like this:

if (copy_line != NULL)
{
    char* method_str = "";
    int method_length = strlen(method_str);
    strtok(method_str, copy_line);
    char* request_str = "";
    int request_length = strlen(request_str);
    strtok(request_str, copy_line);
    char* http_ver_str = "";
    int http_ver_str_length = strlen(http_ver_str);
    strtok(http_ver_str, copy_line);
}

They'll only exist inside that if.

And not inside this if and the rest of the code:

if (request_str != NULL)
{
   char* path = "";
   strtok(path, copy_line);
   char* query_str = "";
   int query_length = strlen(query);
   strtok(query_str, copy_line);
}
4
  • @CoderGirl101 You should post your updated code as a separate question pointing out the new problems! If I'm not able to help, I'm sure others will Dec 8 '16 at 23:56
  • There's no limit on questions here. You can ask 500 in a day if you want and people will glady answer! But first try really hard on your own, because your code has a lot lot lot lot lot of problems about things you should have learned in pset5. I can even go so far as to say that you should try and redo pset5 on your own without asking for anyone's help to see if you can (not wanting to discourage you or sth) Dec 9 '16 at 14:03
  • Alright, thank you! I posted a question but someone told me to just use GDB and work from there. Dec 9 '16 at 20:58
  • you run ./server public and then try to access the webpages directly from your browser or from the terminal using the curl command (MARS showed how at the other answer) Dec 10 '16 at 22:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .