Re: c++ ; error

Discussion in 'C++' started by asit, Feb 13, 2011.

  1. asit

    asit Guest

    here is the code

    #include <iostream>
    #include <vector>
    #include <string>
    #include <cstdlib>

    using namespace std;

    char standard_msg_list[40][3][100] = {
    {"100", "Continue", "Request received, please continue"},
    {"101", "Switching Protocols", "Switching to new protocol; obey Upgrade header"},
    {"200", "OK", "Request fulfilled, document follows"},
    {"201", "Created", "Document created, URL follows"},
    {"202", "Accepted", "Request accepted, processing continues off-line"},
    {"203", "Non-Authoritative Information", "Request fulfilled from cache"},
    {"204", "No Content", "Request fulfilled, nothing follows"},
    {"205", "Reset Content", "Clear input form for further input."},
    {"206", "Partial Content", "Partial content follows."},
    {"300", "Multiple Choices", "Object has several resources -- see URI list"},
    {"301", "Moved Permanently", "Object moved permanently -- see URI list"},
    {"302", "Found", "Object moved temporarily -- see URI list"},
    {"303", "See Other", "Object moved -- see Method and URL list"},
    {"304", "Not Modified", "Document has not changed since given time"},
    {"305", "Use Proxy", "You must use proxy specified in Location to access this " "resource."},
    {"307", "Temporary Redirect", "Object moved temporarily -- see URI list"},
    {"400", "Bad Request", "Bad request syntax or unsupported method"},
    {"401", "Unauthorized", "No permission -- see authorization schemes"},
    {"402", "Payment Required", "No payment -- see charging schemes"},
    {"403", "Forbidden", "Request forbidden -- authorization will not help"},
    {"404", "Not Found", "Nothing matches the given URI"},
    {"405", "Method Not Allowed", "Specified method is invalid for this server."},
    {"406", "Not Acceptable", "URI not available in preferred format."},
    {"407", "Proxy Authentication Required", "You must authenticate with " "this proxy before proceeding."},
    {"408", "Request Timeout", "Request timed out; try again later."},
    {"409", "Conflict", "Request conflict."},
    {"410", "Gone", "URI no longer exists and has been permanently removed."},
    {"411", "Length Required", "Client must specify Content-Length."},
    {"412", "Precondition Failed", "Precondition in headers is false."},
    {"413", "Request Entity Too Large", "Entity is too large."},
    {"414", "Request-URI Too Long", "URI is too long."},
    {"415", "Unsupported Media Type", "Entity body in unsupported format."},
    {"416", "Requested Range Not Satisfiable", "Cannot satisfy request range."},
    {"417", "Expectation Failed", "Expect condition could not be satisfied."},
    {"500", "Internal Server Error", "Server got itself in trouble"},
    {"501", "Not Implemented", "Server does not support this operation"},
    {"502", "Bad Gateway", "Invalid responses from another server/proxy."},
    {"503", "Service Unavailable", "The server cannot process the request due to a high load"},
    {"504", "Gateway Timeout", "The gateway server did not receive a timely response"},
    {"505", "HTTP Version Not Supported", "Cannot fulfill request."}
    };

    class _httpCode
    {
    public:
    int code;
    string message;
    string desc;
    _httpCode():code(0),message(""),desc(""){}
    };

    class httpCodeList
    {
    vector<_httpCode> list;
    public:
    httpCodeList()
    {
    for(int i = 0; i<40; i++)
    {
    _httpCode ob;
    ob.code = atoi(standard_msg_list[0]);
    ob.message.append(standard_msg_list[1]);
    ob.desc.append(standard_msg_list[2]);
    list.push_back(ob);
    }
    }
    _httpCode search(int c)
    {
    for(int i=0; i<(int)list.size(); i++)
    {
    if (list.code == c)
    return list.at(i);
    }
    return _httpCode;
    }
    };

    int main()
    {
    httpCodeList ob;
    _httpCode ob1 = ob.search(503);
    cout<<"Code : "<<ob1.code<<endl;
    cout<<"Message : "<<ob1.message<<endl;
    cout<<"Desc : "<<ob1.desc<<endl;
    return 0;
    }
    asit, Feb 13, 2011
    #1
    1. Advertising

  2. asit <> wrote:
    > here is the code


    <lots snipped>

    > class _httpCode
    > {
    > public:
    > int code;
    > string message;
    > string desc;
    > _httpCode():code(0),message(""),desc(""){}
    > };


    > class httpCodeList
    > {
    > vector<_httpCode> list;
    > public:
    > httpCodeList()
    > {
    > for(int i = 0; i<40; i++)
    > {
    > _httpCode ob;
    > ob.code = atoi(standard_msg_list[0]);
    > ob.message.append(standard_msg_list[1]);
    > ob.desc.append(standard_msg_list[2]);
    > list.push_back(ob);
    > }
    > }
    > _httpCode search(int c)
    > {
    > for(int i=0; i<(int)list.size(); i++)
    > {
    > if (list.code == c)
    > return list.at(i);
    > }
    > return _httpCode;


    That's the line the compiler complains about. I guess you in-
    tended to return a new instance of the class here. For that
    you need to change that line to

    return _httpCode();

    which will invoke the default constructor and give you a new
    instance of the class.

    Note that the class name alone (without parentheses) makes only
    sense if it follows 'new' since in that context it tells 'new'
    what kind of object it is supposed to allocate memory for. So

    return *new _httpCode;

    would be correct since 'new httpCode' creates an object (a pointer
    to a new instance of class _httpCode) but not

    return _httpCode;

    were there's no object to be returned, just a class name.

    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
    Jens Thoms Toerring, Feb 13, 2011
    #2
    1. Advertising

  3. On Feb 13, 4:41 pm, asit <> wrote:
    > here is the code
    >
    > #include <iostream>
    > #include <vector>
    > #include <string>
    > #include <cstdlib>
    >
    > using namespace std;
    >
    > char  standard_msg_list[40][3][100] = {
    >         {"100", "Continue", "Request received, please continue"},
    >         {"101", "Switching Protocols", "Switching to new protocol; obey Upgrade header"},
    >         {"200", "OK", "Request fulfilled, document follows"},
    >         {"201", "Created", "Document created, URL follows"},
    >         {"202", "Accepted", "Request accepted, processing continues off-line"},
    >         {"203", "Non-Authoritative Information", "Request fulfilled from cache"},
    >         {"204", "No Content", "Request fulfilled, nothing follows"},
    >         {"205", "Reset Content", "Clear input form for further input."},
    >         {"206", "Partial Content", "Partial content follows."},
    >         {"300", "Multiple Choices", "Object has several resources -- see URI list"},
    >         {"301", "Moved Permanently", "Object moved permanently -- see URI list"},
    >         {"302", "Found", "Object moved temporarily -- see URI list"},
    >         {"303", "See Other", "Object moved -- see Method and URL list"},
    >         {"304", "Not Modified", "Document has not changed since given time"},
    >         {"305", "Use Proxy", "You must use proxy specified in Location to access this " "resource."},
    >         {"307", "Temporary Redirect", "Object moved temporarily -- see URI list"},
    >         {"400", "Bad Request", "Bad request syntax or unsupported method"},
    >         {"401", "Unauthorized", "No permission -- see authorization schemes"},
    >         {"402", "Payment Required", "No payment -- see charging schemes"},
    >         {"403", "Forbidden", "Request forbidden -- authorization will not help"},
    >         {"404", "Not Found", "Nothing matches the given URI"},
    >         {"405", "Method Not Allowed", "Specified method is invalid for this server."},
    >         {"406", "Not Acceptable", "URI not available in preferred format."},
    >         {"407", "Proxy Authentication Required", "You must authenticate with " "this proxy before proceeding."},
    >         {"408", "Request Timeout", "Request timed out; try again later."},
    >         {"409", "Conflict", "Request conflict."},
    >         {"410", "Gone", "URI no longer exists and has been permanently removed."},
    >         {"411", "Length Required", "Client must specify Content-Length."},
    >         {"412", "Precondition Failed", "Precondition in headers is false."},
    >         {"413", "Request Entity Too Large", "Entity is too large."},
    >         {"414", "Request-URI Too Long", "URI is too long."},
    >         {"415", "Unsupported Media Type", "Entity body in unsupported format."},
    >         {"416", "Requested Range Not Satisfiable", "Cannot satisfy request range."},
    >         {"417", "Expectation Failed", "Expect condition could not be satisfied."},
    >         {"500", "Internal Server Error", "Server got itself in trouble"},
    >         {"501", "Not Implemented", "Server does not support this operation"},
    >         {"502", "Bad Gateway", "Invalid responses from another server/proxy."},
    >         {"503", "Service Unavailable", "The server cannot process the request due to a high load"},
    >         {"504", "Gateway Timeout", "The gateway server did not receive a timely response"},
    >         {"505", "HTTP Version Not Supported", "Cannot fulfill request."}
    >         };
    >
    > class _httpCode
    > {
    > public:
    >         int code;
    >         string message;
    >         string desc;
    >         _httpCode():code(0),message(""),desc(""){}
    >
    > };
    >
    > class httpCodeList
    > {
    >         vector<_httpCode> list;
    > public:
    >         httpCodeList()
    >         {
    >                 for(int i = 0;  i<40; i++)
    >                 {
    >                         _httpCode ob;
    >                         ob.code = atoi(standard_msg_list[0]);
    >                         ob.message.append(standard_msg_list[1]);
    >                         ob.desc.append(standard_msg_list[2]);
    >                         list.push_back(ob);
    >                 }
    >         }
    >         _httpCode search(int c)
    >         {
    >                 for(int i=0; i<(int)list.size(); i++)
    >                 {
    >                         if (list.code == c)
    >                                 return list.at(i);
    >                 }
    >                 return _httpCode;


    You have to return an object, not a class. I believe it should be
    return _httpCode().
    It means, you can't find your object in the list, and you
    return an Invalid object by default constructor.

    >         }
    >
    > };
    >
    > int main()
    > {
    >         httpCodeList ob;
    >         _httpCode ob1 = ob.search(503);
    >         cout<<"Code : "<<ob1.code<<endl;
    >         cout<<"Message : "<<ob1.message<<endl;
    >         cout<<"Desc : "<<ob1.desc<<endl;
    >         return 0;
    >
    > }
    >
    >


    Just as a general guideline:
    Don't use an identifier starting with _ like _httpCode.
    The C++ implementation like GCC or Visual Studio use such
    names heavily.

    Regards,
    -- Saeed Amrollahi
    Saeed Amrollahi, Feb 13, 2011
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hfk0
    Replies:
    2
    Views:
    21,627
  2. JavaQueries
    Replies:
    1
    Views:
    3,613
    John C. Bollinger
    Mar 1, 2005
  3. Balaji
    Replies:
    3
    Views:
    10,061
  4. Bishop
    Replies:
    1
    Views:
    753
    Bishop
    Feb 24, 2007
  5. juvi
    Replies:
    3
    Views:
    1,035
    Alexey Smirnov
    Jan 22, 2009
Loading...

Share This Page