我的代码如下,path1.updatePathInfo() 是一个调用空函数的函数,如果删除它就可以正常工作。我不明白。
////////////////////////////////////////////////////main.cpp
#include <iostream>
#include<vector>
#include "CPath.h"
using namespace std;
int main()
{
for(int i = 0;i<10;++i)
{
cout<<i;
CPath path1;
CPath path2;
cross(path1,path2);
path1.updatePathInfo(); //this is an function which call an empty function
//if I delete this it works fine.I could't understand.
path2.updatePathInfo();
}
}
///////////////////////////////////////// ///////////CPath.h
#ifndef CPATH_H_
#define CPATH_H_
#include<iostream>
#include<vector>
#include<fstream>
#include<map>
#include<cmath>
#include<cstdlib>
class CPath
{
public:
CPath();
friend void cross(CPath & path1,CPath & path2);
void updatePathInfo();
std::vector< int > getPathByCode();
void createRadomCode();
std::vector< int > code;
double pathLineLength;
};
#endif //CPATH_H_
///////////////////////////////////////// //////////////////CPath.cpp
#include"CPath.h"
CPath::CPath()
{
createRadomCode();
}
void cross(CPath & path1,CPath & path2)
{
int max,min;
max = rand()%8;
min = rand()%8;
if(min > max)
{
max ^= min;min ^= max;max ^= min;
}
if(0.9*100>rand()%100)
{
for(int i = min;i<=max;++i)
{
path1.code[i]^=path2.code[i];
path2.code[i]^=path1.code[i];
path1.code[i]^=path2.code[i];
}
}
}
void CPath::updatePathInfo()
{
getPathByCode();
}
std::vector< int > CPath::getPathByCode()
{
//this function has nothing
}
void CPath::createRadomCode()
{
for(int i = 0 ; i<8 ; ++i)
{
code.push_back(rand()%(i+1));
}
}