LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-25-2018, 12:17 PM   #1
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
C++ :: How to get two separate classes to talk to each other?


This is new to me in classes and how to. I got two classes, one class needs the data vales from the other class, But I am getting an error.

some of the data in here I need to use in the other class.
Code:
#ifndef WMSLIDESHOW_HPP_INCLUDED
#define WMSLIDESHOW_HPP_INCLUDED


#define APPNAME "wmslideshow"
#define VERSION "1.5"
#define AUTHOR "Michael Heras"
#define COPYRIGHT " (c) 2018"
#define CLASSNAME "wmslideshow"
#define INSTANCENAME "wmslideshow"

const char *const wClassName = CLASSNAME;
const char *const wInstanceName = INSTANCENAME;

#include <X11/Xlib.h>
#include <X11/extensions/shape.h>

class cimage;

class WMDockWindow
{
private:

    Display *wDisplay;
    Window Root;
    Window wAppwin;
    Window WiconWin;
    XClassHint classHint;
    XSizeHints sizeHints;
    XWMHints   wmHints;
    Atom       deleteWindow;
    std::string filename;
    std::string pathname;
    int stime;
    int setRandom;
    int setOrdered;

    void usage(char **argv);

public:
    WMDockWindow() {}
    WMDockWindow(int argc, char **argv);
    ~WMDockWindow() {};
    void parseCommandLine(int argc, char **argv);
    inline int getStime() { return stime; };
    inline int getIsRandom() { return setRandom; };
    inline int getIsOrdered() { return setOrdered; };
    inline Display* getDisplay(){ return wDisplay; };
    inline Window getIconWin() {return WiconWin; };

};

#endif // WMSLIDESHOW_HPP_INCLUDED
cpp

Code:
/*
    wmslideshow.cpp

  Copyright (c) 2018 Michael Heras

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307,
  USA.
*/




#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <string>
#include <cctype> //isdigit
#include <X11/extensions/shape.h>
#include <Imlib2.h>

#include "wmslideshow.hpp"
#include "files.hpp"


void WMDockWindow::parseCommandLine(int argc, char **argv)
{
    if (argc > 1)
    {
        for (int i = 1; i < argc; i++)
        {
            if(!strcmp(argv[i], "-f"))
            {
                std::string filename = argv[i+1];
                if (checkIfFile(filename))
                    std::cout<<filename<<std::endl;
               else
               {
                   std::cerr<<"Not a valid file "<<filename<<std::endl;
                   exit(1);
               }

            i++;
            }
            else if (!strcmp(argv[i], "-p"))
            {
                std::string pathname = argv[i+1];
                if (checkIfDirectory(pathname))
                    std::cout<<pathname<<std::endl;
                else
                {
                    std::cerr<<"Not a valid path "<<pathname<<std::endl;
                    exit(1);
                }
                i++;
            }
            else if (!strcmp(argv[i], "-t"))
            {
                if (argv[i+1] == NULL)
                {
                    std::cerr<<"No valid input. "<<std::endl;
                    usage(argv);
                    exit(0);
                }
                else if ( (stime =  int_or_ch(argv[i+1])) == -1 )
               {
                   std::cerr<<"Invalid time. "<<argv[i+1]<<std::endl;
                   exit(1);
               }

                i++;
            }
            else if (!strcmp(argv[i], "-r"))
            {
                setRandom = 1;
                i++;
            }
            else if (!strcmp(argv[i], "-o"))
            {
                setOrdered = 1;
                i++;
            }
            else
            {
                std::cerr<<APPNAME<<" :Invalid option. -> "<<argv[i]<<std::endl;
                usage(argv);
            }


        }//end for parse
    }//end if argc
}

WMDockWindow::WMDockWindow(int argc, char **argv)
{
    char *displayName = NULL;

     // Open display
    if ((wDisplay = XOpenDisplay(displayName)) == NULL)
    {
        std::cerr << APPNAME << ": could not open display " << displayName << std::endl;
        exit(0);
    }

    // Get root window
    Root = RootWindow(wDisplay, DefaultScreen(wDisplay));

    // Create windows
    wAppwin = XCreateSimpleWindow(wDisplay, Root, 0, 0, 64, 64, 5, 5, 0);
    WiconWin = XCreateSimpleWindow(wDisplay, wAppwin, 1, 1, 56, 56, 0, 0, 0);

    // Set classhint
    // to conform to the C char* agasint
    // c++ requiring a const char*
    classHint.res_name =  const_cast<char*>(wInstanceName);
    classHint.res_class = const_cast<char*>(wClassName);
    XSetClassHint(wDisplay, wAppwin, &classHint);

   // Create delete atom
   deleteWindow = XInternAtom(wDisplay, "WM_DELETE_WINDOW", False);
   XSetWMProtocols(wDisplay, wAppwin, &deleteWindow, 1);
   XSetWMProtocols(wDisplay, WiconWin, &deleteWindow, 1);

   // Set windowname
   XStoreName(wDisplay, wAppwin, APPNAME);
   XSetIconName(wDisplay, wAppwin, APPNAME);

   // Set sizehints
   sizeHints.flags= USPosition;
   sizeHints.x = 0;
   sizeHints.y = 0;
   XSetWMNormalHints(wDisplay, wAppwin, &sizeHints);

   // Set wmhints
   wmHints.initial_state = WithdrawnState;
   wmHints.icon_window = WiconWin;
   wmHints.icon_x = 0;
   wmHints.icon_y = 0;
   wmHints.window_group = wAppwin;
   wmHints.flags = StateHint | IconWindowHint | IconPositionHint | WindowGroupHint;
   XSetWMHints(wDisplay, wAppwin, &wmHints);

   // Set command
    XSetCommand(wDisplay, wAppwin, argv, argc);
    XMapWindow(wDisplay, wAppwin);
    XMapWindow(wDisplay, WiconWin);
    XClearWindow (wDisplay,WiconWin);
    XFlush (wDisplay);
    XSync(wDisplay, False);
}

void WMDockWindow::usage(char **argv)
{
    std::cout<<std::endl<<APPNAME<<" "<<AUTHOR<<" "<<VERSION<<" "<<COPYRIGHT<<std::endl<<std::endl
    <<argv[0]<<" -p : path to directory of images."<<std::endl
    <<argv[0]<<" -f : file containing absoloute path to images."<<std::endl
    <<argv[0]<<" -t : delay time in seconds."<<std::endl
    <<argv[0]<<" -r : display images in random order."<<std::endl
    <<argv[0]<<" -o : displa images in ordered list."<<std::endl<<std::endl
    <<APPNAME<<" -t 30 -r -p /path/to/image directory"<<std::endl;
}

class 2
Code:
#ifndef CPIMAGE_HPP_INCLUDED
#define CPIMAGE_HPP_INCLUDED

#include <Imlib2.h>

class WMDockWindow;

class cimage
{
private:

    Imlib_Image Timage;
    Visual *visual;
    int isize;

public:
    cimage()
    {
        isize = 50;
        Timage = NULL;
    };
    cimage(int t) : isize(t) {};
    virtual ~cimage();
    Imlib_Image loadImage(std::string file);
    int load_image_err(std::string path);
    inline Imlib_Image getImage() { return Timage; };
    inline void setIsize(int t) { isize = t; };
    inline int getIsize() { return isize; };
    void setImageToDockapp();

};

#endif // CPIMAGE_HPP_INCLUDED
cpp
Code:
#include <iostream>
#include <Imlib2.h>

#include "cpimage.hpp"

//#include "wmslideshow.hpp"
#include "files.hpp"

using namespace std;

Imlib_Image cimage::loadImage(std::string file)
{
    const char * filename = file.c_str();
    return imlib_load_image_without_cache(filename);
}



int cimage::load_image_err(std::string path)
{
    Imlib_Load_Error err;
    const char *fpath = path.c_str();

    Timage = imlib_load_image_with_error_return(fpath, &err);

    if (err)
    {
        std::cout<<"Bad Image: "<<fpath<<std::endl;

        return 1;
    }
    return 0;
}

void cimage::setImageToDockapp()
{
    visual = DefaultVisual(WMDockWindow->getDisplay(), DefaultScreen( WMDockWindow->getDisplay() ) );

    imlib_context_set_dither(1);
    imlib_context_set_display(WMDockWindow.getDisplay() );
    imlib_context_set_visual(visual);

  //  image = imlib_load_image("/home/userx/Pictures/Missing-Persons-Give.jpg");

    //image = check_image_err(image);
    imlib_context_set_image(getImage());
    imlib_context_set_drawable(WMDockWindow.getIconWin());
    imlib_render_image_on_drawable_at_size(0, 0, isize,isize);

    if (getImage())
        imlib_free_image();
}
the functions in the 2nd class need to access some of the functions returns from the other class.
Code:
void cimage::setImageToDockapp()
{
    visual = DefaultVisual(WMDockWindow->getDisplay(), DefaultScreen( WMDockWindow->getDisplay() ) );

    imlib_context_set_dither(1);
    imlib_context_set_display(WMDockWindow.getDisplay() );
    imlib_context_set_visual(visual);

  //  image = imlib_load_image("/home/userx/Pictures/Missing-Persons-Give.jpg");

    //image = check_image_err(image);
    imlib_context_set_image(getImage());
    imlib_context_set_drawable(WMDockWindow.getIconWin());
    imlib_render_image_on_drawable_at_size(0, 0, isize,isize);

    if (getImage())
        imlib_free_image();
}
main
Code:
#include <iostream>
#include "wmslideshow.hpp"

using namespace std;

int main(int argc, char **argv)
{
    WMDockWindow myDoc;
    cout << "Hello world!" << endl;
    myDoc.parseCommandLine(argc, argv);
    WMDockWindow(argc, argv);

    return 0;
}
regardless if I use the dot . or pointer -> I keep getting these errors.
Code:
||=== Build: Debug in wmslideshow+ (compiler: GNU GCC Compiler) ===|
/home/userx/C++Projects/wmslideshow+/cpimage.cpp||In member function 'void cimage::setImageToDockapp()':|
/home/userx/C++Projects/wmslideshow+/cpimage.cpp|37|error: expected primary-expression before '->' token|
/home/userx/C++Projects/wmslideshow+/cpimage.cpp|37|error: expected primary-expression before '->' token|
/home/userx/C++Projects/wmslideshow+/cpimage.cpp|40|error: expected primary-expression before '.' token|
/home/userx/C++Projects/wmslideshow+/cpimage.cpp|47|error: expected primary-expression before '.' token|
||=== Build failed: 4 error(s), 0 warning(s) (0 minute(s), 2 second(s)) ===|
How would I fix that?

(this is still work in progress, one step at a time, so yeah. This is where I'm stuck at.)
Thanks in advance.

Last edited by BW-userx; 04-25-2018 at 12:51 PM.
 
Old 04-25-2018, 12:35 PM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,068

Rep: Reputation: 7363Reputation: 7363Reputation: 7363Reputation: 7363Reputation: 7363Reputation: 7363Reputation: 7363Reputation: 7363Reputation: 7363Reputation: 7363Reputation: 7363
you still did not learn. Posted just parts of your files, without names so we need to guess what the heck happening at all. There is no easy way to identiy cpimage.cpp line 37 or others. There no way to know what was cut and why (and was that important or not).

So I can only guess:
WMDockWindow->getDisplay()
and
WMDockWindow.getDisplay()
will only work if getDisplay is a static method of class WMDockWindow - or the class was instantiated, like:
Code:
WMDockWindow *w = new WMDockWindow();
w->getDisplay()
Or, as you instantiated it in main, you need to store it in a (global) variable or....
 
Old 04-25-2018, 12:45 PM   #3
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by pan64 View Post
you still did not learn. Posted just parts of your files, without names so we need to guess what the heck happening at all. There is no easy way to identiy cpimage.cpp line 37 or others. There no way to know what was cut and why (and was that important or not).

So I can only guess:
WMDockWindow->getDisplay()
and
WMDockWindow.getDisplay()
will only work if getDisplay is a static method of class WMDockWindow - or the class was instantiated, like:
Code:
WMDockWindow *w = new WMDockWindow();
w->getDisplay()
Or, as you instantiated it in main, you need to store it in a (global) variable or....
but it was in the back of my mind, how much to post without giving too much. so let me appease you. post #1 updated.

thanks by the way. now I'm going to wait.. because
Code:
||=== Build: Debug in wmslideshow+ (compiler: GNU GCC Compiler) ===|
/home/userx/C++Projects/wmslideshow+/cpimage.cpp||In member function 'void cimage::setImageToDockapp()':|
/home/userx/C++Projects/wmslideshow+/cpimage.cpp|37|error: invalid use of incomplete type 'class WMDockWindow'|
/home/userx/C++Projects/wmslideshow+/cpimage.hpp|6|note: forward declaration of 'class WMDockWindow'|
/home/userx/C++Projects/wmslideshow+/cpimage.cpp|39|error: invalid use of incomplete type 'class WMDockWindow'|
/home/userx/C++Projects/wmslideshow+/cpimage.hpp|6|note: forward declaration of 'class WMDockWindow'|
/home/userx/C++Projects/wmslideshow+/cpimage.cpp|39|error: invalid use of incomplete type 'class WMDockWindow'|
/home/userx/C++Projects/wmslideshow+/cpimage.hpp|6|note: forward declaration of 'class WMDockWindow'|
/home/userx/C++Projects/wmslideshow+/cpimage.cpp|42|error: invalid use of incomplete type 'class WMDockWindow'|
/home/userx/C++Projects/wmslideshow+/cpimage.hpp|6|note: forward declaration of 'class WMDockWindow'|
/home/userx/C++Projects/wmslideshow+/cpimage.cpp|49|error: invalid use of incomplete type 'class WMDockWindow'|
/home/userx/C++Projects/wmslideshow+/cpimage.hpp|6|note: forward declaration of 'class WMDockWindow'|
||=== Build finished: 5 error(s), 0 warning(s) (0 minute(s), 2 second(s)) ===|

Last edited by BW-userx; 04-25-2018 at 01:01 PM.
 
Old 04-25-2018, 12:51 PM   #4
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,259

Rep: Reputation: 5338Reputation: 5338Reputation: 5338Reputation: 5338Reputation: 5338Reputation: 5338Reputation: 5338Reputation: 5338Reputation: 5338Reputation: 5338Reputation: 5338
Quote:
Originally Posted by BW-userx View Post
but it was in the back of my mind, how much to post without giving too much.
thanks by the way.
For architecture questions like this, you can leave out the function bodies. The class and function definitions (their arguments and return types) are what's important.
 
Old 04-25-2018, 01:11 PM   #5
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by dugan View Post
For architecture questions like this, you can leave out the function bodies. The class and function definitions (their arguments and return types) are what's important.
that's basically what I thought I did, I gave both classes and only posted the function screwing up.

as it stands right now I looked up static and classes as pan64 made mention of,
https://www.tutorialspoint.com/cplus...ic_members.htm

Modded the two data types to static
Code:
class WMDockWindow
{
private:

   static  Display *wDisplay;
    Window Root;
    Window wAppwin;
   static  Window WiconWin;
    XClassHint classHint;
    XSizeHints sizeHints;
    XWMHints   wmHints;
    Atom       deleteWindow;
    std::string filename;
    std::string pathname;
    int stime;
    int setRandom;
    int setOrdered;

    void usage(char **argv);

public:
    WMDockWindow() {}
    WMDockWindow(int argc, char **argv);
    ~WMDockWindow() {};
    void parseCommandLine(int argc, char **argv);
    inline int getStime() { return stime; };
    inline int getIsRandom() { return setRandom; };
    inline int getIsOrdered() { return setOrdered; };
    inline Display* getDisplay(){ return wDisplay; };
    inline Window getIconWin() {return WiconWin; };

};
changed the functions to call by the example in the link.
Code:
  visual = DefaultVisual(WMDockWindow::getDisplay(), DefaultScreen( WMDockWindow::getDisplay() ) );
and am getting
Code:
||=== Build: Debug in wmslideshow+ (compiler: GNU GCC Compiler) ===|
/home/userx/C++Projects/wmslideshow+/cpimage.cpp||In member function 'void cimage::setImageToDockapp()':|
/home/userx/C++Projects/wmslideshow+/cpimage.cpp|38|error: incomplete type 'WMDockWindow' used in nested name specifier|
/home/userx/C++Projects/wmslideshow+/cpimage.cpp|38|error: incomplete type 'WMDockWindow' used in nested name specifier|
/home/userx/C++Projects/wmslideshow+/cpimage.cpp|41|error: incomplete type 'WMDockWindow' used in nested name specifier|
/home/userx/C++Projects/wmslideshow+/cpimage.cpp|48|error: incomplete type 'WMDockWindow' used in nested name specifier|
||=== Build failed: 4 error(s), 0 warning(s) (0 minute(s), 2 second(s)) ===|
so off to google that ... now ..
 
Old 04-25-2018, 01:47 PM   #6
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
I just stuck it all into the one class, as it was only two data types anyways. simplifying it.
 
Old 04-25-2018, 01:51 PM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,068

Rep: Reputation: 7363Reputation: 7363Reputation: 7363Reputation: 7363Reputation: 7363Reputation: 7363Reputation: 7363Reputation: 7363Reputation: 7363Reputation: 7363Reputation: 7363
(no, it is not solved) you still don't understand, you need static member function, or you need to know the instance (keep it in a variable).
WMDockWindow is not a variable, but a type (a class). You are not allowed to invoke a non-static function of a type - without instance.
 
Old 04-27-2018, 01:15 PM   #8
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by pan64 View Post
(no, it is not solved) you still don't understand, you need static member function, or you need to know the instance (keep it in a variable).
WMDockWindow is not a variable, but a type (a class). You are not allowed to invoke a non-static function of a type - without instance.
O' Tay, I took some steps back and rewrote it without using a class or classes, just straight C++, got all of the kinks worked out of it, and its working without errors. I just turned it into a class and fix my issue with how to get the object into somewhere else. now its loading some other dockapps icon and using that to display instead of the loaded images its getting.

I passed the object into the prams. and I figured out the new issue .. need to fix a means to get the proper data to the image to load it properly.

Last edited by BW-userx; 04-27-2018 at 04:18 PM.
 
Old 04-28-2018, 02:27 AM   #9
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,068

Rep: Reputation: 7363Reputation: 7363Reputation: 7363Reputation: 7363Reputation: 7363Reputation: 7363Reputation: 7363Reputation: 7363Reputation: 7363Reputation: 7363Reputation: 7363
without code I do not really understand anything. But if it works for you probably it is ok.
I think you still need to learn about OOP. A lot.
 
1 members found this post helpful.
Old 04-28-2018, 10:07 AM   #10
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Yep! I agree. When the need dictates it then I'll be doing that. I just rethought my plan of attack on how should I do this line of thought. As I stated two classes was over kill.

I just did this with my one class that I took it down too,
Code:
inline WMWindowDock &getObject() { return *this; }
then used that to pass its members into the functions I needed to access. It compiles with no errors and no more warnings, and does what I want it to do. That being the "main" concern for a program.

Thanks for chiming in through and trying to help.

Last edited by BW-userx; 04-28-2018 at 10:19 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Libvirtd - VMs on separate virtual networks cannot talk to each other za267 Linux - Virtualization and Cloud 2 08-03-2017 05:39 PM
[SOLVED] Booting LFS on separate USB HDD + separate /boot parition nivwusquorum Linux From Scratch 33 12-14-2011 06:38 AM
Class inheritance terminology: super-classes or base classes? 2ck Programming 6 07-20-2011 10:17 AM
Talk Talk to introduce controversial virus alert system Jeebizz Linux - News 0 11-25-2010 10:01 AM
OOP (PHP) classes and extended classes ldp Programming 3 03-05-2005 11:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration