相关疑难解决方法(0)

对int指针的引用会导致错误

我有以下代码片段,它是一个数组大小调整功能的实现.它似乎是正确的,但是当我编译程序时,我得到以下错误:

g++ -Wall -o "resizing_arrays" "resizing_arrays.cpp" (in directory: /home/aristofanis/Desktop/coursera-impl)
resizing_arrays.cpp: In function ‘int main()’:
resizing_arrays.cpp:37: error: invalid initialization of non-const reference of type ‘int*&’ from a temporary of type ‘int*’
resizing_arrays.cpp:7: error: in passing argument 1 of ‘void resize(int*&, int, int, int)’
resizing_arrays.cpp:39: error: invalid initialization of non-const reference of type ‘int*&’ from a temporary of type ‘int*’
resizing_arrays.cpp:7: error: in passing argument 1 of ‘void resize(int*&, int, int, int)’
resizing_arrays.cpp:41: error: invalid initialization of non-const reference of type ‘int*&’ …
Run Code Online (Sandbox Code Playgroud)

c++ int pointers reference

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

为什么引用无法捕获临时而const ref和rval ref可以

为什么引用无法捕获临时值,而const引用和右值引用可以捕获并延长对象的生命周期.换句话说,虽然两个第一行是合法的但第三行不是:

const string &a = string("a");
string &&b = string("b");
string &c = string("c"); // why illegal?
Run Code Online (Sandbox Code Playgroud)

c++ reference rvalue-reference const-reference

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

无法从'initializer-list'转换为'std :: initializer_list <Keyword>&'

我正在尝试使用初始化列表将关键字列表传递给tokenizer进行注册.但它在Visual Studio 2013中不起作用.它适用于ideone.com上的gcc.有没有办法在VS中使用这个或类似的语法?

struct Keyword
{
    const char* name;
    int id;
};

void AddKeywords(int group, std::initializer_list<Keyword>& lis) {}

// usage
AddKeywords(ITEM_TYPE, {
    { "weapon", IT_WEAPON },
    { "armor", IT_ARMOR }
});
Run Code Online (Sandbox Code Playgroud)

完整错误:

item.cpp(810): error C2664: 'void AddKeywords(int,std::initializer_list<Keyword> &)' : cannot convert argument 2 from 'initializer-list' to 'std::initializer_list<Keyword> &'
Run Code Online (Sandbox Code Playgroud)

c++ initializer-list c++11 visual-studio-2013

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

为什么复制构造函数需要是const?

我有一个关于复制构造函数的问题,假设一个class A包含一个int默认构造函数的复制构造函数(?)和一个接收一个构造函数的构造函数.int

class A {
  int value;
public:
  A(): value(0) {}                  // Default
  A(A& copy): value(copy.value) {}  // Copy (?)
  A(const int n): value(n) {}       // Receives an int for 'value'

  int get_value() { return value; } // Return value
};
Run Code Online (Sandbox Code Playgroud)

并且包含一个A pointer名为的类BoxForA:

class BoxForA {
  A *obj;
public:
  BoxForA(): obj(nullptr) {}
  BoxForA(const int a) { obj = new A(a); }

  A popf(){ return *obj; }  // Returns the content of …
Run Code Online (Sandbox Code Playgroud)

c++ constructor c++11

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

为什么运算符&lt;&lt;对于test2 + test3不能超载?

我尝试重载operator<<,但有警告我无法重载。我可以按如下方式重载该运算符:

std::cout << test4 << std::endl;
Run Code Online (Sandbox Code Playgroud)

但是我不能按如下方式重载它:

std::cout << test2 + test3 << std::endl;
Run Code Online (Sandbox Code Playgroud)

我的主要代码是:

Stonewt test2(2, 8, Stonewt::STONE);
std::cout << "test2: " << test2 << std::endl;

Stonewt test3(2, 3, Stonewt::POUNDS);

std::cout << "test3: " << test3 << std::endl;

Stonewt test4 = test2 + test3;
std::cout << test4 << std::endl;         // operator << can overload
std::cout << test2 + test3 << std::endl; // operator << cannot overload
Run Code Online (Sandbox Code Playgroud)

下面是friend功能

std::ostream& operator <<(std::ostream& os, Stonewt& a)
{
    if …
Run Code Online (Sandbox Code Playgroud)

c++ class operator-overloading friend-function c++11

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

为什么在删除了“移动构造函数”时使用了“复制构造函数”?

作为主题,相关代码为:

#include <iostream>     

class ABC     
{  public:  
    ABC() 
    {
        std::cout<< "default construction" << std::endl;
    }

    ABC(const ABC& a) 
    {
        std::cout << "copy construction" << std::endl;
    } 

    ABC(const ABC&& a) 
    {
        std::cout << "move construction" << std::endl;
    }
};                         

int main()   
{  
   ABC c1 = ABC();  

   return 0;  
}
Run Code Online (Sandbox Code Playgroud)

使用 -fno-elide-constructors -std=c++11 输出

default construction
move construction
Run Code Online (Sandbox Code Playgroud)

如果我删除上面的移动构造函数,则输出为:

default construction
copy construction
Run Code Online (Sandbox Code Playgroud)

为什么copy construction可以用whilemove constructor已经去掉了?你看,如果有用户定义的move constructor,编译器更喜欢使用move constructor

根据一些文档,编译器提供了一个默认值move constructor。**那么为什么编译器不使用默认值move …

c++ constructor copy-constructor move-semantics c++11

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

为什么添加"const"解决"无效的操作数到二进制表达式"

我已经超载了运营商+ Integer operator+(Integer & a, Integer & b).但是当我执行a = b + c + d时,它会将无效操作数的错误提供给二进制表达式.但是通过添加const参数,不会再出现错误.为什么会这样?

c++ overloading constants

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

重载左移和右移运算符(cin 和 cout)

我已经在这里做了一个 Point 类。我写的时候一切正常

cout << p1 << endl; //which p is a Point
Run Code Online (Sandbox Code Playgroud)

但是当我有两个 Point 和 write 对象时

cout << (p1 + p2) << endl; //or (p1 - p2) and etc...
Run Code Online (Sandbox Code Playgroud)

我得到错误。您可以在此处查看错误。我不知道原因。请帮忙。

c++

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

无法使函数接受rvalue和lvalue引用

class Vec3{

private:
    float x, y, z;

public:
    Vec3() = default;
    Vec3(const float c) {x = c; y = c; z = c;}

    static  Vec3& normalize(Vec3& v) {/* normalize */ return v;}

};

Vec3 aaa = Vec3( 1.0f);
Vec3 bbb = Vec3::normalize( Vec3( 1.0f));
Vec3 ccc = Vec3::normalize( aaa);
Run Code Online (Sandbox Code Playgroud)

我想编写将向量作为参数的函数,对它们进行一些处理并将它们作为引用返回.

在上面的代码中bbb不会编译,因为它是对rvalue的非const引用.我不能使它成为const因为normalize需要修改对象.如果我使函数接受rvalue references(Vec3&& v)然后ccc不会编译因为aaa是左值.我可以在不写两个版本的情况下完成这项工作normalize吗?

(我对rvalue vs左值引用感到困惑,我不明白为什么a someFunc(const Vec3& v)会接受rvalues和lvalues,而非const版本不会.)

c++ code-duplication rvalue lvalue

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

func() = var_name; 是什么意思?c++ 中的赋值操作?

我正在研究左值和右值,但我对此感到困惑:

#include<iostream>
int& get_val(){
    return 10;
}
int main(){
    get_val() = 5;
    int a = get_val();
    std::cout<<a<<std::endl;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我知道int a = get_val();(即它将返回的值分配给变量),但我想知道这是做什么的:get_val() = 5;。有什么int& get_val()作用?

我知道我们无法运行这段代码,但我想了解其背后的概念。

c++ function lvalue

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

将右值绑定到左值引用

我有以下C ++代码(VS2013):

#include <iostream>
using namespace std;

class A {
    int i;
public:
    A(int i) : i(i) {
        cout << "Constructor: " << i << endl;
    }
    A(const A &o) : i(o.i) {
        cout << "Copy constructor: " << i << endl;
    }
    ~A() {
        cout << "Destructor: " << i << endl;
    }
};

A test(const A &a, A b, A *c) {
    return *c;
}

int main() {
    A b(10);
    cout << "START OF TEST" << endl;
    test(1, …
Run Code Online (Sandbox Code Playgroud)

c++ constructor type-conversion copy-constructor

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

指针向量与值向量之间的差异

我听说在C ++中不建议使用指针,但我不明白为什么。

我的问题是我想创建一个类对象向量来管理我的类。

vector<MyClass> vectorOfClass;
Run Code Online (Sandbox Code Playgroud)

自然地,为了获得更好的性能,我应该使用类对象指针的向量吗?

vector<MyClass *> vectorOfClass;
Run Code Online (Sandbox Code Playgroud)

也许可以创建类对象的引用向量?

vector<MyClass &> vectorOfClass;
Run Code Online (Sandbox Code Playgroud)

所以我的问题是:

  • 这些方式有什么区别?
  • 创建类对象向量的最优化方法是什么?

c++ memory pointers class vector

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