Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何使<&书信电报;操作员是否打印类成员?_C++_Class_Operator Overloading_Runtime Error_Graph Theory - Fatal编程技术网

C++ 如何使<&书信电报;操作员是否打印类成员?

C++ 如何使<&书信电报;操作员是否打印类成员?,c++,class,operator-overloading,runtime-error,graph-theory,C++,Class,Operator Overloading,Runtime Error,Graph Theory,这是上课时间 class graph { public: graph() {}; // constructor graph(int size); friend ostream& operator<< (ostream& out, graph g); private: int size; bool** grph; }; 类图{ 公众: graph(){};/

这是上课时间

    class graph {
    public:
        graph() {}; // constructor
        graph(int size);
        friend ostream& operator<< (ostream& out, graph g);
    private:
        int size;
        bool** grph;
    };
类图{
公众:
graph(){};//构造函数
图形(整数大小);

friend ostream&operator从不输入循环,因为

i < g.size
另外,您没有指定复制构造函数,因此将使用隐式构造函数。但是隐式构造函数只是复制值,因此指针
grph
将指向相同数据的两个对象。您正在泄漏内存,这就是为什么(技术上)不重要的原因,但您应该实现适当的析构函数和复制/移动构造函数


但是因为
操作符问题取决于类属性
大小的初始化失败
在costructor方法中

添加到ctor
this.size=size
应该可以解决您的问题,正如Rakete1111正确指出的那样


以下方法允许您打印false或true,否则除了其他人所说的之外,您还可以尝试
cout(您没有初始化
size
成员),你的
运算符如果为true,这只是问题的一部分。看运算符的签名,然后看OP为类提供的c'tor。@NathanOliver,我看不到。它们是一样的?OP正在制作副本。他们有副本c'tor吗?@Galik解决了大小问题,但没有解决打印问题,打印是专业的问题。现在他们将打印浅拷贝,这将(希望如此)导致访问冲突。@Galik oops。我看错了。回答这个问题没问题。谢谢你直截了当地回答。虽然这个代码片段可能会解决这个问题,但确实有助于提高你文章的质量。记住,你是在将来为读者回答这个问题,那些人可能会问I don’我不知道你的代码建议的原因。我更好地了解了你的构造函数,实际上原因是“size”属性的初始化失败……这个类还需要重载一些函数以适应复制。这篇文章应该很有用:
    ostream& operator<< (ostream& out, graph g) {
        for (int i = 0; i < g.size; ++i) {
            for (int j = 0; j < g.size; ++j) 
                out << g.grph[i][j] << "\t";
            out << endl;
        }
        return out;
    }
     graph g(5);
 cout << g << endl;
i < g.size
this->size = size; //Sets member variable 'size' to 'size' from the function arguments
ostream& operator<< (ostream& out, graph g) {
        for (int i = 0; i < g.size; ++i) {
            for (int j = 0; j < g.size; ++j) 
                out << (g.grph[i][j] ? "true" : "false") << "\t";
            out << endl;
        }
        return out;
    }
ostream& operator<< (ostream& out, const graph &g)
class graph {
public:
    graph(int size = 0);
    graph(const graph &src);
    ~graph();

    graph& operator=(const graph &rhs);

    friend std::ostream& operator<< (std::ostream& out, const graph &g);
private:
    int size;
    bool** grph;
};

std::ostream& operator<< (std::ostream& out, const graph &g);
graph::graph(int size)
    : grph(NULL), size(size)
{
    grph = new bool*[size];
    for (int i = 0; i < size; ++i) {
        grph[i] = new bool[size];
    }
    for (int i = 0; i < size; ++i) {
        for (int j = i; j < size; ++j) {
            if (i == j) {
                grph[i][j] = false;
            } else {
                grph[i][j] = grph[j][i] = (prob() < 0.19);
            }
        }
    }
}

graph::graph(const graph &src)
    : grph(NULL), size(src.size)
{
    grph = new bool*[size];
    for (int i = 0; i < size; ++i) {
        grph[i] = new bool[size];
        for (int j = i; j < size; ++j) {
            grph[i][j] = src.grph[i][j];
    }
}

graph::~graph() {
    for (int i = 0; i < size; ++i) {
        delete[] grph[i];
    }
    delete[] grph;
}

graph& graph::operator=(const graph &rhs)
{
    if (this != &rhs)
    {
        graph tmp(rhs);
        std::swap(grph, tmp.grph);
        std::swap(size, tmp.size);
    }
    return *this;
}

std::ostream& operator<< (std::ostream& out, const graph &g) {
    for (int i = 0; i < g.size; ++i) {
        for (int j = 0; j < g.size; ++j) {
            out << g.grph[i][j] << "\t";
        }
        out << endl;
    }
    return out;
}
class graph {
public:
    graph(int size = 0);
    friend ostream& operator<< (ostream& out, const graph &g);
private:
    std::vector<std::vector<bool> > grph;
};

std::ostream& operator<< (std::ostream& out, const graph &g);
graph::graph(int size)
{
    grph.resize(size);
    for (int i = 0; i < size; ++i) {
        grph[i].resize(size);
    }
    for (int i = 0; i < size; ++i) {
        for (int j = i; j < size; ++j) {
            if (i == j) {
                grph[i][j] = false;
            } else {
                grph[i][j] = grph[j][i] = (prob() < 0.19);
            }
        }
    }
}

ostream& operator<< (ostream& out, const graph &g) {
    for (int i = 0; i < g.grph.size(); ++i) {
        std:::vector<bool> &row = g.grph[i];
        for (int j = 0; j < row.size(); ++j) {
            out << row[j] << "\t";
        }
        out << endl;
    }
    return out;
}