我是C ++的新手。我已经开始在项目上编写相当大的代码,并且我想为此做一个良好的基础。在Visual Studio中运行时出现以下错误:
抛出异常:读取访问冲突。 此是nullptr。发生
搜索类似的问题后,我找不到我的代码的解决方案。该错误发生在Driver.cpp中,位于: m_setup-> SetEquation(...)
我们非常感谢您的帮助。如果您有改善我的代码结构的建议,我也将不胜感激。
Main.h:
Date b1 b2 b3 b4 vals
0 2003 5 0 4 3 5.0
1 2003 2 2 1 8 2.0
2 2004 2 3 1 1 3.0
3 2004 1 8 2 1 8.0
4 2005 2 1 6 2 6.0
5 2006 1 7 2 9 9.0
Main.cpp:
#ifndef FEM_AD1D_MAIN_H
#define FEM_AD1D_MAIN_H
namespace FEM_AD1D
{
class Driver;
Driver* m_driver;
}
#endif
Driver.h:
#include "Main.h"
#include "Driver.h"
#include <iostream>
using namespace FEM_AD1D;
int main()
{
std::cout << "Hello World!\n";
m_driver->Run();
}
Driver.cpp:
#ifndef FEM_AD1D_DRIVER_H
#define FEM_AD1D_DRIVER_H
namespace FEM_AD1D
{
class Setup;
class Driver
{
public:
Driver(Setup* setup);
void Run();
void PreProc();
private:
Setup* m_setup;
const int m_nDim = 1;
const double m_aCoef = 1.0;
const double m_bCoef = 3.0;
const double m_cCoef = 0.0;
const double m_fCoef = 1.0;
const double m_xMin = 0.0;
const double m_xMax = 1.0;
const int m_nElem = 5;
const int m_elemType = 2;
const double m_meshStretch = 0.0;
const int m_nGaussPoint = 3;
};
}
#endif
Setup.h:
#include "Driver.h"
#include "Setup.h"
#include <iostream>
#include <array>
using namespace FEM_AD1D;
Driver::Driver(Setup* setup)
{
m_setup = setup;
//m_setup = new Setup();
}
void Driver::Run()
{
PreProc();
}
void Driver::PreProc()
{
std::cout << "Hello World 2!\n";
m_setup->SetEquation(m_nDim, m_aCoef, m_bCoef, m_cCoef, m_fCoef);
}
Setup.cpp:
#ifndef FEM_AD1D_SETUP_H
#define FEM_AD1D_SETUP_H
#include <vector>
namespace FEM_AD1D
{
class Setup
{
public:
Setup();
void SetEquation(int nDim, double aCoef, double bCoef, double cCoef, double fCoef);
private:
int m_nDim;
double m_aCoef;
double m_bCoef;
double m_cCoef;
double m_fCoef;
};
}
#endif