C++ Object reference not set to an instance of an object - c++

When I try to compile the following program it says "Build failed. Object reference not set to an instance of an object" . I'm kinda new to c++ so if anybody can help me it'll be great . I'm just trying out some example I saw in a book so I don't know whats wrong with this .
using namespace std;
class matrix
{
int m[3][3];
public:
void read(void);
void display(void);
friend matrix trans(matrix);
}
void matrix :: read(void)
{
cout<<"Enter the elements of the 3x3 array matrix : \n";
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<"m["<<i<<"]["<<j<<"] =";
cin>>m[i][j];
}
}
}
void matrix :: display(void)
{
int i,j;
for(i=0;i<3;i++)
{
cout<<"\n";
for(j=0;j<3;j++)
{
cout<<m[i][j]<<"\t";
}
}
}
matrix trans(matrix m1)
{
matrix m2;
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
m2.m[i][j] = m1.m[j][i];
}
}
return(m2); //returning an object
}
int main()
{
matrix mat1,mat2;
mat1.read();
cout<<"\nYou entered the following matrix :";
mat1.display();
mat2 = trans(mat1);
cout<<"\nTransposed matrix :";
mat2.display();
getch();
return 0;
}

1 - Insert semi-colon after the class definition
2 - Insert the correct headers
#include <iostream>
#include <conio.h>
3 - Try getting a compiler that is a bit more descriptive with regards to errors. I did all that i mentioned and your program ran. Try it

It compiles fine after you fix your missing semi-colon (after your class declaration) and add either #include <conio.h> (Visual Studio) or #include <curses.h> (for a POSIX system) for the getch() function (which is not a standard function).

Related

Hello, This simple C++ script pops out an error at line 11. Does anybody know how to fix it?

Description
This code is intended to find a spanner index (i) when dealing with
Splines/NURBS basis functions based on a knot vector (U), a choosen knot (u), the degree of the desired curve (p) and the number of basis funcions (n). The algorithm was taken from the NURBS Book by Piegl and Tiller. The error, I guess, is in the way I declared variable U. Thaks in advanced!
code
# include <iostream>
using namespace std;
int n=3;
int p=2;
double u=5/2;
int U[11]={0,0,0,1,2,3,4,4,5,5,5};
int FindSpan(n,p,u,U) /* an error in this line */
{
if (u==U[n+1]) return (n);
low=p; high=n+1;
mid=(low+high)/2
while(u<U[mid] || u>=U[mid+1])
{
if (u<U[mid]) high=mid;
else low=mid;
mid=(low+high)/2
}
return (mid);
}
You have forgotten some semicolons and types!
here is the correct code:
#include <iostream>
using namespace std;
int n=3;
int p=2;
double u=5/2;
int U[11]={0,0,0,1,2,3,4,4,5,5,5};
int FindSpan(int n, int p, int u, int U[])
{
if (u==U[n+1]) return (n);
int low=p, high=n+1;
int mid=(low+high)/2;
while(u<U[mid] || u>=U[mid+1])
{
if (u<U[mid]) high=mid;
else low=mid;
mid=(low+high)/2;
}
return (mid);
}
int main() {
cout << FindSpan(n, p, u, U);
return 0;
}

How to draw a rectangle using a char parameter in C++?

I want to draw and fill a rectangle using C++. The function parameter passed in must be a char, not an int. In my header file the drawing function is this:
void draw(char);
My rectangle.cpp file is this:
void rectangle::draw(char )
{
for(height=0;height<=height;height++)
{
for(width=1;width<=width;width++)
{
cout<<'*';
}
}
}
My main.cpp file is this:
rectangle d1;
d1.draw(char);
When I run the program it gives me the error:
Expected primary expression before 'char'.
I'm using Code::Blocks 13.12. Any ideas to solve this problem?
Your draw is missing a variable. Change draw(char); to draw(char c);
And both for loops need a maximum range, not the same value as the incrementing variable. height<=height; and width<=width;
#include <iostream>
using namespace std;
void draw(char c )
{
int rheight=10;
int rwidth=20;
for(int height=0;height<=rheight;height++)
{
for(int width=1;width<=rwidth;width++)
{
cout<<c;
}
cout<<"\n";
}
}
int main()
{
draw('*');
cout<<"\n";
return 0;
}

Array Initialization problems: Unexpected behavior

The following program builds perfectly. However, during execution, no matter what value of degree I provide, the program takes only 2 array elements as input. I suppose there might be a problem with the redeclaration of the arrays f[] and fDash[]. In JAVA, arrays can be easily redeclared using the new keyword. Is that possible in c++ too? If not, what is the alternative?
P.S. I am using CodeBlocks 13.12 and compiler settings are standard.
#include <iostream>
#include <cmath>
using namespace std;
class Polynomial
{
public:
void input(void);
void expression(void);
void derivative(void);
double value(double var);
double der(double var);
private:
int f[];
int fDash[];
int degree;
};
void Polynomial::input()
{
cout<<"Enter degree of polynomial:\t";
cin>>degree;
f[degree+1];
fDash[degree];
for(int i=0;i<=degree;i++)
{
cout<<"Enter coefficient of x^"<<i<<":\t";
cin>>f[i];
}
for(int i=0;i<degree;i++)
{
fDash[i]=f[i+1]*(i+1);
}
}
void Polynomial::expression()
{
cout<<f[0];
for(int i=1;i<=degree;i++)
{
cout<<" + "<<f[i]<<"*x^"<<i;
}
}
void Polynomial::derivative()
{
cout<<fDash[0];
for(int i=1;i<degree;i++)
{
cout<<" + "<<fDash[i]<<"*x^"<<i;
}
}
double Polynomial::value(double var)
{
double val=0.0;
for(int i=0;i<=degree;i++)
{
val+=f[i]*pow(var,i);
}
return val;
}
double Polynomial::der(double var)
{
double val=0.0;
for(int i=0;i<degree;i++)
{
val+=fDash[i]*pow(var,i);
}
return val;
}
int main()
{
double lb,ub,step,var,accum=0.0,rms;
int counter=0;
Polynomial p;
p.input();
cout<<"\n\n\nPolynomial is:\nf(x) = ";
p.expression();
cout<<"\n\n\nDerivative is:\nf'(x) = ";
p.derivative();
cout<<"\n\n\nEnter x0,x1,Step:\t";
cin>>lb;
cin>>ub;
cin>>step;
cout<<"\n\n\n====================================";
cout<<"\n\nx\t|\tf\t|\tf'\n\n\n";
var=lb;
while(var<=ub)
{
cout<<var<<"\t|\t"<<p.value(var)<<"\t|\t"<<p.der(var)<<"\n";
accum+=pow(p.value(var),2.0);
var+=step;
counter++;
}
cout<<"\n====================================";
accum/=counter;
rms=sqrt(accum);
cout<<"\nRMS energy of f(x) = "<<rms;
return 0;
}
This does not compile on clang, it fails with "error: field has incomplete type 'int []' int f[];" and likewise for fDash.
Let's see how you declared these fields:
int f[];
int fDash[];
In C++, you can declare arrays with statically defined sizes like so:
int f[5];
int fDash[6];
If you want dynamic arrays, which you need in this case, you'd have to declare
int* f;
int* fDash;
and allocate memory for them with
f = new int[5];
You also must release that memory somewhere like so
delete [] f;
But beware - managing your own memory like this is error prone and should be avoided. You should just use std::vector instead, which is the equivalent of java.util.ArrayList:
std::vector<int> f;
std::vector<int> fDash;
And modify your input function like so:
void Polynomial::input()
{
cout<<"Enter degree of polynomial:\t";
cin>>degree;
int input;
for(int i=0;i<=degree;i++)
{
cout<<"Enter coefficient of x^"<<i<<":\t";
cin>>input;
f.push_back(input);
}
for(int i=0;i<degree;i++)
{
fDash.push_back(f[i+1]*(i+1));
}
}
You don't use arrays correctly. You need to allocate memory if you want to use array of variable length. How use arrays in c++ see this and this, or use std::vector

(C++) Does not stop on std::cin and goes into infinity loop?

I have a wird problem. Im using Visual Studio 2012, thats my code: (something is in polish but i hope you will understand how it works).
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <queue>
using namespace std;
#pragma warning (disable: 4996);
struct strona
{
int zawartosc;
int gdzie;
};
void Wyswietl(int tab[], int dlugosc)
{
cout<<"Tablica wyglada tak"<<endl;
for(int i=0;i<dlugosc;i++)
{
cout<<tab[i]<<"\t";
}
cout<<endl;
}
int main()
{
int ileStron=3;
int *tablicaStron, *tablicaBitowOdniesienia;
tablicaStron=new int[ileStron];
tablicaBitowOdniesienia=new int[ileStron];
queue <strona> kolejka;
char opcja='a';
while(opcja!='k')
{
cout<<"(D)odawac (K)oniec (W)yswietl";
cin>>opcja;//DONT STOP THE PROGRAM!
if(opcja=='D'|opcja=='d')
{
strona tymczas;
cout<<"Podaj co dodać do kolejki";
cin>>tymczas.zawartosc;
int licznik=0;
if(kolejka.size()<ileStron)
{
tymczas.gdzie=kolejka.size();
kolejka.push(tymczas);
tablicaStron[tymczas.gdzie]=tymczas.zawartosc;
}
else if(kolejka.size()==ileStron)
{
cout<<"bang bang";
int czyJest=0;
int licznikfora=0;
for(int i=0;i<ileStron;i++)//sprawdza czy wpisywana strona nie istnieje przypadkiem w tablicy stron
{
if(tablicaStron[i]==tymczas.zawartosc)
{
czyJest=1;
}
licznikfora++;
}
cout<<"czyJest ma wartosc "<<czyJest<<" a licznik fora "<<licznikfora<<endl;
if(czyJest==0)
{
tymczas.gdzie=kolejka.front().gdzie;
kolejka.pop();//TUTAJ SIE BEDZIE ZAPISYWAC DO PAMIECI WIRTUALNEJ
kolejka.push(tymczas);
tablicaStron[tymczas.gdzie]=tymczas.zawartosc;
}
else if(czyJest==1)
{
cout<<"to co chcesz dodac juz jest w pamieci";
}
}
else
{
cout<<"rozmiar kolejki sie nie zgadza";
}
}
else if(opcja=='W'|opcja=='w')
{
Wyswietl(tablicaStron,ileStron);
cout<<endl;
cout<<"pierwszy element w kol: "<<kolejka.front().zawartosc<<"|"<<kolejka.front().gdzie<<" "
<<"ostatni element w kol: "<<kolejka.back().zawartosc<<"|"<<kolejka.back().gdzie<<endl;
}
}
system("pause");
return 0;
}
The problem is that after choosing option (d) - just type d and press enter, then type any few letters and the program should show you
(D)odawac (K)oniec (W)yswietl
but it is starting to loop to infinity...
What is the problem?
I think problem is with this statement:
cin>>tymczas.zawartosc;
At this point if you give a char or string input, program blows up.
to make your program work properly:
start the program and give only one input d, let the control hit this point : cin>>tymczas.zawartosc;
now input a number here, as it is int type. now control hits first cin.
The problem is with using cin to read int but user inputs a char or string. Behaviour is undefined in this case.
This link should resolve confusion: C++ character to int

Newbie - matrix addition implementation in c++

Hello i'm trying to program the addition of 2 matrices into a new one (and it does when i run the program step by step) but for some reason VS 2010 gives me an access error after it does the addition.
Here is the code.
#include <iostream>
#include <cstdio>
#include <conio>
using namespace std;
class operatii
{
typedef double mat[5][5];
mat ms,m1,m2;
int x1,x2,y1,y2;
public:
void preg();
int cit_val();
void cit_mat(int&,int&,double[5][5]);
void suma();
void afisare(int&,int&,double[5][5]);
};
void operatii::preg()
{
cit_mat(x1,y1,m1);
cit_mat(x2,y2,m2);
suma();
afisare(x1,y1,ms);
}
int operatii::cit_val()
{
int n;
cin>>n;
return n;
}
void operatii::cit_mat(int& x,int& y,double m[5][5])
{
char r;
cout<<"Matrice patratica? Y/N ";
cin>>r;
if ((r=='y')||(r=='Y'))
{
cout<<"Numar linii si coloane: ";
x=cit_val();
y=x;
}
else
{
cout<<"Numar linii: ";
x=cit_val();
cout<<"Numar coloane: ";
y=cit_val();
}
for (int i=1;i<=x;i++)
for (int j=1;j<=y;j++)
cin>>m[i][j];
}
void operatii::suma()
{
if ((x1==x2)&&(y1==y2))
for (int i=1;i<=x1;i++)
for (int j=1;i<=y1;j++)
ms[i][j]=m1[i][j]+m2[i][j];
else cout<<"Eroare";
}
void operatii::afisare(int& x,int& y,double m[5][5])
{
cout<<endl;
for (int i=1;i<=x;i++)
{
for (int j=1;j<=y;j++)
cout<<m[i][j];
cout<<endl;
}
}
void main()
{
operatii matrice;
matrice.preg();
system("PAUSE");
}
Any kind of help would be apreciated.
Arrays are 0-based in c++.
Change your various variants of for (somevar=1; somevar<=something) to for (somevar=0; somevar<something)
You're writing past the end of your arrays, which overwrites stack return address, leading to a return to nonexecutable code, again leading to an access violation.
Also,
for (int j=1;i<=y1;j++)
I think you want to use j not i here. Such errors are much easier to see if you use longer and more distinct variable names than "i" and "j", such as e.g. "Line" and "Column"