Solved

C++ Language question

Posted on 1997-09-08
6
866 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

691 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