"const <anonymous>**" and "<unknown type>" errors while compiling

H

H. S.

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<FLOAT>&) [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<double>*, <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 ////////////////////////// %< %< %< %<
 
M

Malte Starostik

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. said:
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<FLOAT>&) [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<double>*, <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
 
M

Malte Starostik

Malte said:
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
 
H

H. S.

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.


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top