c++做的DLL,全局变量的定义

hnrxrn 2016-11-17 04:24:42
c++做的DLL
请问下面红字要求如何实现。能够引用①的定义的值,谢谢
#
#
CSqrootApp::CSqrootApp()
{
double Data[100][100] // ①处,对Data[100][100] 进行赋值

}
CSqrootApp theApp;

extern "C" __declspec( dllexport) void MYUSER( int* pnNumberOfArgs,
double* x, double* dResult)
{
double data[100][100]; //这里需要 ①处的变量值,对1处的变量进行计算后输出
//-----------------

}

c++做的DLL
请问上面红字要求如何实现。能够引用①的定义的值,谢谢

...全文
1044 29 打赏 收藏 转发到动态 举报
写回复
用AI写文章
29 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2016-11-29
  • 打赏
  • 举报
回复
hnrxrn 2016-11-28
  • 打赏
  • 举报
回复
引用 27 楼 hnrxrn 的回复:
[quote=引用 25 楼 zhao4zhong1 的回复:] [code=c]// sqroot.h : main header file for the SQROOT DLL quote] qroot.cpp(67) error C2065: 'data' : undeclared identifier sqroot.cpp(67) : error C2109: subscript requires array or pointer type
解决了,谢谢
hnrxrn 2016-11-28
  • 打赏
  • 举报
回复
[quote=引用 25 楼 zhao4zhong1 的回复:] [code=c]// sqroot.h : main header file for the SQROOT DLL quote] qroot.cpp(67) error C2065: 'data' : undeclared identifier sqroot.cpp(67) : error C2109: subscript requires array or pointer type
hnrxrn 2016-11-28
  • 打赏
  • 举报
回复
qroot.cpp(67) error C2065: 'data' : undeclared identifier sqroot.cpp(67) : error C2109: subscript requires array or pointer type
赵4老师 2016-11-28
  • 打赏
  • 举报
回复
// sqroot.h : main header file for the SQROOT DLL
//

#ifndef __AFXWIN_H__
	#error include 'stdafx.h' before including this file for PCH
#endif

#include "resource.h"       // main symbols

/////////////////////////////////////////////////////////////////////////////
// CSqrootApp
// See sqroot.cpp for the implementation of this class
//

class CSqrootApp : public CWinApp
{
public:
	CSqrootApp();
    double data[200*200];

// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CSqrootApp)
	//}}AFX_VIRTUAL

	//{{AFX_MSG(CSqrootApp)
		// NOTE	- the ClassWizard will add and remove member functions here.
		//	  DO NOT EDIT what you see in these	blocks of generated	code !
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};


/////////////////////////////////////////////////////////////////////////////

// sqroot.cpp

#include "stdafx.h"
#include "sqroot.h"
#include <stdio.h>//

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include <math.h>
BEGIN_MESSAGE_MAP(CSqrootApp, CWinApp)
	//{{AFX_MSG_MAP(CSqrootApp)
		// NOTE	- the ClassWizard will add and remove mapping macros here.
		//	  DO NOT EDIT what you see in these	blocks of generated	code!
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

CSqrootApp::CSqrootApp()
{

// The constructor

   // Remove next line for a "quiet" version of MyUser.DLL
 FILE *f = fopen("c:\\1.txt", "r");//读取的数据,如果是CSV格式,下面的代码该如何写?
 FILE *p = fopen("c:\\data.txt","wt");
 int i, j;
 for (i = 0; i < 100; i++)
 for (j = 0; j < 58; j++)
 fscanf(f, "%lf", &data[i*200+j]);
  //for (i = 0; i < 1; i++)
  //for (j = 0; j < 100; j++)
 //for (j=0;j<100;j++)
  // printf("%lf \n",data[j*200+0]);
   for (j = 0; j < 10; j++)
   fprintf(p,"%lf \n",data[j*200+1]);
 fclose(p);
 fclose(f);
//加载就能读取1.txt的数据,以上已经尝试实现,但是我的数据是CSV格式的,又该如何写?
   AfxMessageBox("Plugin Loaded ");

}


CSqrootApp theApp;

extern "C" __declspec( dllexport) void MYUSER( int* pnNumberOfArgs, double* x, double* dResult)
{

	double result=0;
       int x0=x[0];

    switch(x0)
	{

     case 1000:
		//
		{

	//
			double a,b;

			a=x[1]/0.2;//x[1]任意正实数,横坐标,用公式算出x[1]在变量DATA【】【】中的位置,例如每0.2间隔一个数
			b=(x[2]-0.3)/0.1;//x[2]任意正实数,纵坐标,用公式算出x[2]在变量DATA【】【】中的位置,例如每0.1间隔一个数

       result=data[a*200+b];//此处返回CSqrootApp::CSqrootApp()中的变量data[][],要求给出DATA[][]的坐标,能返回他的值,
							  //但是有可能给出的x[1],x[2],计算出的a,b值不是整数,要求返回附近数据的平均值
							  //比如只有数据data[2][3],data[2][4],data[3][3],data[3][4],但是需要计算x[1]=0.5,x[2]=0.33时的DATA[][],如何写代码?

    
		}
	break;
    

	default:
    break;
	}
	*dResult =result;//返回结果

}
hnrxrn 2016-11-28
  • 打赏
  • 举报
回复
引用 23 楼 zhao4zhong1 的回复:
将sqroot.h贴出来。
// sqroot.h : main header file for the SQROOT DLL
//

#ifndef __AFXWIN_H__
	#error include 'stdafx.h' before including this file for PCH
#endif

#include "resource.h"		// main symbols

/////////////////////////////////////////////////////////////////////////////
// CSqrootApp
// See sqroot.cpp for the implementation of this class
//

class CSqrootApp : public CWinApp
{
public:
	CSqrootApp();

// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CSqrootApp)
	//}}AFX_VIRTUAL

	//{{AFX_MSG(CSqrootApp)
		// NOTE - the ClassWizard will add and remove member functions here.
		//    DO NOT EDIT what you see in these blocks of generated code !
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};


/////////////////////////////////////////////////////////////////////////////
赵4老师 2016-11-28
  • 打赏
  • 举报
回复
将sqroot.h贴出来。
赵4老师 2016-11-28
  • 打赏
  • 举报
回复
你在函数中声明的data只能在函数中使用 在class中声明,才能在构造函数和函数中使用 在class中声明 不能在构造函数中声明 不要使用二维数组 使用一维数组模拟二维数组 double data[200][200];→double data[200*200]; data[y][x]→data[y*200+x]
赵4老师 2016-11-28
  • 打赏
  • 举报
回复
参考18楼读取csv文件。
hnrxrn 2016-11-28
  • 打赏
  • 举报
回复
// sqroot.cpp

#include "stdafx.h"
#include "sqroot.h"
#include <stdio.h>//

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include <math.h>
extern double Data[100][100];
BEGIN_MESSAGE_MAP(CSqrootApp, CWinApp)
	//{{AFX_MSG_MAP(CSqrootApp)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

CSqrootApp::CSqrootApp()
{

// The constructor

   // Remove next line for a "quiet" version of MyUser.DLL
static double Data[200][200];
 FILE *f = fopen("c:\\1.txt", "r");//读取的数据,如果是CSV格式,下面的代码该如何写?
 FILE *p = fopen("c:\\data.txt","wt");
 int i, j;
 for (i = 0; i < 100; i++)
 for (j = 0; j < 58; j++)
 fscanf(f, "%lf", &Data[i][j]);
  //for (i = 0; i < 1; i++)
  //for (j = 0; j < 100; j++)
 //for (j=0;j<100;j++)
  // printf("%lf \n",Data[j][0]);
   for (j = 0; j < 10; j++)
   fprintf(p,"%lf \n",Data[j][1]);
 fclose(p); 
//加载就能读取1.txt的数据,以上已经尝试实现,但是我的数据是CSV格式的,又该如何写?
   AfxMessageBox("Plugin Loaded ");

}


CSqrootApp theApp;

extern "C" __declspec( dllexport) void MYUSER( int* pnNumberOfArgs,
 double* x, double* dResult)
{
    double data[200][200];
	
	double result=0;
       int x0=x[0];
	   
    switch(x0)
	{

     case 1000: 
		//
		{

	//	
			double a,b;

			a=x[1]/0.2;//x[1]任意正实数,横坐标,用公式算出x[1]在变量DATA【】【】中的位置,例如每0.2间隔一个数
			b=(x[2]-0.3)/0.1;//x[2]任意正实数,纵坐标,用公式算出x[2]在变量DATA【】【】中的位置,例如每0.1间隔一个数

       result=data[a][b];//此处返回CSqrootApp::CSqrootApp()中的变量data[][],要求给出DATA[][]的坐标,能返回他的值,
	                          //但是有可能给出的x[1],x[2],计算出的a,b值不是整数,要求返回附近数据的平均值
	                          //比如只有数据data[2][3],data[2][4],data[3][3],data[3][4],但是需要计算x[1]=0.5,x[2]=0.33时的DATA[][],如何写代码?

    
		}
	break;
    

	default:
    break;
	}
	*dResult =result;//返回结果

}
赵4老师 2016-11-28
  • 打赏
  • 举报
回复
使用TRACE辅助调试。 TRACE TRACE( exp ) Parameters exp Specifies a variable number of arguments that are used in exactly the same way that a variable number of arguments are used in the run-time function printf. Remarks Provides similar functionality to the printf function by sending a formatted string to a dump device such as a file or debug monitor. Like printf for C programs under MS-DOS, the TRACE macro is a convenient way to track the value of variables as your program executes. In the Debug environment, the TRACE macro output goes to afxDump. In the Release environment, it does nothing. TRACE is limited to sending a total of 512 characters at a time. If you call TRACE with formatting commands, the total string length after the formatting commands have been expanded cannot be more than 512 characters, including the terminating NULL. Exceeding this limit causes an ASSERT. Note This macro is available only in the debug version of MFC. For more information, seeMFC Debugging Support in Visual C++ Programmer’s Guide. Example // example for TRACE int i = 1; char sz[] = "one"; TRACE( "Integer = %d, String = %s\n", i, sz ); // Output: 'Integer = 1, String = one' See Also TRACE0, TRACE1, TRACE2, TRACE3, AfxDump, afxTraceEnabled
赵4老师 2016-11-28
  • 打赏
  • 举报
回复
http://bbs.csdn.net/topics/360055953
赵4老师 2016-11-28
  • 打赏
  • 举报
回复
static double data[200][200];//在占用内存空间较大的局部数组声明的前面加static将其从堆栈数据段挪到全局数据段即可避开因局部数组大小超过默认堆栈大小1MB造成程序不能正常运行的问题。
hnrxrn 2016-11-18
  • 打赏
  • 举报
回复
引用 12 楼 sunyongliang118 的回复:
[quote=引用 11 楼 hnrxrn 的回复:] [quote=引用 10 楼 sunyongliang118 的回复:] [quote=引用 8 楼 hnrxrn 的回复:] [quote=引用 7 楼 sunyongliang118 的回复:] 先问一下,你知道类的成员变量如何定义吗? 随手写了一个。没有认真校验。仅供参考。
不知道知道类的成员变量如何定义。 调用代码段 怎么放到这里,哪个位置 我要在这里用这个变量的值,进行计算。 extern "C" __declspec( dllexport) void MYUSER( int* pnNumberOfArgs, double* x, double* dResult) { double data[100][100]; //这里需要 ①处的变量值,对1处的变量进行计算后输出 //----------------- } [/quote] 调用代码段你不能放在动态库中。调用代码是和动态库两个独立个体。一般是可执行文件。 动态库是做什么的?明白吗?动态库是在编译时只提供实现函数或者类的声明,在运行时才去获取实现函数或者类的地址。动态库和静态库的本质区别是,静态库编译时是会和可执行文件合体在一起的。而动态库编译时,只是告诉可执行文件:你要调用的函数或者地址在哪里。只有在运行时,可执行文件才去前文的那个哪里的具体的地址去找首先函数或者类。所以动态库和可执行文件是两个独立部分,各不相干。动态库可被若干可执行同时调用。而静态库则是从一而终,跟谁一起编译就和谁结合在一起。 上面的代码我只是写了个简陋的例子,你要活学或活用 [/quote]
引用 10 楼 sunyongliang118 的回复:
[quote=引用 8 楼 hnrxrn 的回复:] [quote=引用 7 楼 sunyongliang118 的回复:] 先问一下,你知道类的成员变量如何定义吗? 随手写了一个。没有认真校验。仅供参考。
不知道知道类的成员变量如何定义。 调用代码段 怎么放到这里,哪个位置 我要在这里用这个变量的值,进行计算。 extern "C" __declspec( dllexport) void MYUSER( int* pnNumberOfArgs, double* x, double* dResult) { double data[100][100]; //这里需要 ①处的变量值,对1处的变量进行计算后输出 //----------------- } [/quote] 调用代码段你不能放在动态库中。调用代码是和动态库两个独立个体。一般是可执行文件。 动态库是做什么的?明白吗?动态库是在编译时只提供实现函数或者类的声明,在运行时才去获取实现函数或者类的地址。动态库和静态库的本质区别是,静态库编译时是会和可执行文件合体在一起的。而动态库编译时,只是告诉可执行文件:你要调用的函数或者地址在哪里。只有在运行时,可执行文件才去前文的那个哪里的具体的地址去找首先函数或者类。所以动态库和可执行文件是两个独立部分,各不相干。动态库可被若干可执行同时调用。而静态库则是从一而终,跟谁一起编译就和谁结合在一起。 上面的代码我只是写了个简陋的例子,你要活学或活用 [/quote] 我的这个DLL,能不能加载后就运行赋值部分(我已经可以实现,在CSqrootApp::CSqrootApp()中),。 我的难点是:在使用DLL需要调用函数的时候,再调用加载时赋值运行时赋值给DATA[][]的那个变量的地址呢? [/quote] 根本不理解你说的难点是什么[/quote] 一个计算软件中需要这个DLL: 这个DLL需要的功能是: 1,这个DLL能输出一个函数,就是 extern "C" __declspec( dllexport) void MYUSER( int* pnNumberOfArgs,),有模板,已经做好。 2、加载后立即立即运行变量赋值部分(赋值方式是从文本文件里读取,文件很大,避免反复读取,所以想只读取一次),以便后面使用。 3、当软件需要这DLL的输出函数时,这个函数可以对上面的变量进行计算
振翅高飞 2016-11-18
  • 打赏
  • 举报
回复
http://blog.csdn.net/lxh1230119/article/details/8071730 这是个动态库例子,虽然很简单,但是很完整。 你自己学习,思考。
振翅高飞 2016-11-18
  • 打赏
  • 举报
回复
引用 11 楼 hnrxrn 的回复:
[quote=引用 10 楼 sunyongliang118 的回复:] [quote=引用 8 楼 hnrxrn 的回复:] [quote=引用 7 楼 sunyongliang118 的回复:] 先问一下,你知道类的成员变量如何定义吗? 随手写了一个。没有认真校验。仅供参考。
不知道知道类的成员变量如何定义。 调用代码段 怎么放到这里,哪个位置 我要在这里用这个变量的值,进行计算。 extern "C" __declspec( dllexport) void MYUSER( int* pnNumberOfArgs, double* x, double* dResult) { double data[100][100]; //这里需要 ①处的变量值,对1处的变量进行计算后输出 //----------------- } [/quote] 调用代码段你不能放在动态库中。调用代码是和动态库两个独立个体。一般是可执行文件。 动态库是做什么的?明白吗?动态库是在编译时只提供实现函数或者类的声明,在运行时才去获取实现函数或者类的地址。动态库和静态库的本质区别是,静态库编译时是会和可执行文件合体在一起的。而动态库编译时,只是告诉可执行文件:你要调用的函数或者地址在哪里。只有在运行时,可执行文件才去前文的那个哪里的具体的地址去找首先函数或者类。所以动态库和可执行文件是两个独立部分,各不相干。动态库可被若干可执行同时调用。而静态库则是从一而终,跟谁一起编译就和谁结合在一起。 上面的代码我只是写了个简陋的例子,你要活学或活用 [/quote]
引用 10 楼 sunyongliang118 的回复:
[quote=引用 8 楼 hnrxrn 的回复:] [quote=引用 7 楼 sunyongliang118 的回复:] 先问一下,你知道类的成员变量如何定义吗? 随手写了一个。没有认真校验。仅供参考。
不知道知道类的成员变量如何定义。 调用代码段 怎么放到这里,哪个位置 我要在这里用这个变量的值,进行计算。 extern "C" __declspec( dllexport) void MYUSER( int* pnNumberOfArgs, double* x, double* dResult) { double data[100][100]; //这里需要 ①处的变量值,对1处的变量进行计算后输出 //----------------- } [/quote] 调用代码段你不能放在动态库中。调用代码是和动态库两个独立个体。一般是可执行文件。 动态库是做什么的?明白吗?动态库是在编译时只提供实现函数或者类的声明,在运行时才去获取实现函数或者类的地址。动态库和静态库的本质区别是,静态库编译时是会和可执行文件合体在一起的。而动态库编译时,只是告诉可执行文件:你要调用的函数或者地址在哪里。只有在运行时,可执行文件才去前文的那个哪里的具体的地址去找首先函数或者类。所以动态库和可执行文件是两个独立部分,各不相干。动态库可被若干可执行同时调用。而静态库则是从一而终,跟谁一起编译就和谁结合在一起。 上面的代码我只是写了个简陋的例子,你要活学或活用 [/quote] 我的这个DLL,能不能加载后就运行赋值部分(我已经可以实现,在CSqrootApp::CSqrootApp()中),。 我的难点是:在使用DLL需要调用函数的时候,再调用加载时赋值运行时赋值给DATA[][]的那个变量的地址呢? [/quote] 根本不理解你说的难点是什么
hnrxrn 2016-11-18
  • 打赏
  • 举报
回复
引用 10 楼 sunyongliang118 的回复:
[quote=引用 8 楼 hnrxrn 的回复:] [quote=引用 7 楼 sunyongliang118 的回复:] 先问一下,你知道类的成员变量如何定义吗? 随手写了一个。没有认真校验。仅供参考。
不知道知道类的成员变量如何定义。 调用代码段 怎么放到这里,哪个位置 我要在这里用这个变量的值,进行计算。 extern "C" __declspec( dllexport) void MYUSER( int* pnNumberOfArgs, double* x, double* dResult) { double data[100][100]; //这里需要 ①处的变量值,对1处的变量进行计算后输出 //----------------- } [/quote] 调用代码段你不能放在动态库中。调用代码是和动态库两个独立个体。一般是可执行文件。 动态库是做什么的?明白吗?动态库是在编译时只提供实现函数或者类的声明,在运行时才去获取实现函数或者类的地址。动态库和静态库的本质区别是,静态库编译时是会和可执行文件合体在一起的。而动态库编译时,只是告诉可执行文件:你要调用的函数或者地址在哪里。只有在运行时,可执行文件才去前文的那个哪里的具体的地址去找首先函数或者类。所以动态库和可执行文件是两个独立部分,各不相干。动态库可被若干可执行同时调用。而静态库则是从一而终,跟谁一起编译就和谁结合在一起。 上面的代码我只是写了个简陋的例子,你要活学或活用 [/quote]
引用 10 楼 sunyongliang118 的回复:
[quote=引用 8 楼 hnrxrn 的回复:] [quote=引用 7 楼 sunyongliang118 的回复:] 先问一下,你知道类的成员变量如何定义吗? 随手写了一个。没有认真校验。仅供参考。
不知道知道类的成员变量如何定义。 调用代码段 怎么放到这里,哪个位置 我要在这里用这个变量的值,进行计算。 extern "C" __declspec( dllexport) void MYUSER( int* pnNumberOfArgs, double* x, double* dResult) { double data[100][100]; //这里需要 ①处的变量值,对1处的变量进行计算后输出 //----------------- } [/quote] 调用代码段你不能放在动态库中。调用代码是和动态库两个独立个体。一般是可执行文件。 动态库是做什么的?明白吗?动态库是在编译时只提供实现函数或者类的声明,在运行时才去获取实现函数或者类的地址。动态库和静态库的本质区别是,静态库编译时是会和可执行文件合体在一起的。而动态库编译时,只是告诉可执行文件:你要调用的函数或者地址在哪里。只有在运行时,可执行文件才去前文的那个哪里的具体的地址去找首先函数或者类。所以动态库和可执行文件是两个独立部分,各不相干。动态库可被若干可执行同时调用。而静态库则是从一而终,跟谁一起编译就和谁结合在一起。 上面的代码我只是写了个简陋的例子,你要活学或活用 [/quote] 我的这个DLL,能不能加载后就运行赋值部分(我已经可以实现,在CSqrootApp::CSqrootApp()中),。 我的难点是:在使用DLL需要调用函数的时候,再调用加载时赋值运行时赋值给DATA[][]的那个变量的地址呢?
振翅高飞 2016-11-18
  • 打赏
  • 举报
回复
引用 8 楼 hnrxrn 的回复:
[quote=引用 7 楼 sunyongliang118 的回复:] 先问一下,你知道类的成员变量如何定义吗? 随手写了一个。没有认真校验。仅供参考。
不知道知道类的成员变量如何定义。 调用代码段 怎么放到这里,哪个位置 我要在这里用这个变量的值,进行计算。 extern "C" __declspec( dllexport) void MYUSER( int* pnNumberOfArgs, double* x, double* dResult) { double data[100][100]; //这里需要 ①处的变量值,对1处的变量进行计算后输出 //----------------- } [/quote] 调用代码段你不能放在动态库中。调用代码是和动态库两个独立个体。一般是可执行文件。 动态库是做什么的?明白吗?动态库是在编译时只提供实现函数或者类的声明,在运行时才去获取实现函数或者类的地址。动态库和静态库的本质区别是,静态库编译时是会和可执行文件合体在一起的。而动态库编译时,只是告诉可执行文件:你要调用的函数或者地址在哪里。只有在运行时,可执行文件才去前文的那个哪里的具体的地址去找首先函数或者类。所以动态库和可执行文件是两个独立部分,各不相干。动态库可被若干可执行同时调用。而静态库则是从一而终,跟谁一起编译就和谁结合在一起。 上面的代码我只是写了个简陋的例子,你要活学或活用
振翅高飞 2016-11-18
  • 打赏
  • 举报
回复
纠错一下: printf("data[%d][%d]%d\n",i,j,pdata[i][j]); 改为 printf("data[%d][%d]=%lf\n",i,j,pdata[i][j]);
hnrxrn 2016-11-18
  • 打赏
  • 举报
回复
引用 7 楼 sunyongliang118 的回复:
先问一下,你知道类的成员变量如何定义吗? 随手写了一个。没有认真校验。仅供参考。
不知道知道类的成员变量如何定义。 调用代码段 怎么放到这里,哪个位置 我要在这里用这个变量的值,进行计算。 extern "C" __declspec( dllexport) void MYUSER( int* pnNumberOfArgs, double* x, double* dResult) { double data[100][100]; //这里需要 ①处的变量值,对1处的变量进行计算后输出 //----------------- }
加载更多回复(9)

64,691

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧