Solved

function pointer cdecl to thiscall problem

Posted on 2010-08-31
8
645 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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
 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

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. …
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

809 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