Solved

function pointer cdecl to thiscall problem

Posted on 2010-08-31
8
644 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 31

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 31

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 31

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
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 NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

929 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

10 Experts available now in Live!

Get 1:1 Help Now