c ++在dll中实现在dll外部定义的虚拟类

时间:2013-04-25 21:45:57

标签: c++ visual-studio-2010 dll

如何在Windows VS2010中执行此操作?

同一解决方案下的2个项目:第一个是带有两个文件的常规控制台应用程序:s.h和j.cpp。第二个项目是一个带有一个头文件的dll应用程序:j.h

S.H:

#ifndef M
#define M

class MyMathFuncs
    {
    public: 
        // Returns a + b
        virtual double Add(double a, double b) const = 0; 

        // Returns a - b
        virtual double Subtract(double a, double b) const = 0; 

        // Returns a * b
        virtual double Multiply(double a, double b) const = 0; 

        // Returns a / b
        // Throws const std::invalid_argument& if b is 0
        virtual double Divide(double a, double b) const = 0; 
    };

double MyMathFuncs::Add(double a, double b) const{}; 

        // Returns a - b
 double MyMathFuncs::Subtract(double a, double b) const{}; 

        // Returns a * b
 double MyMathFuncs::Multiply(double a, double b) const{}; 

        // Returns a / b
        // Throws const std::invalid_argument& if b is 0
 double MyMathFuncs::Divide(double a, double b) const{}; 

j.cpp:

#include "s.h"
#include "j.h"

#include <iostream>
using namespace std;

int main()
{
    double a = 7.4;
    int b = 99;
    MyMathFuncs2 k;
    cout << "a + b = " <<
        k.Add(a, b) << endl;
    cout << "a - b = " <<
        k.Subtract(a, b) << endl;
    cout << "a * b = " <<
        k.Multiply(a, b) << endl;
    cout << "a / b = " <<
        k.Divide(a, b) << endl;

    try
    {
        cout << "a / 0 = " <<
            k.Divide(a, 0) << endl; 
    }
    catch (const invalid_argument &e) 
    {
        cout << "Caught exception: " << e.what() << endl; 
    }
    char c;
    cin >> c;
    return 0;
}

在dll中,j.h:

#ifndef dllheader
#define dllheader

#include "s.h"
#include <stdexcept>

class MyMathFuncs2: public MyMathFuncs {

public:
     __declspec(dllexport) double Add(double a, double b) const  {
        return a + b;
    }

        // Returns a - b
     __declspec(dllexport) double Subtract(double a, double b) const {
        return a - b;
    }

        // Returns a * b
     __declspec(dllexport) double Multiply(double a, double b) const {
        return a * b * b * b;
    } 

        // Returns a / b
        // Throws const std::invalid_argument& if b is 0
     __declspec(dllexport) double Divide(double a, double b) const {
        if (b == 0)
        {
            //throw invalid_argument("b cannot be zero!");
        }

        return a / b;
    }
}



#endif

dll会编译,但控制台应用程序不会:

files (x86)\microsoft visual studio 10.0\vc\include\typeinfo(36): error C2236: unexpected 'struct' '__type_info_node'. Did you forget a ';'?
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\typeinfo(36): error C2143: syntax error : missing ';' before '{'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\typeinfo(36): error C2447: '{' : missing function header (old-style formal list?)
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\typeinfo(41): error C2146: syntax error : missing ';' before identifier '__type_info_root_node'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\typeinfo(41): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\typeinfo(41): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\typeinfo(67): error C2061: syntax error : identifier '__type_info_node'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\typeinfo(74): error C2061: syntax error : ide

1 个答案:

答案 0 :(得分:1)

class MyMathFuncs2的{​​{1}}定义的最后,请看:

j.h

尝试将其更改为:

}