[英]C++ Object reference not set to an instance of an object
當我嘗試編譯以下程序時,它會顯示“構建失敗。對象引用未設置為對象的實例”。 我對C ++有點陌生,所以如果有人可以幫助我,那就太好了。 我只是在嘗試一些我在書中看到的例子,所以我不知道這有什么問題。
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;
}
在修復丟失的分號(在類聲明之后)並為getch()
添加#include <conio.h>
(Visual Studio)或#include <curses.h>
(對於POSIX系統)之后,它可以正常編譯功能(這不是標准功能)。
1-在類定義后插入分號
2-插入正確的標題
#include <iostream>
#include <conio.h>
3-嘗試獲得對錯誤的描述更具描述性的編譯器。 我做了我提到的所有事情,您的程序就運行了。 試試吧
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.