I am tryin to give fraction object in s string via template.

Hello.
I am tryin to give fraction object in s string via template.
Any idea how?

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
struct Fr // simple Fraction
{
    int num;
    int denom;
    Fr(int i,int j)
    {
        num=i;
        denom=j;
    }
    Fr() {}
};


struct stringify
{
    operator Fr() const
    {
        return .... must give the string ==> num/denom ; any idea how to template takes the string?  
I also use 
string a;
return string a;  // and there is problem
    }
    operator std::string() const
    {
        return str ;
    }
    std::string str ;
};

template < typename T > stringify operator+ ( stringify s, const T& object )
{
    std::ostringstream stm ;
    stm << object ;
    if( !s.str.empty() && !std::isspace( *s.str.rbegin() ) ) s.str += ' ' ;
    s.str += stm.str() ;
    return s ;
}

You just can't. Look at Boost.Lexical_Cast ( http://www.boost.org/doc/libs/1_55_0/doc/html/boost_lexical_cast.html )
Boost libraries are so good that even C++11 had used its features (smart pointers). You can use file systems, networking, smart pointers, casts, threading, macros and thousand things more.
Last edited on
You cant with stringify... but you can with Fr. Instead, implement a
1
2
3
4
std::string operator std::string()
{
    return (std::to_string(num) + "/" + std::to_string(denom));
}
How i can use std::string operator std::string() ?
When put in code gcc tells me error

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct Fr 
{
    int num;
    int denom;
    Fr(int i,int j)
    {
        num=i;
        denom=j;
    }
    Fr(){};
    std::string operator std::string()
{
    return (std::to_string(num) + "/" + std::to_string(denom));
}
};



error: return type specified for 'operator std::string {aka std::basic_string<char>}'|

Last edited on
Needs C++11
He means you need to enable C++ 11 with the -std=c++11 flag (I believe).
No, that is wrong. You don't specify a return type for the operator. Here is an example of what you can do:
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
#include <string>
#include <iostream>

struct Fr 
{
    int num;
    int denom;
    Fr(int i,int j)
    {
        num=i;
        denom=j;
    }
    Fr(){};
    
    /* std::string */ operator std::string()
    {
        return (std::to_string(num) + "/" + std::to_string(denom));
    }
};

std::ostream& operator<< (std::ostream& o, const Fr& f) {
    o << f.num << '/' << f.denom;
    return o;
}

int main() {
    Fr f (4, 9);
    std::string s = f;
    std::cout << s << std::endl;
    std::cout << f << std::endl;
    return 0;
}
4/9
4/9

http://coliru.stacked-crooked.com/a/c7c760a201b38d67

Note that by defining operator<< for your class as well, you can use your original stringstream method to convert into a string as well.
Last edited on
Sorry, I didn't know the cast operator don't need a return, but it is kind obvious. (cast to std::string would return int?!?)
Depending on what you're doing, it could.
Very helpful, thank you all.
I have try compilation and is ok.
Thank you.
Last edited on
Huh, how @Avillius? If I want to cast my class to std::string , it's obvious it would return int! </sarcasm>
Topic archived. No new replies allowed.