[Solved] Problems with virtual classes

I have to solve a problem where I need to evaluate a function. Let's called it g(y). This g(y) depends on functions f(y) and h(f(y)). (f and h are known)Have tried to solve this problem using virtual functions, but I get the error message "undefined reference to" do_something_else function. The first evaluation works fine, but the the second one doesn't work. Any help is appreciated.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class A {
public:
	virtual double evaluate(double y) = 0; //Define virtual function
	virtual ~A() { }
};
class B : public A{
	double x;                    // Parameter
public:
	B ( double a ){ x = a; }     // Constructor
       ~B() { }                      // Destructor
	double do_something(double t);  // Evaluate function f(t)
	double evaluate( double t ){ return do_something(t); }
};
class C : public A{
private:
        double b;                      // Parameter
public:
     // Constructor and destructor
        double do_something_else( double s );   // Define h(s)
        double evaluate(double y){              // Evaluate function g(y)
	      A* Beta = new B(b);               // 
	      double f = Beta->evaluate(y);     // At first evaluate f(y)
              double h = do_something_else(f);  // then evaluate h(f(y))
	      return h*f; 
         }
	 friend class B;
};
Last edited on
Show the exact error message for the code you demonstrated.
It is a very weird (.text._ZN6logDer8evaluateESt7complexIdE [logDer::evaluate(std::complex<double>)]+0x126): undefined reference to ` do_something_else(arguments .... )
Have you defined do_something and do_something_else?
Why does the message reference to the function parameter of type std::complex<double>?
You should prepare a sample that demonstrates the problem.
The function f(y) is actually a quite complicated complex mathematical function and I need to evaluate logarithmic derivative of that function g(y). So its argument is complex<double> not double. (actually every number is complex) I don't know if there is something wrong with the syntax, so I tried to show what is the problem. Even my editor says that there is something wrong in that line. Even if do_someting_else is simply a function that takes a double and returns it. But I am sorry there is a one mistake. It should be

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class C : public A{
private:
        double b;                      // Parameter
public:
        // Constructor and destructor
       do_something_else(A* Beta,y){       // Also do_something_else evaluates 
                 return Beta->evaluate(y);     // function f(y)
       }
       double evaluate(double y){              // Evaluate function g(y)
	           A* Beta = new B(b);               // 
	           double f = Beta->evaluate(y);     // At first evaluate f(y)
                   double h = do_something_else(Beta,y);  // then evaluate h(f(y))
	           return h*f; 
       }
        friend class B;
}:
// Finally I need to evaluate function g(y) in the main
A* Celcius = C(b);
C->evaluate(y)

The question is that should I declare the function do_something_else somehow?
Last edited on
If do_something_else returns a double, you have to include the type in the function signature:

1
2
3
    double do_something_else(A* Beta, y) {
        return Beta->evaluate(y);
    }
I solved the problem. Thanks for everybody.
Topic archived. No new replies allowed.