0

你怎么能在 Windows VS2010 中做到这一点?

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

嘘:

#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 中,jh:

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

1 回答 1

1

class MyMathFuncs2定义的末尾j.h,请参见:

}

尝试将其更改为:

};
于 2013-04-25T22:15:13.917 回答