为什么这个C++程序运行得这么奇怪,程序在添加空函数时就死了
why this C++ program runs so strange,the program died when added a empty function
我的代码如下,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();
}
}
///
#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_
//.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()
不返回值,结果是未定义的行为。 您的编译器不需要告诉您有关此的信息,但是如果您在启用所有警告的情况下进行编译,则几乎肯定会告诉您。
顺便说一句,如果您正确格式化了代码,则此类错误将更容易看到。
相关文章:
- C++调用具有 *this 属性的单个帮助程序函数
- 用于检查值是否为其任何参数的帮助程序函数
- 无法将 2D 数组传递到 c++ 中的帮助程序函数中
- C2783: 无法推断帮助程序函数的模板参数
- 友元函数需要一个帮助程序函数
- 在何处放置与类方法相关的帮助程序函数C++
- 输入处理程序函数中的分段错误
- 当要测试的函数需要用户输入时,如何制作驱动程序函数?
- 类参数化与类和帮助程序函数混淆
- 是否有一个Windows驱动程序函数可以执行Windows文件api SeFileAttributes的等效功能
- 实用程序函数不会在 .h 文件中初始化并在 main 中定义.cpp
- 在 boost::d eadline_timer 上创建包装器,无法将参数传递给处理程序函数
- boost::asio::async_read 不会回调我的处理程序函数
- 帮助程序函数应该放在标头中还是在实现文件中
- JNA 程序函数查找失败
- C++读取器函数设计:帮助程序函数、类、私有、过度工程
- 等待从获取锁的函数调用的帮助程序函数中的条件变量
- C++程序函数的一些问题
- 使用 lambda 函数定义非常小的帮助程序函数是一种好风格吗?
- 在程序函数中创建对象数组