2017-07-25 215 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)對它們沒有影響。 –