相关疑难解决方法(0)

为什么push_back在包含unique_ptr的结构上成功,除非该结构具有自定义析构函数?

当且仅当我删除Foo的自定义析构函数时,以下代码才会编译.

struct Foo {
    std::unique_ptr <int> bar;
    ~Foo (void) {} // This Line
};
std::vector <Foo> foos;
foos.push_back (Foo ());
Run Code Online (Sandbox Code Playgroud)

以下是我认为我对这种情况的理解:

它失败,因为unique_ptrs无法复制,并std::vector::push_back (thing)调用thing's复制构造函数.如果我编写Foo一个明确移动的自定义复制构造函数bar,那么一切都会好的.

但是,禁用This Line将导致代码编译.

我想,这应该无法编译,即使没有This Line,因为我仍然在尝试push_back一个unique_ptr.

为什么没有自定义析构函数就能成功,为什么添加自定义析构函数会导致它失败?

编辑:gcc -std=gnu++11在Debian Linux 64位上使用

c++ stl c++11

4
推荐指数
2
解决办法
817
查看次数

C++删除向量中的元素

int main(){     

vector<Customer*> newcustomer;

newcustomer.push_back(new Customer("III", 123333, 555));
newcustomer.push_back(new Customer("LOL", 122222, 444));
newcustomer.push_back(new Customer("PPL", 121111, 333));

for (int i = 0; i < 3; i++){
    cout << newcustomer[i]->getName() << endl;
    cout << newcustomer[i]->getPhone() << endl;
    cout << newcustomer[i]->getID() << endl;
    cout << endl;
}






system("pause");
return 0;

}
Run Code Online (Sandbox Code Playgroud)

所以我创建了一个名为customer的类,你可以插入新客户,getName返回名称,getPhone返回电话号码,GetID返回ID,现在我想从该向量中删除所有内容,不知道该怎么做.

c++ vector

4
推荐指数
2
解决办法
186
查看次数

正确使用析构函数

我刚开始使用C++,现在我有一个非常基本的问题.

我写了两节课:

坐标:

#include <stdio.h>

class Coordinate {
private:
    int x;
    int y;
public:
    Coordinate(int a, int b) {
        x = a;
        y = b;
    };
    void printTest() {
        printf("%d %d\n", x, y);
    };
};
Run Code Online (Sandbox Code Playgroud)

测试:

class Test {
private:
    int q;
    Coordinate *point;
public:
    Test(int a, int b, int c) {
        q = a;
        point = new Coordinate(b, c);
    };
    virtual ~Test() {
        delete point;
    }
};
Run Code Online (Sandbox Code Playgroud)

主功能:

int main() {
    Test *test = new Test(1, 2, 3);
    // …
Run Code Online (Sandbox Code Playgroud)

c++ dynamic-memory-allocation

4
推荐指数
2
解决办法
1684
查看次数

C++法则?(类似于三巨头法)

我一直在阅读C++并在其中编写小程序超过一年.最近我遇到了三巨头的法律.我从来不知道这个法律.

无意中,我在这里找到了:三个规则.

我可以在C++中了解其他任何此类法律吗?

c++

3
推荐指数
3
解决办法
1316
查看次数

与分配运营商的右值参考

在本文中http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/comment-page-1/#comment-1877:

T& T::operator=(T const& x) // x is a reference to the source
{ 
    T tmp(x);          // copy construction of tmp does the hard work
    swap(*this, tmp);  // trade our resources for tmp's
    return *this;      // our (old) resources get destroyed with tmp 
}
Run Code Online (Sandbox Code Playgroud)

但鉴于副本的缺失,这种表述显然效率低下!现在"显而易见"的是,编写复制和交换分配的正确方法是:

T& operator=(T x)    // x is a copy of the source; hard work already done
{
    swap(*this, x);  // trade our resources for x's
    return *this;    // our (old) resources get destroyed with …
Run Code Online (Sandbox Code Playgroud)

c++ rvalue-reference c++11

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

为什么我不能使用"delete"关键字回收动态分配的内存?

我有以下课程:

class Patient {
public:
    Patient(int x);
    ~Patient();
private:
    int* RP;
};

Patient::Patient(int x) { RP = new int [x]; }
Patient::~Patient() { delete [] RP; }
Run Code Online (Sandbox Code Playgroud)

我在堆栈上创建了这个类的实例,如下所示:

void f() { Patient p(10); }
Run Code Online (Sandbox Code Playgroud)

现在,当f()返回时,我得到一个"双重免费或损坏"错误,它向我发出信号,表示尝试删除的内容不止一次.但我不明白为什么会这样.数组的空间是在堆上创建的,只是因为分配了空间的函数返回,我不希望回收空间.

我认为如果我在堆上分配空间(使用new关键字),那么回收该空间的唯一方法是使用delete关键字.救命!

根据要求,这是实际的代码(为简洁起见略有删节)

这是完整的类定义(拆分.cpp.h文件,但一起显示):

class Patient {

public:
    Patient(int numDecisionEpochs);

    ~Patient();

    void recordRP(const int& age, const bool& t);
    void recordBiopsy(const int& age, const int& result);
    void recordPSA(const int& age, const double& level);
    void recordPSA(const int& age);
private:
    int* …
Run Code Online (Sandbox Code Playgroud)

c++ memory-management new-operator

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

复制省略并移动语义不按预期工作

我做了一个程序来评估这样做之间的性能差异:

func3(func2(func1()));
Run Code Online (Sandbox Code Playgroud)

对此:

retval1 = func1();
retval2 = func2(retval1);
func3(retval2);
Run Code Online (Sandbox Code Playgroud)

我更喜欢后者的可读性和易于调试,我想知道编译器(MSVC 12.0)是否会优化发布版本中的中间对象.我的测试程序是这样的:

#include <iostream>

using namespace std;

struct Indicator {
    Indicator() {cout << "Default constructor" << endl;}
    Indicator(const Indicator& other) {cout << "Copy constructor" << endl;}
    const Indicator& operator=(const Indicator& other) {cout << "Assignment operator" << endl;}
    ~Indicator() {cout << "Destructor" << endl;}
};

Indicator func1()
{return Indicator();}

Indicator func2(Indicator&& i)
{return std::move(i);}

Indicator func3(Indicator&& i)
{return std::move(i);}

int main() {
    Indicator i = func3(func2(func1()));
    cout << &i << endl; …
Run Code Online (Sandbox Code Playgroud)

c++ optimization

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

移动赋值运算符上的异常说明符如何影响移动构造函数?

我正在使用GCC 5.2和clang 3.6进行测试,两者都在C++ 14模式下,并且它们提供相同的输出.

对于以下代码

#include <iostream>
#include <type_traits>

struct S {
  // S& operator= (S&&) noexcept { return *this; }
};


int main() {
  std::cout << std::is_nothrow_move_constructible<S>::value
            << std::is_nothrow_move_assignable<S>::value;  
}
Run Code Online (Sandbox Code Playgroud)

11得到的结果.但是如果取消注释移动赋值运算符,则输出变为01.如何noexcept对移动赋值运算符的显式规范可能会影响移动构造函数的规范?

c++ exception language-lawyer noexcept c++14

3
推荐指数
2
解决办法
264
查看次数

Visual Studio 2015.c++ 编译器错误 C2280 尝试引用已删除的函数

我想做的是编译由 CMake 构建的项目。在我的代码中我有下一个方法:

/** "in-place" version of TriangularView::solve() where the result is written in \a other
  *
  * \warning The parameter is only marked 'const' to make the C++ compiler accept a temporary expression here.
  * This function will const_cast it, so constness isn't honored here.
  *
  * See TriangularView:solve() for the details.
  */
template<typename MatrixType, unsigned int Mode>
template<int Side, typename OtherDerived>
void TriangularView<MatrixType,Mode>::solveInPlace(const MatrixBase<OtherDerived>& _other) const
{
    OtherDerived& other = _other.const_cast_derived();
    eigen_assert( cols() == rows() && ((Side==OnTheLeft && …
Run Code Online (Sandbox Code Playgroud)

c++ eigen visual-studio-2015 slam

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

std :: thread,类构造函数和析构函数

在C++ 11中测试线程时,我创建了以下示例:

#include <iostream>
#include <thread>

class Foo {
public:
    Foo(void) {
        std::cout << "Constructor called: " << this << std::endl;
    }
    ~Foo(void) {
        std::cout << "Destructor called: " << this << std::endl;
    }
    void operator()() const {
        std::cout << "Operatior called: " << this << std::endl;
    }
};

void test_normal(void) {
    std::cout << "====> Standard example:" << std::endl;
    Foo f;
}

void test_thread(void) {
    std::cout << "====> Thread example:" << std::endl;
    Foo f;
    std::thread t(f);
    t.detach();
}


int main(int …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading c++11 stdthread

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