简体   繁体   English

为什么会出现此错误:C ++中'operator <<'不匹配?

[英]Why am I getting this error: nomatch for 'operator<<' in C++?

Please help me out on why I'm getting an error where I have mentioned the comment guys. 请帮我解决为什么我在提到评论员的地方出错了。 I'm really stuck here! 我真的被困在这里! What other details should I give exactly? 我还应该提供哪些其他详细信息?

Main.cpp Main.cpp

#include <iostream>
#include<person.h>
#include <sstream>

int main()
{
    cout << "Constructor Overloading Demo !!!" << endl;
    person vish;
    cout << vish.toString() << endl; /* I get an error for this statement though I feel this is correct */
    return 0;
}

person.cpp //constructor definition file person.cpp //构造函数定义文件

#include "person.h"
#include <sstream>

person::person()
{
    name = "vish";
    age = 25;
    //ctor
}

void person::toString()
{
    stringstream st;
    st << "Name: ";
    st << name;
    st << "& Age: ";
    st << age;
    prnt = st.str();
    cout << prnt << endl;
    //return 10;
}

person.h //constructor declaration person.h //构造函数声明

#ifndef PERSON_H
#define PERSON_H
#include<iostream>
using namespace std;
class person
{
    public:
        person();
       void toString();

        //virtual ~person();
    protected:
    private:
        string name;
        int age;
        string prnt;
};

#endif // PERSON_H

As name of method person::toString() suggests it should return string , not void (which means that method does not return anything): 就像方法person::toString()建议的那样,它应该返回string ,而不是void (这意味着该方法不返回任何东西):

class person { 
   ...
   std::string toString(); 
    //^--------- type should be changed

of course you need to change that method implementation accordingly: 当然,您需要相应地更改该方法的实现:

std::string person::toString()
{
    stringstream st;
    st << "Name: ";
    st << name;
    st << "& Age: ";
    st << age;
    return st.str();
}

I realize that my function was returning void. 我意识到我的函数正在返回void。 but my question is: why does that matter ? 但是我的问题是:为什么这很重要?

Your output to cout is this: 您对cout输出是这样的:

cout << vish.toString(); // endl is omitted for clarity

which is equal to one of: 等于以下之一:

cout.operator<<( vish.toString() );
operator<<( cout, vish.toString() ); 

whatever is available. 任何可用的。 This means that function or method operator<< needs result of call to toString() and it cannot accept void . 这意味着函数或方法operator<<需要调用toString()结果,并且不能接受void If you just want to print from method toString() write: 如果只想从方法toString()打印,请编写:

vish.toString();

that would work fine (except method name would be still confusing, but compiler does not care). 可以正常工作(除非方法名仍然令人困惑,但编译器不在乎)。

operator<< expects a string to be passed to it. operator <<期望将一个字符串传递给它。 Your ToString function is returning void 您的ToString函数返回void

string person::toString()
{
    stringstream st;
    st << "Name: ";
    st << name;
    st << "& Age: ";
    st << age;
    return st.str();

}

edit: 编辑:

The statement 该声明

std::cout << vish.toString();

is identical to 与...相同

std::cout.operator<<(vish.toString());

The prototype for the operator is 操作员的原型是

std::ostream& operator<<(std::string);
std::ostream& operator<<(int);
//...

The function expects a string or some other parameter as an argument. 该函数需要一个字符串或其他参数作为参数。 Your function is defined as a standalone print function. 您的功能定义为独立的打印功能。 So it should either be modified as stated above or called by itself. 因此,应按上述说明对其进行修改或对其进行单独调用。

The function name 'toString()' implies that the function returns a string representation of the object that can be used for anything, not just printing, so it should return a string. 函数名称“ toString()”表示该函数返回该对象的字符串表示形式,该对象可以用于任何用途,而不仅仅是打印,因此应返回一个字符串。 If the function prints the object it should be called 'print' instead of 'toString'. 如果函数打印对象,则应将其称为“ print”而不是“ toString”。

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

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