musicmanz93
musicmanz93

Reputation: 29

Converting two characters passed by reference into a string?

Here's my program:

#include <iostream>
#include <string>
using namespace std;

template <class T>
class Example
{
  private:
    T data;

  public:
    Example() { data = 0; }
    void setData(T elem) { data = elem; }

    template <class U>
    friend ostream& operator << (ostream &, const Example<U>&);

    friend ostream& operator << (ostream &, const Example<char>&);

    friend string operator + (const Example<char> &, const Example<char> &);

    template <class U>
    friend U operator + (const Example<U> &, const Example<U> &);
};

template <class U>
U operator + (const Example<U> &a, const Example<U> &b)
{
    U c;
    c = a+b;
    return(c);
}

string operator + (const Example<char> &a, const Example<char> &b)
{
       string a1("");
       a1+=a.data;
       a1+=b.data;
       return(a1);

}

template <class T>
ostream& operator << (ostream &o, const Example<T> &t)
    {
      o << t.data;
      return o;
    }


ostream& operator << (ostream &o, const Example<char> &t)
{
  o << "'" << t.data << "'";
  return o;
}



int main()
{
    Example<int> tInt1, tInt2;
    Example<char> tChar1, tChar2;

    tInt1.setData(15);
    tInt2.setData(30);

    tChar1.setData('A');
    tChar2.setData('B');

    cout << tInt1 << " + " << tInt2 << " = " << (tInt1 + tInt2) << endl;
    cout << tChar1 << " + " << tChar2 << " = " << (tChar1 + tChar2) << endl;
    return 0;
}

How do I go about making the two characters into a string that I can return? I've tried multiple ways and I can't seem to get any of them to work. I think it may have something to do with the characters being passed by reference.

EDIT: Ok so I got that specific function working with no problems. Now I got it compiled but before anything is displayed, there's a segmentation fault. Something is wrong with the addition for the the U data type. It'll add A and B and return AB, but it won't add 15 and 30. Also, I have to say thank you for all your help. I'm still new to programming and I really appreciate it.

Upvotes: 1

Views: 120

Answers (4)

sehe
sehe

Reputation: 393557

The simplest thing that would work:

string operator + (const Example<char> &a, const Example<char> &b)
{
    return { a.data, b.data };
}

If you have an 'older' compiler:

string operator + (const Example<char> &a, const Example<char> &b)
{
    char both[] = {a.data, b.data};
    return string(both, both+2);
}

See it live: http://liveworkspace.org/code/2ORW8E$0

UPDATE To the edit question: There is also a big problem (infinite recursion) here:

template <class U>
U operator + (const Example<U> &a, const Example<U> &b)
{
    U c;
    c = a+b; // NEEDS TO BE a.data + b.data;
    return(c);
}

Here is a fixed version:

#include <iostream>
#include <string>
using namespace std;

template <class T>
class Example
{
private:
    T data;

public:
    Example() : data()
    {
    }
    void setData(T elem)
    {
        data = elem;
    }

    template <class U>
    friend ostream& operator << (ostream &, const Example<U>&);
    friend ostream& operator << (ostream &, const Example<char>&);
    friend string operator + (const Example<char> &, const Example<char> &);

    template <class U>
    friend U operator + (const Example<U> &, const Example<U> &);
};

template <class U>
U operator + (const Example<U> &a, const Example<U> &b)
{
    U c;
    c = a.data+b.data;
    return(c);
}

string operator + (const Example<char> &a, const Example<char> &b)
{
    char both[] = {a.data, b.data};
    return string(both, both+2);
}

template <class T>
ostream& operator << (ostream &o, const Example<T> &t)
{
    o << t.data;
    return o;
}


ostream& operator << (ostream &o, const Example<char> &t)
{
    o << "'" << t.data << "'";
    return o;
}

int main()
{
    Example<int> tInt1, tInt2;
    Example<char> tChar1, tChar2;
    tInt1.setData(15);
    tInt2.setData(30);
    tChar1.setData('A');
    tChar2.setData('B');
    cout << tInt1 << " + " << tInt2 << " = " << (tInt1 + tInt2) << endl;
    cout << tChar1 << " + " << tChar2 << " = " << (tChar1 + tChar2) << endl;
    return 0;
}

Upvotes: 0

Amit
Amit

Reputation: 697

You can do following:

string operator + (const Example<char> &a, const Example<char> &b)
{
    std::ostringstream ss;
    ss << a.data << b.data;

    return ss.str();
}

Upvotes: 0

user529758
user529758

Reputation:

Just use the built-in functionality of std::string, provided there's a member called data which holds the data of type <T>:

std::string operator+(const Example<char> &a, const Example<char> &b)
{
    std::string result("");
    result += a.data;
    result += b.data;
    return result;
}

Upvotes: 0

Bilal Akil
Bilal Akil

Reputation: 4755

#include <sstream>

string operator + (const Example<char> &a, const Example<char> &b) {
    std::ostringstream sstream;
    sstream << a << b;
    return sstream.str();
}

Upvotes: 1

Related Questions