求C++ Builder 6.0中调用C++某头文件中的函数的解决办法
求C++ Builder 6.0中调用C++某头文件中的函数的解决方法!
怎样在C++ Builder 6.0中的Unit1.cpp文件中将两个Edit组件的值作为参数,调用C++头文件a.h中的函数not_digit()
我的想法是这样:
在Unit1.cpp文件中引用"a.h"头文件(包括一些必要的stirng等引用),然后在Unit1.cpp中编写如下代码:
char c = not_digit(Edit1->Text);
就是要将Edit1组件的值传进去函数not_digit()中,然后返回一个值,它在头文件a.h中,原型如下:
char not_digit(const string &all_s)
{}
问题来了,运行之后这行代码提示错误:char c = not_digit(Edit1->Text);
我在网上找了一种方法,就是用指针,如下:
char *a;
a = Edit1->Text.c_str();
char c = not_digit(a);
虽然那一行没出错,但是在函数not_digit()函数里却起不了作用,无论在Edit1组件里面输入什么值,函数not_digit()都返回字符0(即'0').
整个函数如下所示:
char not_digit(const string &all_s)
{
char flag_not = '0';
for(string::size_type i = 0; i != all_s.size(); ++i)
{
char ch = all_s[i];
if(isalpha(ch))
{
flag_not = 'a';
break;
}
else if(ispunct(ch))
{
if(ch == '.' && atof(all_s.c_str()) - atoi(all_s.c_str()) != 0)
{
flag_not = '.';
}
else
{
flag_not = '*';
}
break;
}
else if(isspace(ch))
{;
flag_not = ' ';
break;
}
else
{
continue;
}
}
return flag_not;
}
求有经验的人帮帮忙!在此先谢谢热心回答的朋友们了!
------解决方案--------------------
新建了一个工程,放入2个Edit, 一个Button,然后用你的代码,
可以工作,输入1234 就在Edit2显示0 abc显示a ----显示*
//---------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
#include <string>
using namespace std;
char not_digit(const string &all_s)
{
char flag_not = '0';
for(string::size_type i = 0; i != all_s.size(); ++i)
{
char ch = all_s[i];
if(isalpha(ch))
{
flag_not = 'a';
break;
}
else if(ispunct(ch))
{
if(ch == '.' && atof(all_s.c_str()) - atoi(all_s.c_str()) != 0)
{
flag_not = '.';
}
else
{
flag_not = '*';
}
break;
}
else if(isspace(ch))
{;
flag_not = ' ';
break;
}
else
{
continue;
}
}
return flag_not;
}
//---------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
char *a;
a = Edit1->Text.c_str();
char c = not_digit(a);
Edit2->Text = String(c);
}
//---------------------------------------
怎样在C++ Builder 6.0中的Unit1.cpp文件中将两个Edit组件的值作为参数,调用C++头文件a.h中的函数not_digit()
我的想法是这样:
在Unit1.cpp文件中引用"a.h"头文件(包括一些必要的stirng等引用),然后在Unit1.cpp中编写如下代码:
char c = not_digit(Edit1->Text);
就是要将Edit1组件的值传进去函数not_digit()中,然后返回一个值,它在头文件a.h中,原型如下:
char not_digit(const string &all_s)
{}
问题来了,运行之后这行代码提示错误:char c = not_digit(Edit1->Text);
我在网上找了一种方法,就是用指针,如下:
char *a;
a = Edit1->Text.c_str();
char c = not_digit(a);
虽然那一行没出错,但是在函数not_digit()函数里却起不了作用,无论在Edit1组件里面输入什么值,函数not_digit()都返回字符0(即'0').
整个函数如下所示:
char not_digit(const string &all_s)
{
char flag_not = '0';
for(string::size_type i = 0; i != all_s.size(); ++i)
{
char ch = all_s[i];
if(isalpha(ch))
{
flag_not = 'a';
break;
}
else if(ispunct(ch))
{
if(ch == '.' && atof(all_s.c_str()) - atoi(all_s.c_str()) != 0)
{
flag_not = '.';
}
else
{
flag_not = '*';
}
break;
}
else if(isspace(ch))
{;
flag_not = ' ';
break;
}
else
{
continue;
}
}
return flag_not;
}
求有经验的人帮帮忙!在此先谢谢热心回答的朋友们了!
------解决方案--------------------
新建了一个工程,放入2个Edit, 一个Button,然后用你的代码,
可以工作,输入1234 就在Edit2显示0 abc显示a ----显示*
//---------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
#include <string>
using namespace std;
char not_digit(const string &all_s)
{
char flag_not = '0';
for(string::size_type i = 0; i != all_s.size(); ++i)
{
char ch = all_s[i];
if(isalpha(ch))
{
flag_not = 'a';
break;
}
else if(ispunct(ch))
{
if(ch == '.' && atof(all_s.c_str()) - atoi(all_s.c_str()) != 0)
{
flag_not = '.';
}
else
{
flag_not = '*';
}
break;
}
else if(isspace(ch))
{;
flag_not = ' ';
break;
}
else
{
continue;
}
}
return flag_not;
}
//---------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
char *a;
a = Edit1->Text.c_str();
char c = not_digit(a);
Edit2->Text = String(c);
}
//---------------------------------------