string length: invalid static_cast - c++11

I was expecting this sample code to work:
std::string s;
int number=1;
s = std::to_string(number);
int size=static_cast<int>(s.length);
However it gives the error:
main.cpp:178:39: error: invalid static_cast from type ‘’ to type ‘int’
int size=static_cast(s.length);
Then, I also tried:
int size=atoi(s.length);
Which gives me the error:
cannot convert ‘std::basic_string<_CharT, _Traits, _Alloc>::length<char, std::char_traits<char>
... to type ‘const char*’
Then, I tried this option:
int size=atoi(s.c_str());
This one worked. Any hints why atoi(s.length) does not work, and instead atoi(s.c_str()) is required?
So, suppose that I have the input string as 999, the total of digits will be 3. Using s.length would be the best way to get the total of digits, however the s.length casting gives the error.

You did not invoke the length method.
You should use int size=static_cast<int>(s.length());: notice the call operator at the end of the length method name.
However, if by doing this, you are trying to convert the string to an integer, this is wrong. This only gives you the number of characters in the string.

Related

How to convert string to long int in omnet++?

In omnet++ 5.5.1, i declared a vector in NED file using string:
string state = default("1 2 3 4 5");
In the .cc file, I wrote
std::vector<std::string> statesStr = cStringTokenizer(par("state")).asVector();
std::vector<long> statesPar;
for (auto k : statesStr) {
// How to convert the string to long?
statesPar.push_back(k.c_str()); // error
}
The above code shows the below error
invalid conversion from ‘const char*’ to ‘std::vector::value_type {aka long int}’ [-fpermissive]
Would anyone please suggest to me how to fix the error? Thank you.
This is a basic C++ question, unrelated to OMNeT++. You cannot put a string into a long vector. Strings can be converted to long with the std::stol() function.

Make gcc print the code line when "required from here" error is printed

When my static assertion triggers, I get the following error message:
In file included from test2.cpp:1:
test.h: In instantiation of ‘test::test<T, test_from,test_to>::test() [with T = Eigen::Transform<double, 3, 1>; unsigned int test_from = 3740358174u; unsigned int test_to = 837757431u]’:
test2.cpp:51:27: required from here
test.h:38:9: error: static assertion failed: Wrong number assignment. Numbers in computation are assigned to a different object.
static_assert(test_from==test_from2,"Wrong number assignment. Numbers in computation are assigned to a different object.");
^
I would like gcc to print the line and highlight the character for the "required from here" disgnostic:
In file included from test2.cpp:1:
test.h: In instantiation of ‘test::test::test() [with T = Eigen::Transform; unsigned int test_from = 3740358174u; unsigned int test_to = 837757431u]’:
test2.cpp:51:27: required from here
destination = source.inverse();
^
test.h:38:9: error: static assertion failed: Wrong number assignment. Numbers in computation are assigned to a different object.
static_assert(test_from==test_from2,"Wrong number assignment. Numbers in computation are assigned to a different object.");
^
Is there any compiler flag or code pragma I can activate to have this behaviour?

error when trasforming tolower, help would be nice

I'm trying to convert a txt file so all words can be lower case. and when I try I get an error I've tried to differ ways but no luck, my code is as followed
ifstream words("sample_doc.txt");
ofstream samp("sample.txt");
set <string> lower;
set <string> to_low;
copy(istream_iterator<string>(words), istream_iterator<string> (), inserter(lower, begin(lower)) );
transform(begin(lower), end(lower), inserter(to_low, begin(to_low) ), tolower );
when I tried to complete I get this error
'int (int)': cannot convert argument 1 from 'const std::basic_string,std::allocator>' to 'int'
Your problem is that int tolower(int ch); takes as argument an int or a char. However, you are passing it a std::string.
for(std::string& x: lower)
{
transform(x.begin(), x.end(), x.begin(), [](unsigned char c) { return std::tolower(c);});
to_low.insert(x);
}
If you change your last line to the above loop, it will convert each string in std::set<std::string> lower to a lower case string, character by character, and insert it in std::set<std::string> to_low
Hope this helps!

passing argument 1 of 'strlen' makes pointer from integer without a cast

It is not clear why I get a warning of:
[Warning] passing argument 1 of 'strlen' makes pointer from integer without a cast [enabled by default]
expected 'const char *' but argument is of type 'char'
on two of the 3 statements containing strlen() below.
Even when I attempted to cast *str it still gave the same warning.
bfr is a character buffer. *str points to that char buffer after the call to
gets(). If I use strlen(*str) I get a warning. If I use strlen(bfr) I do not.
But *str should be the equivalent to bfr. Thus the confusion regarding the error.
Now in reality, strlen arg 1 is defined as strlen(const char *string). So I
would have expected strlen(bfr) to also produce an error since bfr[] is a
char string and not a const char either.
And where is the integer that is being made into a pointer?
I am using gcc under wXDev-C++.
void test(){
FILE *fileID = fopen("somefile.txt","r");
char *str, len;
char bfr[16];
str = fgets(bfr,16,fileID); // str will be set equal to &bfr[0]
len = strlen(*str); // This gives a warning
len = strlen((const char)*str); // This gives a warning
len = strlen(bfr); // This does not give a warning
}
Sometimes you just need to take a fresh look in the morning at a problem. I realized that strlen is looking for a pointer to a string and 'str' is defined as a pointer. So *str would be a pointer to a pointer. So the warning was correct. It should read len = strlen(s) not len = strlen(*s). And it is 'str' pointing to 'bfr' not *str;
Answered my own question.

How to cast int to float in GLSL (WebGL)?

My code is (inside the void main):
float res;
for(int i=0; i<15; i++) {
res = float(i)/15.0;
//...
}
Unfortunately I get a syntax error at float(i)/15.0
If I just write i/15.0, then the error is:
wrong operand types no operation '/' exists that takes a left-hand operand of type 'mediump int' and a right operand of type 'const float' (or there is no acceptable conversion)
If I just try i/15 then the result is an integer, but I would like to get a float.
How is it possible to cast int to float?
It seems that you're not allowed to cast in GLSL. Therefore, "you have to use a constructor".
Try this:
// http://www.shaderific.com/glsl-types/
// "Implicit type conversions are not supported.
// Type conversions can be done using constructors..."
float i_float = float(i);
res = i_float / 15.0;
PS: If you have a look at the documentation, it says that "... Either integer type can be converted into floats, and integers and floats can be converted into doubles." ... I find it odd that your code is not accepted by the GLSL compiler. (cf. Reto Koradi's comment)

Resources