Exception thrown: read access violation. **this** was nullptr. occurred - c++

I am a novice at C++. I have started writing a rather large code on a project and I want to make a good base for that. I get the following error when running in Visual Studio:
Exception thrown: read access violation. this was nullptr. occurred
After searching similar questions, I could not find the solution for my code. The error is occurring in Driver.cpp at:
m_setup->SetEquation(...)
Any help is really appreciated. I also appreciate if you have suggestion to improve my code structure.
Main.h:
#ifndef FEM_AD1D_MAIN_H
#define FEM_AD1D_MAIN_H
namespace FEM_AD1D
{
class Driver;
Driver* m_driver;
}
#endif
Main.cpp:
#include "Main.h"
#include "Driver.h"
#include <iostream>
using namespace FEM_AD1D;
int main()
{
std::cout << "Hello World!\n";
m_driver->Run();
}
Driver.h:
#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
Driver.cpp:
#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.h:
#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
Setup.cpp:
#include "Setup.h"
#include "Driver.h"
#include <iostream>
#include <vector>
using namespace FEM_AD1D;
Setup::Setup() :
m_nDim(0),
m_aCoef(0.0),
m_bCoef(0.0),
m_cCoef(0.0),
m_fCoef(0.0)
{
}
void Setup::SetEquation(int nDim, double aCoef, double bCoef, double cCoef, double fCoef)
{
// Set the coefficients of the equation
std::cout << "Hello World 3!\n";
m_nDim = nDim;
m_aCoef = aCoef;
m_bCoef = bCoef;
m_cCoef = cCoef;
m_fCoef = fCoef;
}

Related

clang: error: linker command failed with exit code 1 (use -v to see invocation)

I am getting a linking error with my two C++ files as seen below.
Compile error
It compiles up to the point of two object files then fails on linking.
Below is my code, I commented out most of main in order to isolate the issue.
I am using the boost/JSON library. The duplicates are all from the boost JSON library.
The JSONParser file is going to take in a JSON object and turn it to a C++ object but I don't think anything to do with the actual code is the issue. Just either a library or header file issue.
main.cpp
#include "JSONParser.hpp"
/*
#include <boost/beast/core.hpp>
#include <boost/beast.hpp>
#include <boost/beast/ssl.hpp>
#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>
#include <iostream>
#include <string>
#include <thread>
namespace net = boost::asio;
namespace beast = boost::beast;
using namespace boost::beast;
using namespace boost::beast::websocket;
*/
int main (void) {
//net::io_context ioc;
//tcp_stream sock(ioc);
//net::ssl::context ctx(net::ssl::context::tlsv12);
}
JSONParser.hpp
#ifndef JSONPARSER_H
#define JSONPARSER_H
#define BOOST_JSON_STACK_BUFFER_SIZE 1024
#include <boost/json/src.hpp>
class candlestick{
private:
double openPrice;
double closePrice;
double priceHigh;
double priceLow;
double baseVolume;
double quoteVolume;
bool isClosed;
public:
candlestick (double oP, double cP, double pH, double pL, double bV, double qV, bool iC);
void setopenPrice(double p) {
openPrice = p;
}
double getopenPrice(void) {
return openPrice;
}
void setclosePrice(double p) {
closePrice = p;
}
double getclosePrice(void) {
return closePrice;
}
void setpriceHigh(double p) {
priceHigh = p;
}
double getpriceHigh(void) {
return priceHigh;
}
void setpriceLow(double p) {
priceLow = p;
}
double getpriceLow(void) {
return priceLow;
}
void setbaseVolume(double v) {
baseVolume = v;
}
double getbaseVolume(void) {
return baseVolume;
}
void setquoteVolume(double v) {
quoteVolume = v;
}
double getquoteVolume(void) {
return quoteVolume;
}
void setClosed(bool c) {
isClosed = c;
}
bool getClosed(void) {
return isClosed;
}
};
candlestick::candlestick (double oP, double cP, double pH, double pL, double bV, double qV, bool iC) {
openPrice = oP;
closePrice = cP;
priceHigh = pH;
priceLow = pL;
baseVolume = bV;
quoteVolume = qV;
isClosed = iC;
};
candlestick createCandlestickObject ();
#endif
JSONParser.cpp
#include "JSONParser.hpp"
#include <iostream>
#include <sstream>
#include <string>
candlestick createCandlestickObject () {
candlestick c (2, 2, 2, 2, 2, 2, true);
return c;
}
By commenting out the #include "JSONParser.hpp" out of main, I was able to remove the error, therefore definitely a linking issue.

C++: undefined reference to Constructor

I just started working with C++ and am working on an exercise that deals with polymorphic pointers. I'm having trouble trying to solve an error message I believe I'm getting from my Rectangle.cpp as I call the class from my main.cpp.
The error message:
undefined reference to 'Rectangle::Rectangle(double, double)'
Main.cpp
#include <iostream>
#include "Rectangle.h"
using namespace std;
//////////////////////////////////////////////
// --- FUNCTIONS DECLARATION---
void introduceShape(Shape*);
double calculateShapeArea(Shape*);
double calculateShapeCircumferece(Shape*);
int main()
{
Rectangle rectangle1(5,2);
// Rectangle *rec1 = new Rectangle(5,2);
introduceShape(&rectangle1);
cout << "My area is: " << calculateShapeArea(&rectangle1) << ", my circumference is: " << calculateShapeCircumferece(&rectangle1) << endl << endl;
return 0;
}
//////////////////////////////////////////////
// --- FUNCTIONS ---
void introduceShape(Shape* shapeToIntroduce)
{
return shapeToIntroduce->introduce();
}
double calculateShapeArea(Shape* shapeToCalculate)
{
return shapeToCalculate->calculateArea();
}
double calculateShapeCircumferece(Shape* shapeToCalculate)
{
return shapeToCalculate->calculateCircumference();
}
Rectangle.h
#ifndef RECTANGLE_H_INCLUDED
#define RECTANGLE_H_INCLUDED
#include "Shape.h"
#include <iostream>
using namespace std;
class Rectangle: public Shape
{
double width;
double height;
public:
Rectangle(double , double );
void introduce();
double calculateArea();
double calculateCircumference();
};
#endif // RECTANGLE_H_INCLUDED
Rectangle.cpp
#include "Rectangle.h"
#include <iostream>
using namespace std;
Rectangle::Rectangle(double width, double height)
{
this->width = width;
this->height = height;
}
void Rectangle::introduce()
{
cout << "I AM A RECTANGLE !" << endl;
}
double Rectangle::calculateArea()
{
return width*height;
}
double Rectangle::calculateCircumference()
{
return 2*(width+height);
}
Shape.h
#ifndef SHAPE_H_INCLUDED
#define SHAPE_H_INCLUDED
class Shape
{
public:
virtual void introduce() = 0;
virtual double calculateArea() = 0;
virtual double calculateCircumference() = 0;
};
#endif // SHAPE_H_INCLUDED
The error is generated by the linker because it can not see where the definition of the constructor is located.
If you are using an IDE, you should add .cpp file to the project so that it can be compiled and the definition would be found by the linker. It not, then you have to compile it yourself -assuming you are using gcc:
g++ Rectangle.cpp
will combine cpp files into one executable and should not show you that error.
Visit this post

Multiple files function calls C++

I'm writing a program that calculates the norm of a vector (dot product with itself).
I have no problem implementing the code, what I can't do is call a function from a function other than the main one.
header.h
#ifndef HEADER
#define HEADER
void readArray(double [], int &);
void printArray(double [], int &);
void norm(double [], int &);
double scalarProduct(double [], int &);
#endif
norm.cc
// norm.cc
#include <iostream>
#include <cmath>
using namespace std;
void norm(double array[], int & size)
{
double norm;
norm = sqrt(scalarProduct(array, size));
cout << "Norm = " << norm << endl;
}
scalarProduct.cc
// scalarProduct.cc
#include <cmath>
double scalarProduct(double array[], int & size)
{
double ps = 0.0;
for(int i = 0; i < size; i++)
{
ps += pow(array[i], 2);
}
}
in the main.cc file I added the line
#include "header.h"
and all the functions I call from main work like a charm, but calling productScalar() from norm() doesn't work. I added the same #include "header.h" line but the compiler says I can't define the same function more than once. How can I solve this?
Add
#include "header.h"
into norm.cc

C++ biginteger class operator=

I am trying to implement a biginteger class, and after I created a biginteger class, with a proper header file, and at first I am trying to define a operator=() operator, so when I make a new biginteger object, I will be able to make it equals with a integer.
This is the main.cpp:
#include <iostream>
#include "bigint.h"
using namespace std;
int main()
{
bigint bela = 15;
cout << "Hello world!" << bela.mennyi() <<endl;
return 0;
}
And this is the biginteger header:
#ifndef BIGINT_H
#define BIGINT_H
#include <vector>
#include <iostream>
class bigint
{
public:
bigint();
void operator=(const int &a);
int mennyi();
protected:
private:
std::vector<int> numarray;
};
#endif // BIGINT_H
And the biginteger.cpp file:
#include "bigint.h"
#include <iostream>
using namespace std;
bigint::bigint()
{
numarray.resize(0);
}
void bigint::operator=(const int &a)
{
int b = a;
if(b >= 0)
{
numarray.resize(0);
while(b!=0){
numarray.push_back(b%10);
b = b/10;
}
}
}
int bigint::mennyi()
{
int ki = 0;
for(int i = (numarray.size())-1; i>=0; i--)
{
ki = ki*10 + numarray[i];
}
return ki;
}
When I start the debugging I get an error saying: conversion from 'int' to non-scalar type 'bigint' requested.
You should implement this constructor:
bigint::bigint(int);

C++ GUI - 'Marshal' : is not a class or namespace name

Any and all help is greatly appreciate. Thank you for taking the time out to review my issue.
I am currently receiving an errors
1>c:\users\fordn_000\documents\tcc_odu\it310\programs\it310_homework_program_2_nford\it310_homework_program_2_nford\Form1.h(625): error C2653: 'Marshal' : is not a class or namespace name
1>c:\users\fordn_000\documents\tcc_odu\it 310\programs\it310_homework_program_2_nford\it310_homework_program_2_nford\Form1.h(625): error C3861: 'StringToHGlobalAnsi': identifier not found
This is my GUI form code, I want to use the command marshal however, that appears where there error is taking place
private: System::Void DisplayButton_Click(System::Object^ sender, System::EventArgs^ e)
{
int InitProductID = 0;
char* InitDescription;
int InitManufID = 0;
double InitWholeSale = 0.0;
double InitMarkup = 0.0;
int InitQuanity = 0;
String^ TypeString;
//EXTRACT FROM INPUT TEXT BOX'S
InitProductID = Convert::ToInt32(ProductIDNumberBoxNew->Text);
InitDescription = (char*)(void*)Marshal::StringToHGlobalAnsi(DescriptionBox->Text);
InitManufID = Convert::ToInt32(ManufacturerBox->Text);
InitWholeSale = Convert::ToDouble(WholesalePriceBox->Text);
InitMarkup = Convert::ToDouble(MarkupBox->Text);
InitQuanity = Convert::ToInt32(QuantityBox->Text);
//CREATE INSTANCE OF CLASS
Inventory InventoryItem(InitProductID, InitDescription, InitManufID, InitWholeSale, InitMarkup, InitQuanity);
//DISPLAY TO OUTPUT TEXT BOXS
ProductIDNumberOutBox->Text = Convert::ToString(InventoryItem.GetProductID());
TypeString=gcnew String(InventoryItem.GetDescription());
ManufacturerOutBox->Text = Convert::ToString(InventoryItem.GetManufID());
//RETAIL PRICE OUTBOX
QuantityOutBox->Text= Convert::ToString(InventoryItem.GetQuanity());
}
This is my stdafx header file below
#pragma once
// TODO: reference additional headers your program requires here
#include "Inventory.h"
This is my stdafx cpp file below
#include "stdafx.h"
#include "Form1.h"
Finally this is my inventory header file
//SPECIFICATION FILE (INVENTORY.H)
#ifndef INVENTORY_H
#define INVENTORY_H
#include <iostream>
#include <iomanip>
using namespace std;
class Inventory
{
private:
int ProductID;
mutable char Description[25];
int ManufID;
double WholeSale;
double Markup;
int Quanity;
public:
//CONSTRUCTORS
Inventory( );
Inventory(int, char[], int, double, double, int);
//GET FUNCTIONS
int GetProductID( )const;
char* GetDescription( )const;
int GetManufID( )const;
double GetWholeSale( )const;
double GetMarkup( )const;
int GetQuanity( )const;
//DISPLAY FUNCTION
void Display( )const;
//RETURN FUNCTION
double RetailPrice( )const;
};
#endif
I think you need to reference this:
using namespace System::Runtime::InteropServices;