Thread: Interface problem

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    204

    Interface problem

    Hi,
    i am having trouble compiling my interface. I am trying to store a reference variable as a member variable of the interface object. Compiler says that the variable has not be initiated correctly.

    LCD inherits from VisualInterface which is expecting a DisplayDriver object to be passed in (DisplayDriver is another interface, but thats not important).
    I pass the displayDriver object in when LCD is instantiated in maininterfaces.zip

    I was pasing it before as a pointer but was told that this could cause me problems with memory leaks and a reference was better, but now I cant seem to get it to compile.

    Thanks

    Alex

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by a.mlw.walker View Post
    Hi,
    i am having trouble compiling my interface. I am trying to store a reference variable as a member variable of the interface object. Compiler says that the variable has not be initiated correctly.
    O rly? Because everybody here has an idea what code you are working on and what that member variable is.

    Interesting that you have been here for several years, and you still don't know how to describe your problem and how to post code in the code tags (I downloaded the code and it is sufficiently small to paste).

    Quote Originally Posted by a.mlw.walker View Post
    I pass the displayDriver object in when LCD is instantiated in main
    o_O where is that "main"?

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    204
    Do you not see the member variable displayDriver in the interface VisualInterface?

    Does it matter what displayDriver actually is? Surely to keep things simple just to know that it is being passed in is enough? if I had included main, it would not have been small enough to paste.

    can you also define "small enough to paste"? where's the cut off. i had looked at the code and concluded it is not small enough to paste - thats just a matter of opinion right?

    however...
    Code:
    class VisualInterface {
    public:
    	VisualInterface(DisplayDriver& displayDriver){}
    ...
    	virtual ~VisualInterface() {}
    
    	DisplayDriver& displayDriver;
    }
    ...
    
    
    class LCD : public VisualInterface {
    public:
    	LCD(DisplayDriver& displayDriver){this->displayDriver = displayDriver;}
    }
    from the above is there not enough information to show you what I am trying to do?

    LCD extends VisualInterface. It tries to pass the reference to the memberVariable displayDriver, which is "owned" by VisualInterface.
    However the compiler says that I am not initializing the member Variable of VisualInterface.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    you can't initialize references that way. you have to do it in the constructor's initializer list.

    Quote Originally Posted by a.mlw.walker View Post
    Code:
    class LCD : public VisualInterface {
    public:
    	LCD(DisplayDriver& displayDriver) : displayDriver(displayDriver) {}
    }
    also, I don't recommend having a parameter named the same as a data member.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  5. #5
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by a.mlw.walker View Post
    Do you not see the member variable displayDriver in the interface VisualInterface?
    Omg no, I don't. If you mention a non-existent main, you may as well mention a non-existent member variable. Is is so !$%!&$ hard to tell the variable name?

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    204
    Hopefully thats it Elkvis, thanks.

    Do you mean like this:
    Code:
    LCD(DisplayDriver& displayDriver){this->displayDriver = displayDriver;}
    ?
    kmdv, yeah I agree, i could have written the variable name, but i thought you'd had to look anyway to know what i was talking about, and considering there is only one member variable, I didnt think it would be necessary.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by a.mlw.walker View Post
    Do you mean like this:
    Code:
    LCD(DisplayDriver& displayDriver){this->displayDriver = displayDriver;}
    ?
    no. I literally gave you the correct syntax in the example in my previous post.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Actually, Elkvis, your code is slightly wrong. You cannot initialize an inherited member in the initialiser list of a derived class, so it should be:

    Code:
    class VisualInterface
    {
    public:
        VisualInterface(DisplayDriver& DisplayDriver): m_DisplayDriver(DisplayDriver) {}
    ...
    protected:
        DisplayDriver& m_DisplayDriver;
    }
    ...
     
     
    class LCD : public VisualInterface
    {
    public:
        LCD(DisplayDriver& DisplayDriver): VisualInterface(DisplayDriver) {}
    }
    There is no problem in having the driver as a pointer either, though, so long as you don't delete it. However, you should be aware that if you use a reference, you can't copy or assign the class unless you explicitly define a copy constructor and assignment operator. If you use a pointer, it will work, but you do have to think about if you really want the behaviour the default generated code does.
    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.

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Elysia View Post
    Actually, Elkvis, your code is slightly wrong. You cannot initialize an inherited member in the initialiser list of a derived class
    you're right. I overlooked the inheritance.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    204
    this is the ticket:
    Code:
    : VisualInterface(DisplayDriver) {}

    Thank you very much guys, I've been looking for that for a while. i didnt realise you could do that.

    Its an interface to attach screen/serial terminal etc so each one will only be created once.

    On that pointer/reference point, I was told that in C++ you shouldnt use pointers, but is that just a general rule, cases like this might jusitify it?

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The general rule is that you should avoid new (and thus, delete). There is nothing forbidding you to use pointers, if they are used correctly. There are things pointers can do that references cannot (for example, being rebound). Use the best tool for the job is the best answer. References are typically stronger than pointers, so I would say prefer them over pointers where possible.
    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.

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well, in particular, you have to consider:

    1) which display driver, and if there are more than one
    2) is it possible that the display driver goes out of scope before the visual interface does

    If you can say more than one in response to 1, pointers are be appropriate if your answer is no to 2. Like Elysia says, pointers can be reassigned. If your answer to 2 is yes or I don't know, then you would have to choose a reference counted smart pointer, or use plain old objects instead. References can dangle as much as pointers can.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. serial interface problem
    By phoneix in forum C Programming
    Replies: 3
    Last Post: 04-20-2012, 12:15 PM
  2. problem with c++, mplayer interface.
    By IM back! in forum C++ Programming
    Replies: 10
    Last Post: 02-21-2010, 02:20 PM
  3. Replies: 1
    Last Post: 03-03-2006, 12:22 PM
  4. C++ Share Interface Problem
    By Morphios in forum C++ Programming
    Replies: 0
    Last Post: 04-28-2003, 07:30 PM
  5. Return Interface: problem
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 06-22-2002, 05:03 PM