Solved

C++ Language question

Posted on 1997-09-08
6
856 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
Comment Utility
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
Comment Utility
Note:   I made both types of changes and it compiled with no complaints on my system.
0
 

Author Comment

by:roche
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:roche
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now