Hi,
I am trying to compile these set of C++ files and trying out class
inheritence and function pointers. Can anybody shed some light why my
compiler is not compiling them and where I am going wrong?
I am using g++ (GCC) 3.3.5 on a Debian Sarge system. The compiler complains:
//****************************
//**************************** Compiler output starts ***********
cd /home/red/tmp/testprogs/
time make
g++ -gstabs -ansi -Wall -c -I./ hstestprog.cc -o hstestprog.o
derivedclass2.h: In constructor `
DerivedClass2<FLOAT>::DerivedClass2(hsOperation<FL OAT>&) [with FLOAT =
double]':
hstestprog.cc:34: instantiated from `int CallingFunc(FLOAT, FLOAT*)
[with FLOAT = double]'
hstestprog.cc:19: instantiated from here
derivedclass2.h:19: error: no matching function for call to `
DerivedClass1<double, hsOperation<double> >::DerivedClass1(const
<anonymous>**)'
derivedclass1.h:12: error: candidates are: DerivedClass1<double,
hsOperation<double> >::DerivedClass1(const DerivedClass1<double,
hsOperation<double> >&)
derivedclass1.h:22: error: DerivedClass1<FLOAT,
FOP>::DerivedClass1(FOP*, void (FOP::*)(FLOAT*, FLOAT*)) [with FLOAT =
double, FOP = hsOperation<double>]
hstestprog.cc:34: instantiated from `int CallingFunc(FLOAT, FLOAT*)
[with FLOAT = double]'
hstestprog.cc:19: instantiated from here
derivedclass2.h:21: error: no matching function for call to `
DerivedClass2<double>::vcVirtualFunc(hsOperation<d ouble>*, <unknown
type>)'
virtualclass.h:21: error: candidates are: void VirtualClass<FLOAT, TYPE,
FOP>::vcVirtualFunc(FOP*, void (FOP::*)(TYPE*, TYPE*)) [with FLOAT =
double,
TYPE = double, FOP = hsOperation<double>]
make: *** [hstestprog.o] Error 1
//**************************** Compiler output Ends ***********
//****************************
The source files are given below.
Thanks,
->HS
PS: Here are the source files I am trying to compile, 5 in all:
//******** hsoperation.h ***************** %< %< %< %<
#ifndef _HSOPERATION_H_
#define _HSOPERATION_H_
#include "virtualclass.h"
/*! This header file defines a template class which provides an
overloaded function that does some operation. This class mainly sets up
some data and provides the method to operate on that data. This class
can provide various methods to operate differently on the data.*/
template<class TYPE>
class hsOperation{
public:
hsOperation(TYPE* x, TYPE* y);
void vOperationAdd(TYPE* x, TYPE* y);
void GetSolution(TYPE* R);
TYPE Result;
TYPE X,Y;
};
template <class TYPE>
inline hsOperation<TYPE>::
hsOperation(TYPE* x, TYPE* y){
X = *x;
Y = *y;
}
template <class TYPE>
inline void hsOperation<TYPE>::
vOperationAdd(TYPE* x, TYPE* y)
{
Result = (*x) + (*y);
}
template <class TYPE>
inline void hsOperation<TYPE>::GetSolution(TYPE* R)
{
*R = Result;
}
#endif //_HSOPERATION_H_
/////////// hsoperation.h ////////////// %< %< %< %<
//******** hsoperation.cc ************** %< %< %< %<
#include <cstddef>
#include <iostream>
#include "hsoperation.h"
#include "derivedclass2.h"
template <class FLOAT>
int CallingFunc(FLOAT N, FLOAT* Result);
int main()
{
//hsOperation<int> hsOP;
int i,j;
double dN, dR;
i = 2;
j = 3;
//hsOP.vOperation(&i,&j);
//std::cout << "Result: " << hsOP.Result << std::endl;
CallingFunc<double>(dN, &dR);
}
template <class FLOAT>
int CallingFunc(FLOAT N, FLOAT* pResult)
{
double di, dj;
di=3.5;
dj=10.0;
//initialize certain values here to operated upon later
hsOperation<FLOAT> matrix(&di,&dj);
//now operate on the initialized values
DerivedClass2<FLOAT> prob(matrix);
//now get the result of the operation
prob.Solution(pResult);
return 0;
} // real nonsymmetric standard problem, values and vectors, regular mode.
/////////// hsoperation.cc //////////////// %< %< %< %<
//******** derivedclass1.h *************** %< %< %< %<
#ifndef _DERIVEDCLASS1_H_
#define _DERIVEDCLASS1_H_
#include "virtualclass.h"
/*! This is the first derived class, that corresponds to ARNonSymStdEig
in arsnsym.h
*/
template<class FLOAT, class FOP>
class DerivedClass1:
public virtual VirtualClass<FLOAT, FLOAT, FOP> {
public:
DerivedClass1(FOP* objOPp, void(FOP::* MultOPxp)(FLOAT[], FLOAT[]));
};
template<class FLOAT, class FOP>
inline DerivedClass1<FLOAT, FOP>::
DerivedClass1(FOP* objOPp, void(FOP::* MultOPxp)(FLOAT[], FLOAT[])){
vcVirtualFunc(ObjOPp,MultOPxp);
}
#endif //_DERIVEDCLASS1_H_
/////////// derivedclass1.h ///////////////// %< %< %< %<
//******** derivedclass2.h ***************** %< %< %< %<
#ifndef _DERIVEDCLASS2_H_
#define _DERIVEDCLASS2_H_
#include "hsoperation.h"
#include "derivedclass1.h"
template<class FLOAT>
class DerivedClass2:
public virtual DerivedClass1<FLOAT, hsOperation<FLOAT> > {
public:
DerivedClass2(hsOperation<FLOAT>& A);
void Solution(FLOAT* pR);
};
template<class FLOAT>
inline DerivedClass2<FLOAT>::
DerivedClass2(hsOperation<FLOAT>& A){
vcVirtualFunc(&A, hsOperation<FLOAT>::vOperationAdd);
}
template<class FLOAT>
inline void DerivedClass2<FLOAT>::
Solution(FLOAT* pR){
//*pR = Result;
}
#endif //_DERIVEDCLASS2_H_
/////////// derivedclass2.h ////////////////// %< %< %< %<
//******** virtualclass.h ****************** %< %< %< %<
#ifndef _VIRTUALCLASS_H_
#define _VIRTUALCLASS_H_
/*! This is the Virtual class which will have the virtual function*/
template <class FLOAT, class TYPE, class FOP>
class VirtualClass{
public:
typedef void (FOP::* TypeOPx)(TYPE[], TYPE[]);
FOP* objOP;// Object that has MultOPx as a member function.
TypeOPx MultOPx;// Function that evaluates the product OP*x.
virtual void vcVirtualFunc(FOP*, TypeOPx);
};
template <class FLOAT, class TYPE, class FOP>
void VirtualClass<FLOAT, TYPE, FOP>::
vcVirtualFunc(FOP* objOPp, void (FOP::* MultOPxp)(TYPE[],TYPE[])){
objOP = objOPp;
MultOPx = MultOPxp;
}
#endif // _VIRTUALCLASS_H_
/////////// virtualclass.h /////////////////// %< %< %< %<
//******** Makefile ************************* %< %< %< %<
PROGRAM= hstestprog
OBJS= hstestprog.o
CXX = g++
CXX_FLAGS = -gstabs -ansi -Wall -c
CPPFLAGS= -I./
#put paths and names of libraries
LIBS=
#list other object files's pathnames here
OTHEROBS=
#lnker stuff
LD = g++
LDFLAGS = $(LIBS) -lm
####################doxygen document generation stuff####################
#config file to be used to procude the documentation
DOXFILE=
#directory created by doxygen. As specified in the dox config file.
DOXOUTDIR=
#program to produce the documentation
DOXPROG=doxygen
RM = rm -f
RMDIR= rm -rf
$(PROGRAM) : $(OBJS)
$(LD) $(LDFLAGS) $^ -o $@ $(LDFLAGS) $(OTHEROBS)
#testtnt.o:testtnt.cc
hstestprog.o: hstestprog.cc hsoperation.h virtualclass.h derivedclass1.h
derivedclass2.h
#specify the rule
%.o : %.cc
$(CXX) $(CXX_FLAGS) $(CPPFLAGS) $< -o $@
#make the documentation
####html: *.cc *.h $(DOXFILE)
#### $(RMDIR) $(DOXOUTDIR)
#### $(DOXPROG) $(DOXFILE)
#### echo "docs made"
####$(DOXFILE):
#### $(DOXPROG) -g $(DOXFILE)
cleanALL: cleanall
$(RMDIR) $(DOXOUTDIR)
#clean up operations
prune:
$(RM) *~
clean: prune
$(RM) -f $(OBJS)
cleanall: clean
$(RM) -f $(PROGRAM) *.o *~
/////////// Makefile ////////////////////////// %< %< %< %<
--
Please remove the underscores ( the '_' symbols) from my email address
to obtain the correct one. Apologies, but the fudging is to remove spam. 3 2387
Hi,
{please try to strip the code down some more next time, that includes
combining everything into a single source file, and it's nice to somehow
mark the lines of code that correspond to the error messages.}
H. S. schrieb: Hi,
I am trying to compile these set of C++ files and trying out class inheritence and function pointers. Can anybody shed some light why my compiler is not compiling them and where I am going wrong?
I am using g++ (GCC) 3.3.5 on a Debian Sarge system. The compiler complains:
derivedclass2.h: In constructor ` DerivedClass2<FLOAT>::DerivedClass2(hsOperation<FL OAT>&) [with FLOAT = double]': hstestprog.cc:34: instantiated from `int CallingFunc(FLOAT, FLOAT*) [with FLOAT = double]' hstestprog.cc:19: instantiated from here derivedclass2.h:19: error: no matching function for call to ` DerivedClass1<double, hsOperation<double> >::DerivedClass1(const <anonymous>**)' derivedclass1.h:12: error: candidates are: DerivedClass1<double, hsOperation<double> >::DerivedClass1(const DerivedClass1<double, hsOperation<double> >&)
DerivedClass1< ... > has these ctors:
// Your user-defined one
DerivedClass1(FOP* objOPp, void(FOP::* MultOPxp)(FLOAT[], FLOAT[]));
// Compiler-generated cctor
DerivedClass1(const DerivedClass1&);
That's the candidates listed in the output above.
Now DerivedClass2's ctor looks like this:
template<class FLOAT>
inline DerivedClass2<FLOAT>::
DerivedClass2(hsOperation<FLOAT>& A){
vcVirtualFunc(&A, &hsOperation<FLOAT>::vOperationAdd); // see below
}
As it doesn't include a base initializer, the compiler has no choice but
to require a default ctor for DerivedClass1, which doesn't exist.
That's the 1st error (don't ask me where it gets the <anonymous>**
from). Basically, your ctor above is equivalent to this one:
template<class FLOAT>
inline DerivedClass2<FLOAT>::
DerivedClass2(hsOperation<FLOAT>& A) : DerivedClass1<FLOAT>(){
vcVirtualFunc(&A, &hsOperation<FLOAT>::vOperationAdd); // see below
}
Which of course can't work.
derivedclass1.h:22: error: DerivedClass1<FLOAT, FOP>::DerivedClass1(FOP*, void (FOP::*)(FLOAT*, FLOAT*)) [with FLOAT = double, FOP = hsOperation<double>] hstestprog.cc:34: instantiated from `int CallingFunc(FLOAT, FLOAT*) [with FLOAT = double]' hstestprog.cc:19: instantiated from here derivedclass2.h:21: error: no matching function for call to ` DerivedClass2<double>::vcVirtualFunc(hsOperation<d ouble>*, <unknown type>)' virtualclass.h:21: error: candidates are: void VirtualClass<FLOAT, TYPE, FOP>::vcVirtualFunc(FOP*, void (FOP::*)(TYPE*, TYPE*)) [with FLOAT = double, TYPE = double, FOP = hsOperation<double>]
When gcc mentions <unknown type>, in most cases (and yours) that's
related to "forgetting" the & with member function pointers:
// Wrong
vcVirtualFunc(&A, hsOperation<FLOAT>::vOperationAdd);
// Needs to be:
vcVirtualFunc(&A, &hsOperation<FLOAT>::vOperationAdd);
Two more things:
#ifndef _HSOPERATION_H_ #define _HSOPERATION_H_
Names beginning with an underscore followed by a capital letter and
names containing two consecutive underscores are reserved for the (C++
language) implementation. Thus, you must not use such names in your
source files.
template <class TYPE> inline hsOperation<TYPE>:: hsOperation(TYPE* x, TYPE* y){ X = *x; Y = *y; }
Why are you passing x and y as pointers? If they are always some int or
float types, passing by value comes with no penalty, otherwise a (const
as you don't modify them) reference would seem more adequate:
template <class TYPE>
inline hsOperation<TYPE>::
hsOperation(const TYPE& x, const TYPE& y){
X = x;
Y = y;
}
Same for similar occurances, it also saves all those dereferencing (*)
operators inside the function and the addressof (&) operators in the
calling code.
template <class TYPE> inline void hsOperation<TYPE>::GetSolution(TYPE* R) { *R = Result; }
Same issue, plus why "return" the result this way, when the function
doesn't have an actual return type?
To stick with R as an output parameter, note this time it's a non-const
reference:
template <class TYPE>
inline void hsOperation<TYPE>::GetSolution(TYPE& R)
{
R = Result;
}
Or the more straightforward approach:
template <class TYPE>
inline const TYPE& hsOperation<TYPE>::GetSolution()
{
return Result;
}
Cheers,
Malte
Malte Starostik schrieb: Why are you passing x and y as pointers? If they are always some int or float types, passing by value comes with no penalty, otherwise a (const as you don't modify them) reference would seem more adequate:
template <class TYPE> inline hsOperation<TYPE>:: hsOperation(const TYPE& x, const TYPE& y){ X = x; Y = y; }
For the sake of completeness, the preferred way to initialize members is
an initialization list:
template <class TYPE>
inline hsOperation<TYPE>::
hsOperation(const TYPE& x, const TYPE& y)
: X(x), Y(y)
{
}
This reduces the implicit default initialization followed by an explicit
assignment to an explicit non-default initialization.
Cheers,
Malte
Apparently, _Malte Starostik_, on 19/03/05 21:42,typed: Hi,
{please try to strip the code down some more next time, that includes combining everything into a single source file, and it's nice to somehow mark the lines of code that correspond to the error messages.}
Yes, of course. Thanks for letting me know. Apologies for all the mess
in my post. I was looking for some way I could post a tgz of the source
files on the Yahoo webpage one can get on Geocities but couldn't figure
out how to upload a .tgz :(
This is one of my first posts on this newsgroup and I really appreciate
your polite note. I will surely keep this in mind.
<Some very helpful comments snipped for brevity> Cheers, Malte
I cannot thank you enough for your very instructive and helpful
explanations. They not only solved my problem, but they also were
helpful in providing many insights into class declarations and
inheritence. You made my day! I am actually trying to compile the
ARPACK++ library on a Linux machine using the g++ compiler I mentioned
earlier. It seems to be quite uphill task given that the code is so old.
Thanks once again for your help,
->HS
--
Please remove the underscores ( the '_' symbols) from my email address
to obtain the correct one. Apologies, but the fudging is to remove spam. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Eric A. Forgy |
last post by:
Hello,
I am just learning Java and am trying to write a method that does
something like
//===========================================
public Static List find(double array,double val,String...
|
by: Brandon J. Van Every |
last post by:
I'm realizing I didn't frame my question well.
What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump
up in your chair and scream "Wow! Ruby has *that*? That is SO...
|
by: Bhavin |
last post by:
Hi
I'm trying to set default value for input type file.
<input type="File" name="tx_pdffile" accept="*.pdf"
class="boxText" value="abc.pdf">
but this value (abc.pdf) doesnt appear in the text...
|
by: mark |
last post by:
Forgive me if this seems like a stupid question but I need help...
I'm trying to do a simple online form that emails me the results from
a few fields.
Here is the code:
<form...
|
by: matheussousuke |
last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails.
The goal is: Make it send the email with the URL...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
| |