将引用传递的两个字符转换为字符串?

时间:2013-03-19 09:48:57

标签: c++ string character

这是我的计划:

#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;
}

如何将两个字符组成一个我可以返回的字符串?我尝试了多种方法,但我似乎无法让它们中的任何一种工作。我认为它可能与通过引用传递的字符有关。

编辑: 好的,所以我得到了特定的功能,没有任何问题。现在我已经编译了但是在显示任何内容之前,存在分段错误。添加U数据类型有问题。它会添加A和B并返回AB,但它不会添加15和30.另外,我要感谢你的帮助。我还是编程新手,我真的很感激。

4 个答案:

答案 0 :(得分:1)

#include <sstream>

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

答案 1 :(得分:0)

只需使用std::string的内置功能,前提是有一个名为data的成员,其中包含<T>类型的数据:

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

答案 2 :(得分:0)

最简单的方法:

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

如果你有一个'较旧'的编译器:

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

现场直播:http://liveworkspace.org/code/2ORW8E$0

更新编辑问题:这里还有一个很大的问题(无限递归):

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);
}

这是一个固定版本:

#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;
}

答案 3 :(得分:0)

您可以执行以下操作:

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

    return ss.str();
}