Solved

function pointer cdecl to thiscall problem

Posted on 2010-08-31
8
643 Views
Last Modified: 2013-12-14
#include<iostream.h>
#include "cl2h.h"


nsur.cpp
------------
void main()
      {


      pcl::disp(&sum);

      }
cl1h.h
-----------
class pcl1
      {public:
      double  sum(double a,double b)
            {
            double c=a+b;
            }
      };

cl2h.h
--------
#include<iostream.h>
#include "cl1h.h"
double (pcl1::*pt2)(double,double)=&pcl1::sum;
class pcl
      {
      public:
      void disp(double (*pt2) (double,double))
            {
            double c=(*pt2)(20,30);
            cout<<"sum"<<c;
            }
      };


When I compile I get error
nsur.cpp(11) : error C2065: 'sum' : undeclared identifier
Could you please identify it
0
Comment
Question by:vallikumari
  • 4
  • 3
8 Comments
 
LVL 30

Expert Comment

by:Zoppo
ID: 33565505
Hi vallikumari,

IMO there are some problems in the code:

1. 'pcl::disp(&sum);' in 'main()' has to be 'pcl::disp(&pcl::sum);'
2. Make sure 'cl2h.h' is included in 'nsur.cpp'
3. Both function calls ('pcl::disp(&sum);' and 'double c=(*pt2)(20,30);') are written as if you intended to use static functions instead of member functions - so you either have to implement these functions to be static functions or you need to instantiate objects of the classes and call the functions against them ...
4. 'pcl1::sum' needs to return the value 'c'

ZOPPO
0
 

Author Comment

by:vallikumari
ID: 33565633
#include<iostream.h>
#include "cl2h.h"
void main()
      {
      pcl::disp(&pcl::sum);
      }
#include<iostream.h>
#include "cl1h.h"

class pcl
      {
      public:
            
            
      void disp(double (*pt2) (double,double))
            {
            pcl1 p;
            double c=p.(*pt2)(20,30);
            cout<<"sum"<<c;
            }
      };
class pcl1
      {
      public:
 double   sum(double a,double b)
            {
            double c=a+b;
      return c;
      }
      };
modified the code as per you have said but, then it is giving the following errors
c:\documents and settings\administrator\desktop\npro\cl2h.h(12) : error C2059: syntax error : '('
c:\documents and settings\administrator\desktop\npro\cl2h.h(13) : error C2039: 'cout' : is not a member of 'pcl1'
        c:\documents and settings\administrator\desktop\npro\cl1h.h(2) : see declaration of 'pcl1'
c:\documents and settings\administrator\desktop\npro\cl2h.h(13) : error C2297: '<<' : illegal, right operand has type 'char [4]'
c:\documents and settings\administrator\desktop\npro\cl2h.h(13) : error C2039: 'c' : is not a member of 'pcl1'
        c:\documents and settings\administrator\desktop\npro\cl1h.h(2) : see declaration of 'pcl1'
c:\documents and settings\administrator\desktop\npro\nsur.cpp(8) : error C2039: 'pcl' : is not a member of 'pcl1'
        c:\documents and settings\administrator\desktop\npro\cl1h.h(2) : see declaration of 'pcl1'
c:\documents and settings\administrator\desktop\npro\nsur.cpp(8) : error C2039: 'pcl' : is not a member of 'pcl1'
        c:\documents and settings\administrator\desktop\npro\cl1h.h(2) : see declaration of 'pcl1'
c:\documents and settings\administrator\desktop\npro\nsur.cpp(8) : error C2653: 'pcl' : is not a class or namespace name
c:\documents and settings\administrator\desktop\npro\nsur.cpp(8) : error C2039: 'disp' : is not a member of 'pcl1'
        c:\documents and settings\administrator\desktop\npro\cl1h.h(2) : see declaration of 'pcl1'
c:\documents and settings\administrator\desktop\npro\nsur.cpp(8) : error C2039: 'pcl' : is not a member of 'pcl1'
        c:\documents and settings\administrator\desktop\npro\cl1h.h(2) : see declaration of 'pcl1'
c:\documents and settings\administrator\desktop\npro\nsur.cpp(8) : error C2039: 'pcl' : is not a member of 'pcl1'
        c:\documents and settings\administrator\desktop\npro\cl1h.h(2) : see declaration of 'pcl1'
c:\documents and settings\administrator\desktop\npro\nsur.cpp(8) : error C2653: 'pcl' : is not a class or namespace name
Error executing cl.exe.
0
 
LVL 30

Accepted Solution

by:
Zoppo earned 50 total points
ID: 33565677
'cout' is declared in namespace 'std', so you need to use 'std::cout'.

In the declaration of 'disp' IMO you need to declare 'pt2' to be a member function of 'pcl', somehow like 'void disp(double (pcl::*pt2) (double,double))'

Depending on you VisualStudio-version you should include <iostream> instead of <iostream.h>.
0
 
LVL 8

Assisted Solution

by:Pramod Bugudai
Pramod Bugudai earned 50 total points
ID: 33575614
Hi. All the latest VC++ Compiler uses #include<iostream>. In that you need to include

using namespace std;  

This will remove all name confusion like std::cout.

regards
pramod

0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 30

Expert Comment

by:Zoppo
ID: 33575690
Well, the use of 'using namespace std;' is a matter of many discussions - I myself don't like it, at least not on a file-global scope; if any I use it within a function or a class ...

If you're interested in a detailed discussion about the 'using namespace std;' you can take a look here: http://www.experts-exchange.com/Q_24646218.html
0
 

Assisted Solution

by:vallikumari
vallikumari earned 0 total points
ID: 34674117
I have solved this
0
 

Author Comment

by:vallikumari
ID: 34674154
aa
0
 

Author Closing Comment

by:vallikumari
ID: 34709714
no
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

706 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now