简体   繁体   English

为什么将我的打印方法与std :: cout一起使用会导致错误?

[英]Why does using my print method with std::cout result in an error?

#include <iostream>

using namespace std;

class Fam
{
    public:
        char you, urmom, urdad;
        void addPerson(char y, char m, char f)
        {
            you = y;
            urmom = m;
            urdad = f;
        }
};

class Tree: public Fam
{
    public:
        void showFamtree()
        {
            cout<< "Name: " << you << endl;
            cout<< "Mother's name: " << urmom <<endl;
            cout<< "Father's name: " << urdad <<endl;
        }
};

int main(void)
{
    Tree tree;

    char a,b,c;
    cin >> a;
    cin >> b;
    cin >> c;

    tree.addPerson(a,b,c);

    cout<< "Family tree: " << tree.showFamtree() <<endl;

    return 0;    
}

I wanted to print the family tree with the person's name, mother's name, father's name but when I compile it, I get the following error: 我想用该人的名字,母亲的名字,父亲的名字打印家谱,但是当我编译它时,出现以下错误:

invalid operands to binary expression ( basic_ostream<char, std::char_traits<char> > and void ) 二进制表达式的无效操作数( basic_ostream<char, std::char_traits<char> >void

tree.showFamtree() returns nothing (ie void ), it doesn't make any sense to try to pass it to std::cout . tree.showFamtree()返回任何内容(即void ),尝试将其传递给std::cout没有任何意义。 You might change 你可能会改变

cout<< "Family tree: " << tree.showFamtree() <<endl;

to

cout << "Family tree: " << endl;
tree.showFamtree();

If you define operator << like this 如果您这样定义operator <<

ostream& operator << ( ostream & ostr , const Tree & t ){
    ostr << "Name:" << t.you << endl
        << "Mother's name:" << t.urmom << endl
        << "Father's name:" << t.urdad << endl;
    return ostr;
}

then you can use 那么你可以使用

cout<< "Family tree: " << tree <<endl;

This is known as operator overloading in C++. 这在C ++中称为运算符重载

To use something similar to this: 要使用与此类似的东西:

void showFamtree()
{
    cout<< "Name: " << you << endl;
    cout<< "Mother's name: " << urmom <<endl;
    cout<< "Father's name: " << urdad <<endl;
}

with: 与:

cout << "Family tree: " << tree.showFamtree() << endl;

One C++ approach would be to use std::stringstream, as in: 一种C ++方法是使用std :: stringstream,如下所示:

std::string showFamtree()
{
    std::stringstream ss;
    ss << "Name: " << you << endl;
    ss << "Mother's name: " << urmom <<endl;
    ss << "Father's name: " << urdad <<endl;
    return (ss.str());
}

I also often add a label. 我也经常添加标签。 So consider using 所以考虑使用

std::string showFamtree(std::string label)
{
    std::stringstream ss;
    ss << label;
    ss << "Name: " << you << endl;
    ss << "Mother's name: " << urmom <<endl;
    ss << "Father's name: " << urdad <<endl;
    return (ss.str());
}

and change invocation to 并将调用更改为

cout << tree.showFamtree("Family tree: ") << endl;

Note - Perhaps the label should be on its own line, for consistent white space on the left of the 'tree'. 注意-也许标签应该在自己的行上,以便在“树”的左侧保持一致的空白。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM