how use literal operators?

on my class String i have these friend literal operators:
1
2
3
4
5
6
7
8
9
10
friend String operator ""_s(const char* chrValue, size_t stringcount)
    {
        return String("SSSS");
    }

    friend String operator ""_b(const char *chrValue, size_t stringcount)
    {

        return String("BBBB");
    }

heres how i use it:
string strTest4="h"_b;

but i get an error:
"unable to find string literal operator 'operator""_b' with 'const char [2]', 'unsigned int' arguments"
so how can i use literal operators?
(i know that for add it inside de class, i must use the 'friend' way)
PS: i hate edit just for add the 'code' command :(
Last edited on
When defining a friend function inside a class definition it is not automatically declared in the surrounding scope, so you need to add declarations outside the class to make it work.

1
2
String operator ""_s(const char*, size_t);
String operator ""_b(const char*, size_t);


Note that if the String(const char*) constructor is public there is no need have them as friends.
Last edited on
i get an error: "conversion from 'String' to non-scalar type 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}' requested"
i did ouside the class:
1
2
3
4
5
String operator ""_b(const char*, size_t)
{

    return String("BBBB");
}

using:
string strTest4= "hello world"_b;
The compiler thinks you want strTest4 to be a std::string because you spelled string with a lower case s.

If you want to use your String class you need to spell with upper case S.

 
String strTest4= "hello world"_b;
yes.. you have right.... that's why we must becarefull with names... i'm sorry.
now works fine... prints "BBBB" like i did.. but i change it and prints the text now.
1
2
3
4
5
String operator ""_b(const char* chrValue, size_t stCount)
{

    return String(chrValue);
}

before i close the thread, i have some questions:
1 - why i must add the second parameter if it's used automatic?
2 - why i can't add these function inside the class?

3 - if the:
1
2
3
4
operator const char*()
    {
        return chrString;
    }

is 1 way, of some, for use cout... what is the other for use cin?
1 - why i must add the second parameter if it's used automatic?


It's not used automatically. You simply don't make use of it.

The size parameter can be useful for performance reasons because you don't need to loop through the string to find the size.

If you want to allow strings with null characters ('\0') in them it's actually necessary to make use of the size parameter because strlen and similar methods of detecting the size will stop at the first null character. With you current implementation it looks like "hello\0world"_b; will construct a String object containing the value "hello".

std::string has a constructor that takes the string as first argument and the size as a second argument. You might want to do something similar for your String class.

2 - why i can't add these function inside the class?

I don't know, but they don't operate on existing objects so usually there would be no

3 - if the:
1
2
3
4
operator const char*()
    {
        return chrString;
    }

is 1 way, of some, for use cout... what is the other for use cin?

That has nothing to do with cout. What it does is that it makes String implicitly convertible to a const char*.
If you want cout to work for your String class I recommend you overload operator<< instead.
The equivalent for cin would be to overload operator>>.

1
2
3
4
5
6
7
8
9
10
11
ostream& operator<<(ostream& os, const String& str)
{
	// ...
	return os;
}

istream& operator>>(istream& is, String& str)
{
	// ...
	return is;
}

Note that you cannot define these operators as members because the first parameter is not the String object so it might make sense to define them as friends.
Last edited on
"That has nothing to do with cout"
tell that to Teach Yourself C++ in One Hour a Day (8th Edition) book:
"Programming Conversion Operators
If you use Listing 12.1 and insert the following line in main():
cout << holiday; // error in absence of conversion operator
The code would result in the following compile failure: error: binary '<<' : no
operator found which takes a right-hand operand of type 'Date'
(or there is no acceptable conversion). This error essentially indicates that
cout doesn’t know how to interpret an instance of Date as class Date does not support
the operators that convert its contents into a type that cout would accept.
We know that cout can work well with a const char*:
std::cout << "Hello world"; // const char* works!
So, getting cout to work with an instance of type Date might be as simple as adding an
operator that returns a const char* version:
operator const char*()
{
// operator implementation that returns a char*
}"
page 341 PDF.
Cambalinho wrote:
tell that to Teach Yourself C++ in One Hour a Day (8th Edition) book:

That's not a book where you'd find sensible C++ practices.
Peter87's comment makes more sense.
true.
today i learned more.
i'm review the C++ with that book. and try test the code.
today i learned very... inclued the setprecision() that isn't on book(i think) like several things ;)
thanks so much for all to all
Topic archived. No new replies allowed.