syntax help needed

This is a discussion on syntax help needed within the C++ Programming forums, part of the General Programming Boards category; I'm trying to implement a strategy pattern which I got from here: Strategy pattern - Wikipedia, the free encyclopedia Here's ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    3

    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?

  2. #2
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    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]
    Last edited by legit; 06-23-2009 at 11:29 AM.
    MSDN <- Programmers Haven!

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    #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.

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    3
    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

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    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;



    ITSA
    Socket Library!

  7. #7
    Registered User
    Join Date
    Jun 2009
    Posts
    3
    Thank you, Sebastiani.

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    You're welcome.



    ITSA
    Socket Library!

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    An indent style which isn't "none" would be a bonus as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    Registered User
    Join Date
    Jun 2009
    Posts
    4
    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;
    }

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 06:33 AM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21