|
★ 小木虫: 金币+0.5, 给个红包,谢谢回帖
我是个初学者,编程过程中遇到不问题。可能有些还很低级,请不要笑我,我现在把一个vs2010开发的有错误的MFC程序移植到vc++6.0上。其他的地方都修改没错误了,就是有一处关于vector的修改不对。我怀疑是不是更高级别的编译器已经支持了vector一些新的操作。
请你给予改正。我是个菜鸟。请把修改后的工程发给我也可以,在vc++6.0上的。因为本人机器低级啊 ,更高级别的编译器运行不起来。多多互相学习!!qq825290827
#include "stdafx.h"
#include "ModeRecognition.h"
#include "ModeRecognitionDlg.h"
#include "math.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg:oDataExchange(CDataExchange* pDX)
{
CDialog:oDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CModeRecognitionDlg dialog
CModeRecognitionDlg::CModeRecognitionDlg(CWnd* pParent /*=NULL*/)
: CDialog(CModeRecognitionDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CModeRecognitionDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CModeRecognitionDlg:oDataExchange(CDataExchange* pDX)
{
CDialog:oDataExchange(pDX);
//{{AFX_DATA_MAP(CModeRecognitionDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CModeRecognitionDlg, CDialog)
//{{AFX_MSG_MAP(CModeRecognitionDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CModeRecognitionDlg message handlers
BOOL CModeRecognitionDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CModeRecognitionDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CModeRecognitionDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
CWnd* pdc=GetDlgItem(IDC_STATIC_PIC);
CDC* hdc=pdc->GetDC();
CPen pen(PS_SOLID,3,RGB(0,0,0));
CPen pen_grid(PS_SOLID,2,RGB(0,0,0));
CPen* oldPen=hdc->SelectObject(&pen);
CString str;
for(int i=0;i<=300;i+=30)
{
int k=i/30;
str.Format(_T("%d",k);
if(k<=9)
{
hdc->TextOut(-10,300-i,str);
hdc->TextOut(i,301,str);
}
else
{
hdc->TextOut(-17,300-i,str);
hdc->TextOut(i,301,str);
}
}
hdc->MoveTo(0,300);
hdc->LineTo(0,-10);
hdc->LineTo(-6,-4);
hdc->MoveTo(0,-10);
hdc->LineTo(6,-4);
hdc->MoveTo(0,300);
hdc->LineTo(310,300);
hdc->LineTo(304,296);
hdc->MoveTo(310,300);
hdc->LineTo(304,304);
hdc->SelectObject(&pen_grid);
for(int j=0;j<=300;j+=30)
{
hdc->MoveTo(0,j);
hdc->LineTo(300,j);
hdc->MoveTo(j,0);
hdc->LineTo(j,300);
}
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CModeRecognitionDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CModeRecognitionDlg::OnButton1()
{
// TODO: Add your control notification handler code here
UpdateData();
if(m_num>80)
{
AfxMessageBox(_T("样本数过多");
return;
}
else if(m_num == 0)
{
AfxMessageBox(_T("样本数为0");
return;
}
rightsam.clear();
clasSample.clear();
Invalidate();
OnPaint();
/* InvalidateRect(CRect(0,0,300,300));*/
/* Invalidate();*/
/* UpdateWindow();*/
// OnPaint();
// CWnd* pdc=GetDlgItem(IDC_STATIC_PIC);
// CDC* hdc=pdc->GetDC();
// //设置画笔颜色
// CPen pen(PS_SOLID,2,RGB(0,0,255));
// CPen* oldpen=hdc->SelectObject(&pen);
// CRect rect;
int x,y;
samples temp;
srand(time(NULL));
vector testSam;
for(int i=0;i<300;i++)
{
x=rand()%10;
y=rand()%10;
temp.x=x;
temp.y=y;
testSam.push_back(temp);
}
int count=testSam.size();
BOOL tag=FALSE;
for(int k=count-1;k>=1;k--)
{
for(int j=k-1;k>=0;k--)
{
if(testSam[k].x==testSam[j].x&&testSam[k].y==testSam[j].y)
{
tag=TRUE;
break;
}
}
if(tag)
{
tag=FALSE;
testSam.pop_back();
count--;
}
else
{
rightsam.push_back(testSam);
testSam.pop_back();
count--;
}
}
testSam.clear();
DrawSample(rightsam,m_num,0);
}
void CModeRecognitionDlg:rawSample(vector vec,int samnum,int cc)
{
CWnd* pdc=GetDlgItem(IDC_STATIC_PIC);
CDC* hdc=pdc->GetDC();
//设置画笔颜色
CPen pen(PS_SOLID,2,m_ref[cc]);
hdc->SelectObject(&pen);
/*CPen* oldpen=hdc->SelectObject(&pen);*/
CRect rect;
int count1=vec.size();
if(count1
for(int i=0;i
{
rect.left=30*vec.x-5;
rect.top=300-30*vec.y-5;
rect.right=30*vec.x+5;
rect.bottom=300-30*vec.y+5;
hdc->Rectangle(rect);
}
}
void CModeRecognitionDlg::OnButton2()
{
// TODO: Add your control notification handler code here
SetWindowText(_T("DAGE");
UpdateData();
if(m_num == 0)
{
AfxMessageBox(_T("没有样本");
return;
}
else if(m_num
{
AfxMessageBox(_T("样本数小于类数");
return;
}
vector center;//聚类中心
if(m_classnum==0)
{
AfxMessageBox(_T("请别考验自己,类别为0,不用聚了");
return;
}
else if(m_classnum>10)
{
AfxMessageBox(_T("类数过多");
return;
}
else
{
/*Invalidate();*/
/* InvalidateRect(CRect(0,0,300,300));*/
/*UpdateWindow();*/
/* OnPaint();*/
for(int i=0;i
center.push_back(rightsam);
}
vector test;
if(m_num>=rightsam.size())m_num=rightsam.size();
for(int i=0;i
test.push_back(rightsam);
vector t1;
julei(center,test);
vector new_center;
new_center=newcenter(test);
while(!judge(new_center,center))
{
center=new_center;
julei(new_center,test);
new_center=newcenter(test);
}
OnPaint();
for(int m=0;m
{
vector temp;
temp.clear();
for(int j=0;j
{
temp.push_back(test[clasSample[m][j]]);
}
DrawSample(temp,clasSample[m].size(),m+1);
}
center.clear();
new_center.clear();
test.clear();
}
void CModeRecognitionDlg::julei(vector cnet,vector test)
{
clasSample.clear();
clasSample.resize(cnet.size(),eachClas);
vector m_distant;
float t=0.0;
for(int i=0;i!=test.size();i++)
{
m_distant.clear();
for(int j=0;j!=cnet.size();j++)
{
float temp=distant(test,cnet[j]);
m_distant.push_back(temp);
}
float min_v=find_min(m_distant);
for(int jj=0;jj
{
if(min_v==m_distant[jj])
{
clasSample[jj].push_back(i);
break;
}
}
}
}
float CModeRecognitionDlg::find_min(vector c1)
{
if(!c1.size())return 0.00;
else
{
for(int i=0;i
for(int j=i+1;j
{
if(c1>c1[j])
{
float temp=c1;
c1=c1[j];
c1[j]=temp;
}
}
return c1[0];
}
}
float CModeRecognitionDlg::distant(samples s1,samples s2)
{
float t=fabs(float(s1.x-s2.x));
float tt=fabs(float(s1.y-s2.y));
return (t+tt);
}
bool CModeRecognitionDlg::judge(vector vec1,vector vec2)
{
int n=vec1.size();
samples fvec1={0,0},fvec2={0,0};
for(int i=0;i
{
fvec1.x+=vec1.x;
fvec1.y+=vec1.y;
fvec2.x+=vec2.x;
fvec2.y+=vec2.y;
}
if((fvec1.x==fvec2.x)&&(fvec1.y==fvec2.y))
return true;
else
return false;
}
vector CModeRecognitionDlg::newcenter(vector test)
{
vector temp;
int nCount=clasSample.size();
for(int i=0;i
{
float tx=0.0,ty=0.0;
for(int j=0;j.size();j++)
{
tx+=test[clasSample[j]].x;
ty+=test[clasSample[j]].y;
}
tx/=clasSample.size();
ty/=clasSample.size();
samples txty;
txty.x=tx,txty.y=ty;
temp.push_back(txty);
}
return temp;
}
void CModeRecognitionDlg::OnButton3()
{
// TODO: Add your control notification handler code here
}
下面是主要出错的程序段:
for(int m=0;m
{
vector temp;
temp.clear();
for(int j=0;j
{
temp.push_back(test[clasSample[m][j]]);
}
DrawSample(temp,clasSample[m].size(),m+1);
}
center.clear();
new_center.clear();
test.clear();
}
编译结果如下:
D:\c++程序\ModeRecognition\ModeRecognitionDlg.cpp(376) : error C2228: left of '.size' must have class/struct/union type
D:\c++程序\ModeRecognition\ModeRecognitionDlg.cpp(378) : error C2109: subscript requires array or pointer type
D:\c++程序\ModeRecognition\ModeRecognitionDlg.cpp(380) : error C2228: left of '.size' must have class/struct/union type
D:\c++程序\ModeRecognition\ModeRecognitionDlg.cpp(392) : error C2664: 'resize' : cannot convert parameter 2 from 'class std::vector >' to 'const int &'
Reason: cannot convert from 'class std::vector >' to 'const int'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
D:\c++程序\ModeRecognition\ModeRecognitionDlg.cpp(409) : error C2228: left of '.push_back' must have class/struct/union type
D:\c++程序\ModeRecognition\ModeRecognitionDlg.cpp(465) : error C2228: left of '.size' must have class/struct/union type
D:\c++程序\ModeRecognition\ModeRecognitionDlg.cpp(467) : error C2109: subscript requires array or pointer type
D:\c++程序\ModeRecognition\ModeRecognitionDlg.cpp(468) : error C2109: subscript requires array or pointer type
D:\c++程序\ModeRecognition\ModeRecognitionDlg.cpp(470) : error C2228: left of '.size' must have class/struct/union type
D:\c++程序\ModeRecognition\ModeRecognitionDlg.cpp(471) : error C2228: left of '.size' must have class/struct/union type
Generating Code...
Compiling...
ModeRecognition.cpp
Generating Code...
执行 cl.exe 时出错. |
|