Studing STL我已經編寫了一個簡單的程序來測試仿函數和修飾符。我的問題是關於使用CLASS或STRUCT編寫仿函數並試圖用函數適配器對其進行操作的區別。 據我瞭解,在C++中,CLASS和STRUCT之間的區別在於,在最後一種情況下,成員默認是公開的。這也是我在本網站的答案中多次閱讀的內容。因此,請解釋爲什麼即使我嘗試使用not2修飾符時聲明所有成員(只是函數overloading())public,這段代碼仍然無法編譯。 (我還沒有嘗試過其他改性劑如粘合劑還)使用not2時struct和class爲STL仿函數
#include <iostream>
#include <vector>
#include <functional>
#include <algorithm>
using namespace std;
template <class T>
void print (T i) {
cout << " " << i;
}
// In the manual I read:
// "In C++, a structure is the same as a class except that its members are public by default."
// So if I declare all members public it should work....
template <class T>
class mystruct : binary_function<T ,T ,bool> {
public :
bool operator() (T i,T j) const { return i<j; }
};
template <class T>
class generatore
{
public:
generatore (T start = 0, T stp = 1) : current(start), step(stp)
{ }
T operator()() { return current+=step; }
private:
T current;
T step;
};
int main() {
vector<int> first(10);
generate(first.begin(), first.end(), generatore<int>(10,10));
first.resize(first.size()*2);
generate(first.begin()+first.size()/2, first.end(), generatore<int>(1,17));
cout << "\nfirst :";
for_each (first.begin(), first.end(), print<int>);
cout << "\nFORWARD SORT :";
sort(first.begin(),first.end(),mystruct<int>()); // OK ! even with CLASS
for_each (first.begin(), first.end(), print<int>);
sort(first.begin(),first.end(),not2(mystruct<int>())); // <--- THIS LINE WILL NOT COMPILE IF I USE CLASS INSTEAD OF STRUCT
cout << "\nBACKWARD SORT :";
for_each (first.begin(), first.end(), print<int>);
cout << endl;
}
Everithing運行,如果我使用預期:
錯誤消息我得到的struct mystruct : binary_function<T ,T ,bool> {
public :
bool operator() (T i,T j) const { return i<j; }
};
部分是:
g++ struct.cpp
/usr/include/c++/4.2.1/bits/stl_function.h:
In instantiation of ‘std::binary_negate >’:
struct.cpp:52: instantiated from here
/usr/include/c++/4.2.1/bits/stl_function.h:116:
error: ‘typedef int std::binary_function::first_argument_type’ is inaccessible
/usr/include/c++/4.2.1/bits/stl_function.h:338:
error: within this context
/usr/include/c++/4.2.1/bits/stl_function.h:119:
error: ‘typedef int std::binary_function::second_argument_type’ is inaccessible ....
似乎至少在這種情況下,一個結構體不等同於有公共成員的類,但爲什麼?
好!現在工作。 !謝謝 ! – GBBL 2009-12-08 21:52:30