how can i overloading the string operator outside a class\enum?

Pages: 12
see these enum:
1
2
3
4
5
6
7
8
9
10
11
12
enum TokenType
    {
        ID=0,
        KeyWord=1,
        Operator=2,
        Expression=3,
        String=4,
        Number=5,
        Integer=5,
        Float=6,
        Char=7
    };

for i use the ostream operators, i must use:
1
2
3
4
5
friend ostream &operator << ( ostream& strm, TokenType tt )
    {
       const string nameTT[] = { "ID", "KeyWord", "Operator", "Expression", "String", "Integer", "Float", "Char" };
       return strm << nameTT[tt];
    }

can i do the same for string operator?
1
2
3
4
5
operator string(TokenType tt)
    {
        const string nameTT[] = { "ID", "KeyWord", "Operator", "Expression", "String", "Integer", "Float", "Char" };
       return nameTT[tt];
    }

error message: "'Compile::operator std::__cxx11::string(Compile::TokenType)' must have no arguments"
what i need is return a string when i use the enum:
string VarType=CodeLine[0].Type;
the 'Type' is a enum variable\method
std::string is no operator, so it cannot be used for operator overloading.
you have right, that's why i did these:
1
2
3
4
5
6
friend string &operator = (string &resulted, const TokenType &tt )
    {
       const string nameTT[] = { "ID", "KeyWord", "Operator", "Expression", "String", "Integer", "Float", "Char" };
       resulted=nameTT[tt];
       return resulted;
    }

but i have these error message:
"'std::__cxx11::string& operator=(std::__cxx11::string&, const Compile::TokenType&)' must be a nonstatic member function"
As @nuderobmonkey said, string isn't an operator and in c++ you aren't allowed to define new operators.

Wouldn't it be more natural as:
string to_string(const TokenType tt)
Last edited on
You can of course define your own conversion function (which uses the "operator" keyword).
But they have to be class members...
Last edited on
Why do you use the 'friend' keyoword?
1
2
3
4
5
6
string &operator = (string &resulted, const TokenType &tt )
    {
       const string nameTT[] = { "ID", "KeyWord", "Operator", "Expression", "String", "Integer", "Float", "Char" };
       resulted=nameTT[tt];
       return resulted;
    }

error message: "'std::__cxx11::string& Compile::operator=(std::__cxx11::string&, const Compile::TokenType&)' must have exactly one argument"
now what i'm doing wrong?
if i do:
1
2
3
4
5
6
string &operator = (const TokenType &tt )
    {
       const string nameTT[] = { "ID", "KeyWord", "Operator", "Expression", "String", "Integer", "Float", "Char" };
       string resulted=nameTT[tt];
       return resulted;
    }

i get these warning: "reference to local variable 'resulted' returned [-Wreturn-local-addr]"
compiles, but on run i get an error: "terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_M_construct null not valid"
Yeah, you're returning a reference to a local variable. You should be returning a reference to *this, that is how assignment operators are supposed to work.

Please, just show us a complete example that reproduces your errors. This is madness.

What are you actually trying to accomplish? Conversion functions can only be in classes.
Last edited on
see these line:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
enum TokenType
    {
        ID=0,
        KeyWord=1,
        Operator=2,
        Expression=3,
        String=4,
        Number=5,
        Integer=5,
        Float=6,
        Char=7
    };
    friend ostream &operator << ( ostream& strm, TokenType tt )
    {
       const string nameTT[] = { "ID", "KeyWord", "Operator", "Expression", "String", "Integer", "Float", "Char" };
       return strm << nameTT[tt];
    }

    string &operator = (const TokenType &tt )
    {
       const string nameTT[] = { "ID", "KeyWord", "Operator", "Expression", "String", "Integer", "Float", "Char" };
       string resulted=nameTT[tt];
       return resulted;
    }

    struct TokensType
    {
        string Token="";
        TokenType Type;
        friend ostream & operator<<(ostream &os, TokensType &err  )
        {
            os << "Token: " << err.Token << "\n";
            os << "Token Type: " << err.Type << "\n";
            return os;
        }

        friend ostream & operator<<(ostream &os, vector<TokensType> &err  )
        {
            for (unsigned int i=0; i<err.size(); i++)
            {
                os << "Token: " << err[i].Token << "\n";
                os << "Token Type: " << err[i].Type << "\n";
                if(i<err.size()-1)
                    os << "\n";
            }
            return os;
        }
    };

//........
void RulesMath(vector<TokensType> CodeLine, unsigned int intCodeLine)
    {
        int VarNameIndex = IsVarName(CodeLine[0].Token);
        string VarType=CodeLine[0].Type;//is what i need, but  the 'Type' it's TokenType enum  

that's why i need overloading the assignment operator
Simplifying...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Example program
#include <iostream>
#include <string>
#include <vector>

using std::ostream;
using std::string;
using std::vector;

enum TokenType
{
    ID=0,
    KeyWord=1,
    Operator=2,
    Expression=3,
    String=4,
    Number=5,
    Integer=5,
    Float=6,
    Char=7
};

string to_string(TokenType tt)
{
   static const string nameTT[] = { "ID", "KeyWord", "Operator", "Expression", "String", "Integer", "Float", "Char" };
   return nameTT[tt]; 
}

ostream &operator << ( ostream& strm, TokenType tt )
{
    return strm << to_string(tt);
}


void RulesMath(vector<TokenType> CodeLine)
{
    for (size_t i = 0; i < CodeLine.size(); i++)
    {
        string VarType = to_string(CodeLine[i]);
        std::cout << VarType << '\n'; // or could just print CodeLine[i] directly
    }
}

int main()
{
    vector<TokenType> tokens = { ID, KeyWord, ID, Expression, Integer, Integer, Float, Float, KeyWord };
    RulesMath(tokens);
}


By the way,
"você tem razão" translates to "you are right", not "you have right".

Edit: You repeat 5 in your enum definition. This is most likely a typo.
Last edited on
the assignment operator can be overload?
for i use these line:
string VarType=CodeLine[0].Type;
????
1. that doesn't call the assignment operator
a. don't use that line
No, std::string's assignment operator cannot be overloaded to work with your enum.

An enum is not a class and cannot have an overloaded assignment operator.
thank you so much for correct me.
thank you so much to all
The assignment operator cannot be a non-member function.
https://en.cppreference.com/w/cpp/language/operators
We can get the effect of doing fancy stuff with an enumeration by wrapping it in a class.

For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <string>
#include <boost/algorithm/string.hpp>

enum colour_t { BLACK, RED, GREEN=2, BLUE=4 };

std::string to_string( colour_t clr )
{
    switch(clr)
    {
        case BLACK : return  "Black" ;
        case RED : return  "Red" ;
        case GREEN: return  "Green" ;
        case BLUE: return  "Blue" ;
    }

    return  "Invalid colour " + std::to_string(clr) ;
}

std::ostream& operator<< ( std::ostream& stm, colour_t clr )
{ return stm << to_string(clr) ; }

struct colour
{
    colour( colour_t clr = BLACK ) noexcept : clr(clr) {}

    colour( std::string str )
    {
        boost::trim(str) ;
        if( boost::iequals( str, "red" ) ) clr = RED ;
        else if( boost::iequals( str, "green" ) ) clr = GREEN ;
        else if( boost::iequals( str, "blue" ) ) clr = BLUE ;
        else clr = BLACK ; // defaults to black
    }

    colour( const char* cstr ) : colour( std::string(cstr) ) {}

    operator colour_t& () noexcept { return clr ; }
    operator colour_t () const noexcept { return clr ; }
    operator std::string() const { return to_string(clr) ; }

    colour_t clr = BLACK ;
};

int main()
{
    colour clr_sky = BLUE ;

    bool sunset = true ;
    if(sunset) clr_sky = " rEd" ;

    const std::string str = clr_sky ;

    std::cout << "the colour is " << clr_sky << '\n' ;
}

https://rextester.com/WHOI81013
please correct me: "cannot be non-member", means that must be a class member?
yes
another correction about enum's: why we must use '::' instead dot?
i mean use 'enumname::enumeconst' instead 'enumname.enumeconst'?
Pages: 12