Ich habe so etwas wie dies:ungültige Verwendung von nicht-statischer Member-Funktion
class Bar
{
public:
pair<string,string> one;
std::vector<string> cars;
Bar(string one, string two, string car);
};
class Car
{
public:
string rz;
Bar* owner;
Car(string car, Bar* p);
};
class Foo
{
public:
Foo (void);
~Foo (void);
int Count (const string & one, const string & two) const;
int comparator (const Bar & first, const Bar & second) const;
std::vector<Bar> bars;
};
int Foo::comparator(const Bar & first, const Bar & second) const{
return first.name < second.name;
}
int Foo::Count (const string & one, const string & two) const{
int result=0;
Bar mybar = Bar(one, two, "");
std::vector<Bar>::iterator ToFind = lower_bound(bars.begin(), bars.end(), mybar, comparator);
if (ToFind != bars.end() && ToFind->one == mybar.one){
result = ...
}
return result;
}
Die Methode Foo::Count
sollte std::lower_bound()
verwenden Element zu finden in vector<Bar>
nach Paar von zwei Saiten. Nun der Teil, der nicht funktioniert. Zu lower_bound()
Ich stelle Methode comparator()
zur Verfügung. Ich dachte, es war in Ordnung, aber g ++ sagt:
c.cpp: In member function ‘int Foo::Count(const string&, const string&) const’:
c.cpp:42:94: error: invalid use of non-static member function
std::vector<Bar>::iterator ToFind = lower_bound(bars.begin(), bars.end(), mybar, comparator);
Und die Methode Count()
muss bleiben const
...
Ich bin ganz neu in C++, weil ich gezwungen bin, es zu lernen.
Irgendwelche Ideen?
Warum genau ist diese Funktion nicht statisch? – chris
Welcher? Ich darf die Deklaration der Funktion Count() – Nash