Solved

C++ Language question

Posted on 1997-09-08
6
859 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

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…
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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

776 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