A c++ newbie getting confused with construction of objects

B

Bhanu

Hi,

My code is this: (i will follow it up with the error)

image.h:

#include "window.h"

class Image
{
public:
Image(Win *w);
~Image();

int create(char **data);
void draw(Drawable drw, int x, int y);

private:
Win *w;
Pixmap pix;
Pixmap mask;
int width, height;
GC gc;
XGCValues xgcv;
};

image.cc:

#include <X11/Xlib.h>
#include "xpm.h"
#include "image.h"
#include "win.h"

Image::Image(Win *win)
{
w = win;
}

Image::~Image()
{
XFreePixmap(w->dpy, pix);
XFreePixmap(w->dpy, mask);
XFreeGC(w->dpy, gc);
}

int Image::create(char **data)
{
int status;
XpmAttributes attributes;

attributes.valuemask = XpmColormap | XpmCloseness;
attributes.colormap = w->screen_colormap;
attributes.closeness = 65535;
status = XpmCreatePixmapFromData(win->dpy, win->w, data, &pix, &mask,
&attributes);
if (status != XpmSuccess) return 0;

width = attributes.width;
height = attributes.height;
gc = XCreateGC(win->dpy, win->win, 0, NULL);
XSetFillStyle(win->display, gc, FillTiled);
XSetTile(win->display, gc, pix);
XSetClipMask(win->display, gc, mask);

return 1;
}

void Image::draw(Drawable drw, int x, int y)
{
xgcv.ts_y_origin = y;
xgcv.ts_x_origin = x;
xgcv.clip_y_origin = y;
xgcv.clip_x_origin = x;
XChangeGC(win->display, gc, GCClipXOrigin | GCClipYOrigin |
GCTileStipXOrigin | GCTileStipYOrigin, &xgcv);
XFillRectangle(win->display, drw, gc, x, y, width, height);
}

window.h:

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <sys/time.h>

#define MAX_IMAGES 10
//this is to make sure that the number of images or the paratroopers in
the window is never more than 10


class Image;

class Win
{
friend class Image;/*so that the image class can access the private
variables of the window*/
public:
//this is the constructor
Win(int argc,char *argv[]);

private:
//here the members will be the objects that have to be shown on the
window i,e the images
Display *dpy;
GC gc, copygc;
Image *img[MAX_IMAGES];
Colormap screen_colormap;
Pixmap buffer;
int screen;
int depth;
int num_images;/*This will keep a track of the number of
paratroopers*/
int LoadImage(char **data);
int DrawImage(int x,int y);
//This is the window class.
};

window.cc

#include "bat.xpm"
#include "ball.xpm"
#include <stdio.h>
#include <stdlib.h>
#define NIL (0)
#include "image.h"


int main(int argc,char *argv[])
{
//This is the main function. Here we create a window object rest will
be taken care from there.
Win w(argc,argv);
return 0;
}

Win::Win(int argc,char *argv[])
{
// Open the display
dpy = XOpenDisplay(NIL);
if (dpy == NULL)
{
fprintf(stderr, "Cannot connect to X server %s\n", "simey:0");
exit(-1);
}

//Get the background color.

int blackColor=BlackPixel(dpy,DefaultScreen(dpy));
//Create a window
Window w = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0,
500, 500, 0, blackColor,
blackColor);

//Get the default screen and the depth
screen = DefaultScreen(dpy);
depth = DefaultDepth(dpy, screen);

//Select the inputs that our program has to receive.
XSelectInput(dpy,w,ButtonPressMask|ExposureMask);

//Create a graphics context
gc=XCreateGC(dpy,w,0,0);

//Map the window that is make it appear on the screen.
XMapWindow(dpy,w);

//Set the foreground to say green of the display that is much like
setting the foreground for this window
XColor green;
Status rc;
screen_colormap = DefaultColormap(dpy, DefaultScreen(dpy));
rc = XAllocNamedColor(dpy, screen_colormap, "green", &green, &green);
if (rc == 0) {
fprintf(stderr, "XAllocNamedColor - failed to allocated
'green' color.\n");
exit(1);
}
XSetForeground(dpy,gc,green.pixel);

buffer = XCreatePixmap(dpy, w, 500, 300, depth);
LoadImage(ball_xpm);
DrawImage(100,100);

XFlush(dpy);
XCloseDisplay(dpy);
}

int Win::LoadImage(char **data)
{
if (num_images>=10) return -1;
img[0]= new Image(this);
img[0]->create(data);
//num_images++;
//return (num_images-1);
//return 1;
}

int Win::DrawImage(int x,int y)
{
img[0]->draw(buffer, x, y);
}


The problem:
Now the error that i get on compiling with using g++ window.cc -o
window.o -lX11 -lXpm is:

/tmp/cceikWLv.o(.text+0x59f): In function `Win::LoadImage(char**)':
: undefined reference to `Image::Image[in-charge](Win*)'
collect2: ld returned 1 exit status


Please help me out with this. What i want to do is just put an image on
a window. This i started out thinking would be a simple job but since
this is the first time i am programming in c++ (i am used to c more) i
am having tons of problems. I am sure i made many nasty mistakes in
this code but am unable to figure out. Please give your suggestions.

Thank you,

Bhanu
 
K

Karl Heinz Buchegger

Bhanu said:
The problem:
Now the error that i get on compiling with using g++ window.cc -o
window.o -lX11 -lXpm is:

This compilers only window.cc
What about image.cc?
You need to compiler it also and specify it in the linking step.
/tmp/cceikWLv.o(.text+0x59f): In function `Win::LoadImage(char**)':
: undefined reference to `Image::Image[in-charge](Win*)'
collect2: ld returned 1 exit status

Sure.
image.cc has not been compiled, and the linker doesn't know that the
function Image::Image( Win*) can be found in it.

So specify it

g++ window.cc image.cc -lX11 -lXpm

this compilers window.cc *and* image.cc *and* links both compiled
results to form the final executable using the libraries X11 and Xpm
Please help me out with this. What i want to do is just put an image on
a window. This i started out thinking would be a simple job but since
this is the first time i am programming in c++ (i am used to c more) i
am having tons of problems. I am sure i made many nasty mistakes in
this code but am unable to figure out. Please give your suggestions.

Learn to use your tools.
 
R

Ron Natalie

Bhanu said:
/tmp/cceikWLv.o(.text+0x59f): In function `Win::LoadImage(char**)':
: undefined reference to `Image::Image[in-charge](Win*)'
collect2: ld returned 1 exit status
You need to include image.cc (where Image::Image(Win*) is declared)
into your build.

You need to ask this in a G++ group, it really has nothing to do with
the C++ language but the use of your specific compiler's command line
options.
 
B

Bhanu

The problem was a very trivial one. I made a mistake. I should have
first compiled and then linked the images thus formed. But i was
linking them right after the compilation. So it worked when i compiled
each of them using the -c option and the linked the images formed using
g++ -o go image.o window.o ..

Thanks for the replies.

Bhanu
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,781
Messages
2,569,615
Members
45,294
Latest member
LandonPigo

Latest Threads

Top