2017-07-25 210 views
1

为什么编译器找不到运算符< <。哪里编译器寻找到找运营商< <的定义,当它遇到行 cout <<f.some_func()<<endl;为什么C++编译器找不到运算符<<

错误: error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream<char>}’ and ‘std::vector<std::vector<int> >’) cout <<f.some_func()<<endl;

some notes:.... error: cannot bind ‘std::ostream {aka std::basic_ostream<char>}’ lvalue to ‘std::basic_ostream<char>&&’ cout <<f.some_func()<<endl;

#include <iostream> 
#include <string> 
#include<vector> 
using namespace std; 

struct Bar 
{ 
    int y; 
}; 

class Foo 
{ 
    int x; 
    friend ostream & operator<<(ostream & os, Foo& f) 
    { 
     os << "Foo: " << f.x << endl; 
     return os; 
    } 
    friend ostream & operator<<(ostream & os, Bar& b) 
    { 
     Foo f; 
     os << "Bar b.y: " << b.y << endl; 
     os << "Bar f.x: " << f.x << endl; 

     return os; 
    } 
    friend ostream & operator<<(ostream & os, vector<vector<int> > const& vec){ 
     os << 5; 
     return os; 
    } 

public: 
    Foo(int x = 10):x{x}{} 
    vector<vector<int> > some_func(){ 
    vector<vector<int> > abc(3, vector<int>(3)); 
    int x = 900; 
    return abc; 
    } 
    //If I do this 
    void wrapper(){ 
    cout << this->some_func() << endl; 
    } 

}; 


int main() 
{ 
    Bar b { 1 }; 
    Foo f{ 13 }; 
    cout << f << endl; 
    //cout << b << endl; 
    cout <<f.some_func()<<endl; 
    return 0; 
} 
+0

因为它看起来没有考虑运算符在'Foo'类中定义的机制,它需要应用于来自命名空间'std'的参数。顺便说一句,我建议你踢'习惯使用名称空间标准;' – StoryTeller

回答

1

功能类似于这样定义的因为班内的朋友有相当不寻常的名字查找规则。他们是免费的功能,但他们的名字在他们是朋友的班级之内。因此,它们只能通过依赖于参数的查找来找到。

但是,至少有一个参数必须是类似于对其定义的类的对象的引用,或者是该类中定义的某个东西。

在你的情况下,该参数是一个iostreamvector<vector<int>>,这两者都不具有任何Foo关系,所以编译器不使用参数相关的查找找到Foo里面的功能。

明显的方法,使工作是有重载FooBarFooBar的定义里面,任何类外的过载std::vector<vector<int>>

#include <iostream> 
#include <string> 
#include<vector> 
using namespace std; 

struct Bar 
{ 
    int y; 

    friend ostream & operator<<(ostream & os, Bar& b) 
    { 
     return os << "Bar b.y: " << b.y; 
    } 
}; 

class Foo 
{ 
    int x; 
    friend ostream & operator<<(ostream & os, Foo& f) 
    { 
     return os << "Foo: " << f.x; 
    } 

public: 
    Foo(int x = 10) : x{ x } {} 

    vector<vector<int> > some_func() { 
     vector<vector<int> > abc(3, vector<int>(3)); 
     int x = 900; 
     return abc; 
    } 
}; 

ostream & operator<<(ostream & os, vector<vector<int> > vec) { 
    return os << 5; 
} 

int main() 
{ 
    Bar b{ 1 }; 
    Foo f{ 13 }; 
    cout << f << '\n'; 
    cout << b << '\n'; 
    cout << f.some_func() << '\n'; 
    return 0; 
} 
+0

好吧,那么,如果我想使用运算符<<的矢量和另一个类外的另一个类函数,但其​​对象像主 –

+0

好吧,我现在让它免费的功能,可以说我在Foo中添加以下func: 现在它会给我编译器错误。我已经将运算符<<定义为空闲函数 void wrapper(){ cout << this-> some_func()<< endl; } –

+0

我不确定我是否理解你的评论。在调用它之前,你必须定义'operator <<',但除此之外,从成员函数调用它根本没有困难。 –

-1

在这一行编译器搜索运算符< <在std :: vector中的定义不是你的foo类。

cout <<f.some_func()<<endl; 

如果你想快速无望修复它改变some_func()的代码;

std::string some_func() 
{ 
    std::string ret; 
    vector<vector<int> > abc(3, vector<int>(3)); 
    int x = 900; 

    for (int i; i = abc.size; i++) 
    { 
     for (int f; f = abc.at(i).size; i++) 
     { 

      ret.push_back(abc.at(i).at(f)); 
     } 
    } 
     return ret; 
} 

正确的解决方案过于定义自己的向量类,并在它定义operator < <。 (这是我从游戏开发以来所做的)。

+0

他不需要。 –

+0

不仅如此,它也不会有所作为。 – StoryTeller

+0

它们不是成员函数,所以它们的访问控制(public/private/protected)对它们没有影响。 –