C++ 对象引用未设置为对象的实例

标签 c++ visual-c++ 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;
}

最佳答案

1 - 在类定义后插入分号
2 - 插入正确的标题

   #include <iostream>
   #include <conio.h>

3 - 尝试获得一个在错误方面更具描述性的编译器。我做了我提到的所有事情,你的程序运行了。试试吧

关于C++ 对象引用未设置为对象的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18989016/

相关文章:

Java 通过对象引用比较两个属性

c++ - Boost Asio 与一次性对象共享相同的 io_service

c++ - 如何使模板参数的构造函数成为 friend ?

c++ - 如何告诉 Inline ASM 变量是十六进制的

具有奇怪范围解析运算符的 C++ typedef

java - 为什么我的 ArrayList 创建一个对象的两个副本?

c++ - 递归包含自身列表的对象

c++ - 这个语法是什么?

c++ - union 中的 std::shared_ptr

visual-c++ - Visual C++ 非托管和托管