Solved

C++ Language question

Posted on 1997-09-08
6
861 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

856 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