I'm trying to write a simple application to get acquainted with dll projects in C++. I've followed the example at MSDN first: http://msdn.microsoft.com/en-us/library/ms235636(v=vs.90).aspx
It works well, so I tried one step further. Here is my code:
// MathFuncsDll.h
#include <vector>
namespace MathFuncs
{
// This class is exported from the MathFuncsDll.dll
class MyMathFuncs
{
public:
// Returns a + b
static __declspec(dllexport) double Add(double a, double b);
// Returns a - b
static __declspec(dllexport) double Subtract(double a, double b);
// Returns a * b
static __declspec(dllexport) double Multiply(double a, double b);
// Returns a / b
// Throws const std::invalid_argument& if b is 0
static __declspec(dllexport) double Divide(double a, double b);
static __declspec(dllexport) double Sumproduct(vector<double>* a, vector<double>* b);
// **Here I use std::vector**
};
}
And in MathFuncsDll.cpp
:
// MathFuncsDll.cpp : Defines the exported functions for the DLL application.
#include "stdafx.h"
#include "MathFuncsDll.h"
#include <stdexcept>
#include <vector>
using namespace std;
namespace MathFuncs
{
double MyMathFuncs::Add(double a, double b)
{
return a + b;
}
double MyMathFuncs::Subtract(double a, double b)
{
return a - b;
}
double MyMathFuncs::Multiply(double a, double b)
{
return a * b;
}
double MyMathFuncs::Divide(double a, double b)
{
if (b == 0)
{
throw invalid_argument("b cannot be zero!");
}
return a / b;
}
// **Here is the definition of Sumproduct**
double MyMathFuncs::Sumproduct(vector<double>* a, vector<double>* b)
{
int size1 = a->size(), size2 = b->size();
if(size1 != size2)
{
throw invalid_argument("The length of input vectors should be the same!");
}
double sum = 0;
for(int i = 0; i < size1; i++)
sum += a->at(i) * b->at(i);
return sum;
}
}
And in main
:
// MyExecRefsDll.cpp
// compile with: /EHsc /link MathFuncsDll.lib
#include <iostream>
#include "MathFuncsDll.h"
using namespace std;
int main()
{
double a = 7.4;
int b = 99;
cout << "a + b = " <<
MathFuncs::MyMathFuncs::Add(a, b) << endl;
cout << "a - b = " <<
MathFuncs::MyMathFuncs::Subtract(a, b) << endl;
cout << "a * b = " <<
MathFuncs::MyMathFuncs::Multiply(a, b) << endl;
cout << "a / b = " <<
MathFuncs::MyMathFuncs::Divide(a, b) << endl;
try
{
cout << "a / 0 = " <<
MathFuncs::MyMathFuncs::Divide(a, 0) << endl;
}
catch (const invalid_argument &e)
{
cout << "Caught exception: " << e.what() << endl;
}
system("pause");
return 0;
}
When I try to build the solution with VS2012, it gives following error messages
error C2511: 'double MathFuncs::MyMathFuncs::Sumproduct(std::vector<_Ty> *,std::vector<_Ty> *)' : overloaded member function not found in 'MathFuncs::MyMathFuncs'
error C2061: syntax error : identifier 'vector'
I am very new to dll projects, so please forgive my ignorance. May I know what am I wrong?
Many thanks in advance.
=============================================================
EDIT1:
I think it could be easier to find an answer by giving a big picture. I am planning to write some .dll files and call them from Python code. As @Chad suggest, it is not a good idea to do in the above approach since it will force the user (which is me) to use the same implementation as STL.
Then, may I know, in order to achieve my plan, if there is a better way to do it?
Thank you very much. :)
==============================================================
EDIT2:
Thanks to all for your kind help. Especially thanks to @Anodyne. Now my origin question is perfectly answered by Anodyne. However, as I stated in EDIT1
, now I have to consider for future cooperation between C++ dll
and Python
.
Any suggestions will be welcome! Many thanks again! :)
PS: should I open a new thread for this?