473,806 Members | 2,967 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help needed with scope resolution

1 New Member
I have a piece of code in which I pass a function to a function and it works fine in stand alone code but when I encorporate it in a class my compiler tells me there is no matching function. The error message is:

test.C: In member function ‘void Field::Evaluate ()’:
test.C:29: error: no matching function for call to ‘Field:: Diff(double&, <unknown type>)’
test.C:18: note: candidates are: double Field:: Diff(double, double (*)(double))

Why does it not recogize the type? What can I do to correct the situation? I think it is because the function MyMathFunction is not defined in the scope of the function Diff but I don't know how to fix that. I wrote a small sample of code that exhibits the behavior I am trying to correct. My compiler version is
g++ (GCC) 4.0.2 20051125 (Red Hat 4.0.2-8)
Thanks in advance.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2. #include <math.h>
  3.  
  4. class Field{
  5.  public:
  6.  
  7. double MyMathFunction1( double );
  8. // In the function below I pass x and f(x) separately
  9. double Diff( double, double(*)(double) ); 
  10.  
  11.  void Evaluate();
  12. };
  13.  
  14. double Field::MyMathFunction1(double x){  return sin(x);}
  15. double Field::Diff( double x, double(*func)(double) )
  16. {
  17.   // The idea is that func(x) becomes  MyMathFunction1(x) 
  18.  
  19.   double h=1e-6;
  20.   return (func(x+h)-func(x-h))/2./h;
  21. }
  22. void Field::Evaluate(){
  23.   // Use some dummy value of x for testing
  24.   double x=1.5707963;
  25.   // pass the function MyMathfunction1(x) to Diff(double,double(*)() )
  26.   cout << "Dsin(x)|x="<<x<<":\t"<<Diff( x, MyMathFunction1 )<<endl;
  27. }
  28.  
  29. int main()
  30. {
  31.  
  32.   Field * DoAnalysis = new Field;
  33.   DoAnalysis->Evaluate();
  34.  
  35.   return 0;
  36. }
  37.  
Oct 15 '06 #1
1 1465
tyreld
144 New Member
Why are you using a function pointer when the function is already a member of the class? Why not just call the function directly?
Oct 16 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
2657
by: exits funnel | last post by:
Hello, I'm confused by the use of the Scope resolution operator on the indicated lines in the following code (which was copied from Thinking in C++ by Bruce Eckel). Removing them seems to have no effect. //Begin Code #include <new> // Size_t definition #include <fstream> using namespace std;
0
1102
by: Howard Gardner | last post by:
/* I don't have to provide scope resolution in sit 0 below: adl finds the function that I mean to use. I do have to provide scope resolution in sits 1 and 2. Can anyone teach me a trick to get around the need for scope resolution in sit 1 or sit 2? sit 1 is my motivating case, but if there's a trick for sit 2 I'd like
3
4420
by: richard pickworth | last post by:
Hello :) I am familiar with using scope resolution operator to define classes, but why does it turn up elsewhere? thanks richard
11
1930
by: milkyway | last post by:
Hello, I have an HTML page that I am trying to import 2 .js file (I created) into. These files are: row_functions.js and data_check_functions.js. Whenever I bring the contents of the files into this HTML file, all is OK but whenever the functions are separated (as it is now), when I run the page, I get the following error: Line 73, object expected.
16
1839
by: sushant | last post by:
hi , can we use scope resolution operator (::) in C??? sushant
6
5404
by: Jack | last post by:
I want to write code that can be compiled as C and C++. In the Windows API, there are many macros that help with scope resolution. For example: #ifdef __cplusplus #define SNDMSG ::SendMessage #else #define SNDMSG SendMessage #endif
7
315
by: John Salerno | last post by:
I have the following code: class DataAccessFrame(wx.Frame): menu_items = def __init__(self): wx.Frame.__init__(self, None, title='Database Access Panel')
6
1865
by: Subra | last post by:
Hi, If I have the same varible defined in global as well as in local scope, how to access global scope varible in a function having the same local def. #include<stdio.h> static int a=25; int main() {
5
1652
by: Shan | last post by:
In the following code: func() { int SmeVar; { int SmeVar; //Here I want to use SmeVar defined in the outer block how can I do that ? }
0
9597
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10620
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10369
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10110
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7650
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6877
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5682
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.