Solved

C++ Language question

Posted on 1997-09-08
6
863 Views
Last Modified: 2012-06-27
I am trying to build a generic Library class under HP-UX
using RogueWave and HP C++. Basically, I want to implement
the Library class as follows :

template <class T> class Library {
private :
    RWBoolean loaded_; // Whether Library<T> has been loaded
    RWTValSlist<T> *list_;

public :
    Library(const RWDBDatabase&);
    ~Library();
    int numEntries(void); // number of entries in Library<T>
    int reset(void); // recreate Library<T> on demand
};

I then want to pass in any T using the constructor in
Library to call a load() method in T for the Library's
initial load. T could be defined as follows :

class myClass {
private :
    RWCString fld1_;
    RWCString fld2_;
    ...

public :
    myClass();
    ~myClass();
    void *load(RWDBDatabase&); // returns a pointer to a
                               // RWTValSlist (cast is done)
    // comparision method for finding a matching item
    RWBoolean compare(const myClass&, myClass *);
    ...;
};

This will provide an easy way of building a Library of
items (i.e. Library<myClass> myClassLibrary), searching for
a value and only needing to provide the item implementation
and load() and compare() methods in T while treating each
Library "in a similar fashion". Unfortunately, the compiler
doesn't like this...the Library() constructor is :

template<class T>
Library<T>::Library(const RWDBDatabase& aDB)
{
    list_ = *((RWTValSlist<T> *) T::load(aDB));
    loaded_ = TRUE;
}

But, as guessed, the compiler doesn't like this
implementation because it doesn't know the size of T yet.
Any suggestions as to a suitable implementation or other
similar class? (P.S. I also want to store a generic
comparision function pointer (i.e. RWBoolean compare(const
T&, T *item)) in the Library but this has caused similar
compiler complications so I have left this member out of
Library). Thx for any info or suggestions. Send responses to
roche@instinet.com...

L8r...
0
Comment
Question by:roche
  • 3
  • 3
6 Comments
 
LVL 4

Accepted Solution

by:
md041797 earned 0 total points
ID: 1169181
The problem is that you either need a static member function or an object instance to call the load function.  Its hard to tell which would apply in your case, but I would guess the static member function:

class myClass {
  ...
  static void *load(RWDBDatabase&); // returns a pointer to a
  ...;
  };

You can call this with myClass::load.  The compiler will translate from T::load.  You cannot use any non-static members in this function's code.

If you need to change to an object instance consider:

template<class T>
Library<T>::Library(const RWDBDatabase& aDB, T &t)
{
  list_ = *((RWTValSlist<T> *) t.load(aDB));
  loaded_ = TRUE;
  }

The point is, you either have an object to work with, or you don't.


Also, this is faulty:
  list_ = *<<--- You are dereferencing here.  Probably not your intent.
    ((RWTValSlist<T> *) t.load(aDB));


                     But, as gues
0
 
LVL 4

Expert Comment

by:md041797
ID: 1169182
Note:   I made both types of changes and it compiled with no complaints on my system.
0
 

Author Comment

by:roche
ID: 1169183
The solution seems simple enough and seems like it SHOULD work...
Unfortunately the HP-UX compiler returns the following :

    error : initializer for member load (1170)

It seems that defining load() in myClass' header file as :

    static void *load(RWDBDatabase&);

and defining the function in a myClass.cxx file as :

    void *load(RWDBDatabase& aDB) {
        ...
    }

causes problems. If I can get it to work the points are
definitely yours... Any suggestions?!?!?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:roche
ID: 1169184
It seems that defining load() as follows in myClass' header file :
    static void *load(RWDBDatabase&);

and in the myClass.cxx file as :

    void *load(RWDBDatabase& aDB) {
        ...
    }

causes the following error :

    error : initializer for load()

The solution semms simple but I want to test it first (I had
already tried the definition of an object as the other alternate
solution, to no avail). Any suggestions?!?!
0
 
LVL 4

Expert Comment

by:md041797
ID: 1169185
This kind of weird error is usually caused by the parser getting confused.  You probably missed a closing } or something.  Post the whole thing so I can see what's wrong.  What does the manual say that this error means?  Its not clear to me.
0
 

Author Comment

by:roche
ID: 1169186
Problem turned out to be as MD stated and also the fact that the linker couldn't figure out where it put the templatized, compiled
file myClass.c after it built it. The solution -- make sure the
"-I." option is set. This way the linker knows what the compiler
is doing. I could never understand this under UNIX. If you put
something somewhere (a compiled/templatized file), shouldn't your
partner in crime know where it is. Gives new meaning to the term
"artificial intelligence". Anyway, thanx for the answer to my
silly mistake of excluding "static"...
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question