2013-03-19 30 views
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.此外,我必须感谢您的全力帮助。我还是新手编程,我非常感谢。

+0

这取决于你没有显示的'Example'的细节。 – juanchopanza 2013-03-19 09:51:18

+0

ünead显示如何单个'示例'字符或字符串 – 2013-03-19 09:56:23

+0

@ Cheersandhth.-Alf Eye希望在空间中使用坏咒语? – 2013-03-19 09:59:38

回答

1
#include <sstream> 

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

+1令人惊讶的是,这与op的更新代码(此答案更新后)的工作良好,并应该也为其他模板参数 – 2013-03-19 10:03:19

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; 
} 
+0

*** downvote的原因是什么,认真?*** – 2013-03-19 10:25:43

+1

我没有投票,但为什么你是否希望有人在你袭击其他人后向你解释他们的投票?我目睹了你对人们作出不正确的指责,谴责他们,等等。你的行为鼓励人们投票,而不是解释。如果你想要解释,你应该改变你的行为。 – 2013-03-19 13:27:49

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

UPDATE要编辑的问题:还有一个很大的问题(无限循环)位置:

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; 
} 
+0

该op的类不是成员'数据',并且visual C++不支持带括号的初始化方法sintax,除了我不确定它是否真的会在这种情况下指定一个数组。 – 2013-03-19 09:57:57

+0

@ Cheersandhth.-Alf谢谢指出。当然,每个人都做出了假设 - 我张贴我的,所以人们可以判断我自己的假设:) – sehe 2013-03-19 09:59:18

+0

@Cheersandhth。 - 具有讽刺意味的是,OP的课程毕竟拥有数据,还有更多的问题在我更新的答案中提到:) – sehe 2013-03-19 10:14:28

0

您可以做以下几项:

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

    return ss.str(); 
} 
+0

+1与@ 2unco相同 – 2013-03-19 10:04:16

+0

是的,看到按下后输入:) @ 2unco以及我的代码假设'<<'是为数据类型'T'定义的。 – Amit 2013-03-19 10:12:08