my code are as follows ,path1.updatePathInfo() is an function which call an empty function,if delete this it will works fine.I could’t understand.
////////////////////////////////////////////////////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));
}
}
CPath::getPathByCode()does not return a value and the result is undefined behaviour. Your compiler is not required to tell you about this, but if you compile with all warnings enabled it almost certainly will.As an aside, if you formatted your code correctly, this sort of error would be much easier to see.