-
syntax help needed
I'm trying to implement a strategy pattern which I got from here: Strategy pattern - Wikipedia, the free encyclopedia
Here's the code:
Code:
#ifndef ERROR_H
#define ERROR_H
#include <QString>
class Error{
public:
Error();
virtual void writeError(QString error) const = 0;
};
#endif // ERROR_H
#ifndef STRATEGYCONTEXT_H
#define STRATEGYCONTEXT_H
#import "error.h"
class StrategyContext{
private:
Error *m_errorStategy;
public:
explicit StrategyContext(Error *error) : m_errorStategy(error){}
};
Here's the error:
/home/rick/Qt/Error/strategycontext.h: 5: error: candidates are: StrategyContext:: StrategyContext(const StrategyContext& )
/home/rick/Qt/Error/strategycontext.h: 9: error: StrategyContext:: StrategyContext(Error*)
Now I know that that code should be in the cpp file and not the h file. I would like to keep the code separate in h and cpp files.
How do I declare that code correctly in strategycontext.h, please?
-
Inheritance maybe? Not 100% sure though.
[edit]Now I think on it, I'm not quite sure that inheritance has anything to do with it, what was I thinking! Now then, looking at your code, it seems that your explicit constructor deals with an implicit conversion, which isn't allowed. You trying to convert a pointer to an Error object to a QString object. Remove the explicit and it should work, or overload a conversion operator belonging to your Error class.... I'm NOT 100% certain however, this may do nothing.[/edit]
-
This is not the entire error message, though, since it doesn't mention the error, only some diagnostic data.
And don't use #import - I don't know what it is, but it certainly isn't standard. Use #include.
-
#import will create a c/c++ header based on a COM type library (in ms vc++ compiler). Most likely you don't want it in this case.
-
Excuse me, the "import" slipped in via my Java backdoor ;)
I have progressed to this:
I want to make Error an abstract class. I want ErrorLog to inherit from Error. What am I doing wrong? How do I fix the code below? Thanks.
Code:
#ifndef ERROR_H
#define ERROR_H
#include <QString>
class Error{
public:
Error();
virtual void writeError(QString errorType) const = 0;
};
#endif // ERROR_H
#include "error.h"
Error::Error(){
}
#ifndef ERRORLOG_H
#define ERRORLOG_H
#include "error.h"
class ErrorLog : public Error{
public:
ErrorLog();
virtual void writeError(QString);
};
#endif // ERRORLOG_H
#include "errorlog.h"
#include <QDebug>
ErrorLog::ErrorLog(){
}
void ErrorLog::writeError(QString s){
qDebug()<<"called ErrorLog->writeError()";
}
/***************** main *****************/
#include "errorlog.h"
#include "errorprompt.h"
#include "strategycontext.h"
int main(){
ErrorLog errorlog;
return 0;
}
Errors:
/home/rick/Qt/Error/main.cpp:6: error: cannot declare variable ‘errorlog’ to be of abstract type ‘ErrorLog’
/home/rick/Qt/Error/errorlog.h:5: note: because the following virtual functions are pure within ‘ErrorLog’:
/home/rick/Qt/Error/error.h:9: note: virtual void Error::writeError(QString) const
-
You forgot to declare the function in the derived class as const (as it is in the base). One more thing: It's generally preferred to pass a const reference to an object rather than a copy, ie:
Code:
virtual void writeError(const QString & errorType) const;
-
-
-
-
An indent style which isn't "none" would be a bonus as well.
-
hello i think you should use this code
in header file
Code:
#ifndef QSTRING_H_INCLUDED
#define QSTRING_H_INCLUDED
#include<iostream.h>
class StrategyInterface
{
public:
virtual void execute() const = 0;
};
class ConcreteStrategyA: public StrategyInterface
{
public:
virtual void execute() const
{
cout << "Called Concrete Strategy A execute method" << endl;
}
};
class ConcreteStrategyB: public StrategyInterface
{
public:
virtual void execute() const
{
cout << "Called Concrete Strategy B execute method" << endl;
}
};
class ConcreteStrategyC: public StrategyInterface
{
public:
virtual void execute() const
{
cout << "Called Concrete Strategy C execute method" << endl;
}
};
class Context
{
private:
StrategyInterface * strategy_;
public:
explicit Context(StrategyInterface *strategy):strategy_(strategy)
{
}
void set_strategy(StrategyInterface *strategy)
{
strategy_ = strategy;
}
void execute() const
{
strategy_->execute();
}
};
#endif // QSTRING_H_INCLUDED
and in main.cpp file you should use
Code:
#include <iostream>
#include"Qstring.h"
using namespace std;
int main(int argc, char *argv[])
{
ConcreteStrategyA concreteStrategyA;
ConcreteStrategyB concreteStrategyB;
ConcreteStrategyC concreteStrategyC;
Context contextA(&concreteStrategyA);
Context contextB(&concreteStrategyB);
Context contextC(&concreteStrategyC);
contextA.execute();
contextB.execute();
contextC.execute();
contextA.set_strategy(&concreteStrategyB);
contextA.execute();
contextA.set_strategy(&concreteStrategyC);
contextA.execute();
return 0;
}
-
And why is this? Why do you think this method is so good?
To me, it seems to accomplish nothing. The whole Context class could be discarded.
Furthermore, the class accepts a pointer to a class, which could be disastrous, since it can be allocated on the heap or a pointer to a local object. There's no telling which. I'd expect that if a class takes a pointer, it would be of an object from the heap, and in that case, even better would be a smart pointer.
Otherwise, a reference should suffice, as it would in this code.