1

这是我的程序:

#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

4 回答 4

1
#include <sstream>

string operator + (const Example<char> &a, const Example<char> &b) {
    std::ostringstream sstream;
    sstream << a << b;
    return sstream.str();
}
于 2013-03-19T09:55:04.010 回答
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;
}
于 2013-03-19T09:55:27.423 回答
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;
}
于 2013-03-19T09:56:46.973 回答
0

您可以执行以下操作:

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

    return ss.str();
}
于 2013-03-19T10:00:14.987 回答