set structs const members

L

Larry

Hi,

I am having problems with the following code:

// mylib.h
#define _CRT_SECURE_NO_WARNINGS
#include <string>
#include <vector>

namespace mylib
{
struct reqline
{
const std::string method;
const std::string uri;
const std::string vprot;
reqline();
reqline(std::string const & line);
};

}



// mylib.cpp
#include "mylib.h"

namespace mylib
{
// reqline
reqline::reqline() : method(""), uri(""), vprot("") {}
reqline::reqline(const std::string & line)
{
std::vector<std::string> vs;
char* token;
const char* delim = " ";
token=strtok((char*)line.c_str(), delim);

while (token != NULL)
{
vs.push_back(std::string(token));
token = strtok(NULL, delim);
}

method = vs[0]; // error
uri = vs[1]; // error
vprot = vs[2]; // error

}
}

// main.cpp
#include <string>
#include "mylib.h"
using namespace mylib;

int main()
{
std::string line = "GET /index.html HTTP/1.1";
reqline rl(line);
(...)
return 0;
}

I would like to have reqline construct set its internal struct by calling it
at runtime with a parameter. Yet. I'm having a problem related maybe to the
fact that I'm not allowd to change a const stringat runtime...I was almost
positive I could set const string in the construction moment though.

what wrong with that?

thanks
 
C

Christophe Bourez

Hi,

  I am having problems with the following code:

// mylib.h
#define _CRT_SECURE_NO_WARNINGS
#include <string>
#include <vector>

namespace mylib
{
 struct reqline
 {
  const std::string method;
  const std::string uri;
  const std::string vprot;
  reqline();
  reqline(std::string const & line);
 };

}

// mylib.cpp
#include "mylib.h"

namespace mylib
{
 // reqline
 reqline::reqline() : method(""), uri(""), vprot("") {}
 reqline::reqline(const std::string & line)
 {
  std::vector<std::string> vs;
  char* token;
  const char* delim = " ";
  token=strtok((char*)line.c_str(), delim);

  while (token != NULL)
  {
   vs.push_back(std::string(token));
   token = strtok(NULL, delim);
  }

  method = vs[0]; //  error
  uri    = vs[1]; // error
  vprot  = vs[2]; // error

 }

}

// main.cpp
#include <string>
#include "mylib.h"
using namespace mylib;

int main()
{
 std::string line = "GET /index.html HTTP/1.1";
 reqline rl(line);
 (...)
 return 0;

}

I would like to have reqline construct set its internal struct by calling it
at runtime with a parameter. Yet. I'm having a problem related maybe to the
fact that I'm not allowd to change a const stringat runtime...I was almost
positive I could set const string in the construction moment though.

what wrong with that?

thanks

You can only initialize const member thanks to the initializer list.
After that, you cannot change it (because const). A possible solution
to this kind of issue is by adding private member functions to the
class like this:

class reqline
{
....
private:
std::string getMethodFrom(const std::string &line) const;
std::string getUriFrom(const std::string &line) const;
std::string getVprotFrom(const std::string &line) const;
};

reqline::reqline(const std::string & line) :
method(getMethodFrom(line)),
uri(getUriFrom(line)),
vprot(getVprotFrom(line))
{}
 
L

Larry

const std::string delim(" ");
std::string::size_type first = 0;
std::string::size_type last = line.find_first_of(delim, first);
method = line.substr(first, last);

I am sorry, bu the code above is not splitting! it just return the entire
line
 
L

Larry

Larry said:
I am sorry, bu the code above is not splitting! it just return the entire
line

here's a little more:

//declare:

struct reqline
{
public:
const std::string method;
const std::string uri;
const std::string vprot;
reqline();
reqline(std::string const & line);
private:
std::string getMethodFrom(const std::string & line) const;
std::string getUriFrom(const std::string & line) const;
std::string getVprotFrom(const std::string & line) const;
};


//define:


reqline::reqline() : method(""), uri(""), vprot("") {}

reqline::reqline(const std::string & line) :
method(getMethodFrom(line)),uri(getUriFrom(line)),vprot(getVprotFrom(line))
{}

std::string reqline::getMethodFrom(const std::string & line) const
{
const std::string delim(" ");
std::string::size_type first = 0;
std::string::size_type last = line.find_first_of(delim, first);
return line.substr(first, last);
}

std::string reqline::getUriFrom(const std::string & line) const
{
const std::string delim(" ");
std::string::size_type first = 0;
std::string::size_type last = line.find_first_of(delim, first);
first = last + 1;
last = line.find_first_of(delim, first);
return line.substr(first, last);
}

std::string reqline::getVprotFrom(const std::string & line) const
{
const std::string delim(" ");
std::string::size_type first = 0;
std::string::size_type last = line.find_first_of(delim, first);
first = last + 2;
last = line.find_first_of(delim, first);
return line.substr(first, last);
}

in main I do:

using namespace std;

line = "GET /wiki/Pagina_principale HTTP/1.1";
mylib::reqline rq(line);

cout << endl << endl;
cout << "method: " << rq.method << endl;
cout << "uri: " << rq.uri << endl;
cout << "vport: " << rq.vprot << endl;
 
L

Larry

Just sorted it out! here's the fixed code:

// reqline
reqline::reqline() : method(""), uri(""), vprot("") {}

reqline::reqline(const std::string & line) :
method(getMethodFrom(line)),uri(getUriFrom(line)),vprot(getVprotFrom(line))
{}

std::string reqline::getMethodFrom(const std::string & line) const
{
const std::string delim(" ");
std::string::size_type first = 0;
std::string::size_type last = line.find_first_of(delim, first);
return line.substr(first, last-first);
}

std::string reqline::getUriFrom(const std::string & line) const
{
const std::string delim(" ");
std::string::size_type first = 0;
std::string::size_type last = line.find_first_of(delim, first);
first = last + 1;
last = line.find_first_of(delim, first);
return line.substr(first, last-first);
}

std::string reqline::getVprotFrom(const std::string & line) const
{
const std::string delim(" ");
std::string::size_type first = 0;
std::string::size_type last = line.find_first_of(delim, first);
first = last + 1;
last = line.find_first_of(delim, first);
first = last + 1;
return line.substr(first, last-first);
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top