1
#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)

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
ree
  • 11
  • 1
  • 2
    Please don't tag unrelated languages, this is certainly not C. – Baum mit Augen May 25 '16 at 01:32
  • Also, quotes are for quotes. not for normal text. – Baum mit Augen May 25 '16 at 01:33
  • 2
    I edited your post to give it a title that carries some information and I included some error message for your problem. (The latter was generated with clang, you may change that to the error message your compiler yields if you want to.) For the future, please make sure your titles describe the problem and your posts include relevant error messages. Have fun on this site. – Baum mit Augen May 25 '16 at 01:44
  • Thanks, everyone. All apologies if my question didn't make that much sense to you. I'm just a Computer Science student trying to save my grades this semester so I can finally shift to my desired major. – ree May 25 '16 at 04:47

3 Answers3

2

tree.showFamtree() returns nothing (i.e. void), it doesn't make any sense to try to pass it to std::cout. You might change

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

to

cout << "Family tree: " << endl;
tree.showFamtree();
songyuanyao
  • 169,198
  • 16
  • 310
  • 405
1

If you define operator << like this

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++.

BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
1

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:

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'.

2785528
  • 5,438
  • 2
  • 18
  • 20