相关疑难解决方法(0)

重载operator =中的内存泄漏

我有这个矩阵的构造函数来分配内存

class Matrix 
{

public:
    int** matrix;
    int cols;
    int rows;
};
Matrix::Matrix(int row, int col)
{
    cols = col;
    rows = row;
    matrix = new int*[rows];
    int i;
    for (i = 0; i < rows; ++i)
    {
        matrix[i] = new int[cols];
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我想重载operator =,但我无法弄清楚如何编写函数并分配新内存,而不会导致内存泄漏或内存不足.

我要做的矩阵=在它上面,已经为它分配了内存,那么我可以删除内存并使另一个内存大小的新内存吗?

现在我在operator =上有这个

this->rows = other.rows;
this->cols = other.cols;

int i, j;
for (i = 0; i < this->rows; ++i)
{
    for (j = 0; j < this->cols; j++)
    {
        this->matrix[i][j] = other.matrix[i][j];
    } …
Run Code Online (Sandbox Code Playgroud)

c++ memory memory-leaks memory-management

0
推荐指数
1
解决办法
319
查看次数

=运算符是否在C++中调用构造函数/ new?

假设我有一个(不可变的)矩阵类,它在构造函数中动态创建一个数组,并在解构器中删除它.

template <typename T>
class matrix {
private:
    T* data;
public:
    size_t const rows, cols;
    matrix(size_t rows, size_t cols) : rows(rows), cols(cols) {
        data = new T[rows*cols];
    }
    ~matrix() {
        delete [] data;
    }
    //access data
    T& operator()(size_t row, size_t col) {
        return data[row*cols + col];
    }
    matrix<T>& operator=(const matrix<T>& other) {
        //what will this->data contain? do I need to delete anything here?
        //should I call the constructor?
        rows = other.rows;
        cols = other.cols;
        data = new T[rows*cols];
        std::copy(&data[0],&data[0] + …
Run Code Online (Sandbox Code Playgroud)

c++ arrays oop assignment-operator assign

0
推荐指数
1
解决办法
90
查看次数

复制构造函数中的递归调用

我按照三个规则实施了一个类,我遇到了崩溃.在调试时,我得出结论,复制构造函数反复调用自身而不是调用相等运算符.为什么会这样?它不应该调用相等运算符吗?

#include <iostream>
#include <deque>
#include <cstdlib>
#define LENGTH 128

typedef struct tDataStruct
{

char strA[LENGTH];

char strB[LENGTH];
int nNumberOfSignals;
double* oQueue;

tDataStruct()
{
    nNumberOfSignals = 0;
    //oQueue = NULL;
    memset(strA, 0, LENGTH);
    memset(strB, 0, LENGTH);
}

~tDataStruct()
{
    if (NULL != oQueue)
    {
        delete[] oQueue;
        oQueue = NULL;
    }
}

tDataStruct(const tDataStruct& other) // copy constructor
{
    if (this != &other)
    {
        *this = other;
    }

}
tDataStruct& operator=(tDataStruct other) // copy assignment
{
    if (this == &other)
    { …
Run Code Online (Sandbox Code Playgroud)

c++ copy-constructor rule-of-three copy-assignment

0
推荐指数
1
解决办法
293
查看次数

为什么通过指定错误来连接两个链表?

所以我们的讲师只是向我们展示了如何连接两个链接列表然后继续并向我们展示了如果实现了将会出错的示例,我似乎并不完全理解为什么它不能正常工作,因为它不是解释.如果我以这种方式连接两个链表,会出现什么问题:

template <typename T>
void LList<T>::concat(LList<T> const & list) {
    end->next = list.start;
    end = list.end;
}
Run Code Online (Sandbox Code Playgroud)

c++ linked-list

0
推荐指数
1
解决办法
83
查看次数

这是五规则(或四规则和 1/2)的正确实施吗?

我正在研究五法则及其近亲(四法则和 1/2、复制和交换习语、朋友交换函数)。

我在测试课上实施了四和 1/2 规则。它编译得很好。我的实现中是否存在任何隐藏的错误?

我特别关注存储在m_unoreredd_map属性中的 unique_ptrs,我将其移动到复制构造函数中,因为它们无法复制。这是处理类中 unique_ptr 的正确方法吗?

某个类.h

#ifndef SOMECLASS_H
#define SOMECLASS_H

#include "someotherclass.h"

#include <QString>
#include <QStringList>

#include <memory>
#include <string>
#include <unordered_map>

class SomeClass
{
    QString m_qstring;
    std::unordered_map<std::string, std::unique_ptr<SomeOtherClass>> m_unordered_map;
    int m_int;
    std::string m_string;
    QStringList m_qstringlist;

public:
    SomeClass() = default;

    // Rule of 5 (or Rule of 4 and 1/2)
    // From https://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom#3279550
    ~SomeClass() = default;                               // Destructor
    SomeClass(SomeClass &other);                          // Copy constructor
    SomeClass(SomeClass &&other);                         // Move constructor
    SomeClass &operator=(SomeClass other);                // Copy/Move assignment …
Run Code Online (Sandbox Code Playgroud)

c++ qt unordered-map rule-of-five

0
推荐指数
1
解决办法
310
查看次数

是什么意思::〜

在下面的代码中,::〜的含义是什么

GaussianMex::~GaussianMex()
{
   int i;
}
Run Code Online (Sandbox Code Playgroud)

c++

-1
推荐指数
2
解决办法
3023
查看次数

段错误,如果从派生类更改指针值

不幸的是,我无法正确描述我的问题,但是:

当我尝试bar使用方法从子类“ B” 更改基类“ A”中定义的指针值时,foo()它给我一个段错误,因为指针栏为空:

#include <iostream>
class A {
    protected:
        virtual void foo() {};
        int* bar;
    public:
        void eval(){
            foo();
            std::cout << *bar;
        }

};

class B : public A {
    void foo() override {
        *bar = 10; //segfault
    }
};

int main(){
    B* foobar = new B();
    foobar->eval();
}
Run Code Online (Sandbox Code Playgroud)

但是,如果条形不是指针,而是普通变量,则可以正常工作。

c++ pointers

-1
推荐指数
1
解决办法
51
查看次数

重载+和=时出错

我想为我的矩阵类重载运算符+和=.我的代码是:

    friend float* operator+(const Matrix& m)
    {
        for(int i = 0; i < (int)(m._n*m._n); i++)
            _m[i] = _m[i] + m._m[i];
    };

    friend Matrix& operator=(const Matrix& m)
    {
        std::swap(_m, m._m);
        return *this;
    };
Run Code Online (Sandbox Code Playgroud)

_m数据和_n方阵的大小.但是我的编译器给了我以下错误:

main.cpp:161:45: error: ‘Matrix& operator=(const Matrix&)’ must be a nonstatic member function
main.cpp: In function ‘float* operator+(const Matrix&)’:
main.cpp:192:12: error: invalid use of non-static data member ‘Matrix::_m’
main.cpp:158:13: error: from this location
main.cpp:192:12: error: invalid use of non-static data member ‘Matrix::_m’
main.cpp:158:21: error: from …
Run Code Online (Sandbox Code Playgroud)

c++ overloading

-2
推荐指数
1
解决办法
272
查看次数

在单链接列表中重载赋值运算符

我正在学习链表.我创建了一个模板实现,包括构造函数,插入器,析构函数,复制构造函数和重载赋值运算符.问题是我的测试程序在重载赋值运算符后没有输出任何内容.

对于我的赋值运算符,我使用Clear()函数在复制之前完全清除列表.我把它放在析构函数中并检查它是否正常工作.我还检查了我的复制构造函数,它也运行良好.

文件node.h:定义节点构建块

#include <iostream>
using namespace std;

template <typename T>
struct Node{
    T _item;
    Node<T>* _next;

    Node() {
        _item = T();
        _next = NULL;
    }

    Node(T item){
        _item = item;
        _next = NULL;
    }

    // Print the value of a node
    friend std::ostream& operator <<(std::ostream& outs, const Node<T> &printMe){
        outs << "[" << printMe._item << "]";
        return outs;
    }
};
Run Code Online (Sandbox Code Playgroud)

文件list.h:定义链接列表模板

#include "node.h"

template <class T>
class List {
public:

    // …
Run Code Online (Sandbox Code Playgroud)

c++ linked-list operator-overloading assignment-operator

-2
推荐指数
1
解决办法
120
查看次数

复制构造函数未在c ++类上删除

我试图创建一个永远不会重复的类,因为它的字段中有很多数据.为此,我定义了这样的类:

class Foo {
public:
   Foo(int i);
   Foo();
   Foo(const Foo&) = delete;
}
Run Code Online (Sandbox Code Playgroud)

如果我尝试执行以下操作,这会导致编译器错误:

Foo a(2);
Foo b = a;
Run Code Online (Sandbox Code Playgroud)

但无论出于何种原因,这都不包括复制数据的情况.

Foo* array;
array = new Foo[10000];
Foo a(2);
array[1] = a;
Run Code Online (Sandbox Code Playgroud)

为什么是这样?

c++

-3
推荐指数
1
解决办法
61
查看次数