标签: most-vexing-parse

使用临时对象调用构造函数

我不明白以下问题.

class InnerBox
{
public:
    InnerBox() : mContents(123) { };
private:
    int mContents;
};


class Box
{
public:

    Box(const InnerBox& innerBox) : mInnerBox(innerBox) { };

private:
    InnerBox mInnerBox;
};


void SomeFunction(const Box& box)
{
    return;
}

int main()
{
    Box box(InnerBox());  // !!PROBLEM!! Doesn't work: compiler thinks this is a function declaration
    SomeFunction(box);    // Error, cannot convert 'function pointer type' to const Box&

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

完整的错误消息是(Visual Studio 2010)

 error C2664: 'SomeFunction' : cannot convert parameter 1 from 'Box (__cdecl …
Run Code Online (Sandbox Code Playgroud)

c++ most-vexing-parse

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

最烦恼的解析:为什么不是`g((f()));`调用`f`的默认构造函数并将结果传递给带有`f`的`g`的ctor?

这不是最烦恼的解析:为什么不是A(()); 工作?,它基于一种形式的解析A a(());,其OP思想能够默认 - A使用额外的括号组构造一个对象.

相比之下,我的问题是关于2个类,f并且g,其中f有一个默认构造函数,而gctor需要一个f.我想g用临时f参数调用ctor ,所有这些都不使用统一的初始化语法.std::coutgctor中有一个语句,因此缺少输出表示函数声明而不是g对象实例化.我在注释中用3个数字注释了示例代码.#1和#2编译时#3注释掉了,反之亦然:

#include <iostream>
struct f {};
struct g {
    g(f) { std::cout << "g's ctor\n"; }
};
int main() {
                          // -----Output-----
    g( f() );             // #1: function declaration; expected
    g( ( f() ) );         // #2: also a function declaration; UNEXPECTED
    // g myG( ( f() ) ); …
Run Code Online (Sandbox Code Playgroud)

c++ constructor class most-vexing-parse function-declaration

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

C++拷贝构造函数失败

我看了一下Stackoverflow认为可能已经有答案的问题所提出的各种选项,但我看到的并没有结束.

示例代码:

#include <math.h>

class v2
{
public:
    float x;
    float y;

    v2(float angle) : x(cos(angle)), y(sin(angle))          {}
    v2(const v2 &v) : x(v.x), y(v.y)                        {}
};

int main(int argc, char **argv)
{
    float const angle(1.0f);
    v2 const test1(angle);
    v2 const test2(v2(angle));
    v2 const test3(test1);

    float const x1(test1.x);
    float const y1(test1.y);

    float const x2(test2.x);                // These two lines fail, claiming left of .x must have class type.
    float const y2(test2.y);

    float const x3(test3.x);
    float const y3(test3.y);

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

这是来自VS 2010的MSVC.test2的创建正确编译,但其成员的访问失败,声称test2没有类类型. …

c++ copy-constructor most-vexing-parse

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

与移动构造函数混淆:无法调用移动构造函数

我在使用C++理解移动构造函数时遇到了困难.我用默认构造函数,复制构造函数,移动构造函数和析构函数创建了一个简单的类.此外,我已经定义了一个具有两个重载的函数,一个接受对该类的引用,另一个接受对该类的右值引用.我的测试代码如下.

#include <iostream>


class c {

public:

    c() {
        std::cout << "default constructor" << std::endl;
    }

    c(const c& s) {
        std::cout << "copy constructor" << std::endl;
    }

    c(c&& s) {
        std::cout << "move constructor" << std::endl;
    }

    ~c() {
        std::cout << "destructor" << std::endl;
    }

};

void f(c& s) {
    std::cout << "passed by reference" << std::endl;
}

void f(c&& s) {
    std::cout << "passed by rvalue reference" << std::endl;
}

int main() {

    c s1; // line 1
    std::cout …
Run Code Online (Sandbox Code Playgroud)

c++ rvalue-reference most-vexing-parse move-constructor move-semantics

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

理解'最令人烦恼的解析' - 为什么允许含糊不清的语法?

在尝试理解C/C++中的"最令人烦恼的解析"问题时,这个问题立即浮现在脑海中 - 为什么会有一个语法导致这个问题开始?

例如,

class Timer
{
public:
    Timer();
};

class TimeKeeper
{
public:
    TimeKeeper(const Timer& t);

    int get_time()
    {
        return 1;
    }
};

int main()
{
    TimeKeeper time_keeper(Timer());
    // the above is eq to this: TimeKeeper time_keeper(Timer (*)());
}
Run Code Online (Sandbox Code Playgroud)

那么为什么不简单地禁止TimeKeeper time_keeper(Timer())成为一个函数声明,它接受一个未命名的函数ptr返回类型Timer?是TimeKeeper time_keeper(Timer (*)())劣等的函数声明者?

是不是由于这种语法,我们甚至得到这种模棱两可,或者我错过了什么?

编辑:就个人而言,我从来没有用过TimeKeeper time_keeper(Timer())函数声明.我总是用它Timer (*)()来指定一个函数指针,因为我发现它更清楚.

c++ most-vexing-parse

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

预计在左侧的结构.或.*但它是一种结构

我得到的编译错误structure required on left side of . or .*chest.contents[0],而且chest是一个结构:

class Item {
public:
    int id;
    int dmg;
};

class Chest {
public:
    Item contents[10];
};

int main() 
{
    Chest chest();

    Item item = chest.contents[0];

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

c++ compiler-errors most-vexing-parse

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

为什么这个语句不会调用构造函数--C++

模板类和普通类:

template <typename Type>
class Holder
{
public:
    Holder(const Type& value) : held_(value)
    {
        cout << "Holder(const Type& value)" << endl;
    }
    Type& Ref() { return held_; }
private:
    Type held_;
};

class Animal
{
public:
    Animal(const Animal& rhs) { cout << "Animal(const Animal& rhs)" << endl; }
    Animal() { cout << "Animal()" << endl; }
    ~Animal() { cout << "~Animal" << endl; }
    void Print() const { cout << "Animal::Print()" << endl; }
};
Run Code Online (Sandbox Code Playgroud)

然后我想Holder<Animal>用这个语句实例化一个Holder<Animal> a(Animal()); …

c++ templates most-vexing-parse

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

为什么匿名对象有时需要默认构造函数?

如果我编写以下程序,它可以正常工作:

struct Foo {
    Foo (std::string x) { std::cout << x << std::endl; }
};

int main () { Foo("hello, world"); }
Run Code Online (Sandbox Code Playgroud)

但是,如果我编写一个稍微不同的程序,我会收到编译错误:

struct Foo {
    Foo (std::string x) { std::cout << x << std::endl; }
};

std::string x("hello, world");

int main () { Foo(x); }
Run Code Online (Sandbox Code Playgroud)

错误是:

prog.cc: In function 'int main()':
prog.cc:10:20: error: no matching function for call to 'Foo::Foo()'

在IDEONE上可以看到完整的错误.

为什么第二个程序出错而不是第一个出错?

c++ most-vexing-parse

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

在这个例子中,C++ 最令人烦恼的解析是如何实现的?

我读到这段代码A a( A() );被编译器解释为函数声明,而在这里我清楚地看到这A()是一个返回对象的函数。建造一个A对象怎么可能是别的东西呢?

我刚刚完整阅读了 cppreference 的函数声明页面:https://en.cppreference.com/w/cpp/language/function,但我没有看到参数列表看起来像这样的任何地方A()

我不明白最令人烦恼的解析如何可以是有效的 C++。

c++ most-vexing-parse

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

如何初始化静态随机生成器数据成员?

我有Random类,但我不知道如何正确初始化其静态数据成员。

// random.h
#pragma once

#include <random>

class Random
{
private:
    static std::uniform_real_distribution<float> sDistribution_;
    static std::mt19937 sGenerator_;
};
Run Code Online (Sandbox Code Playgroud)
// random.cpp
#include "random.h"

std::mt19937 Random::sGenerator_(std::random_device()); // error here
std::uniform_real_distribution<float> Random::sDistribution_(0.0f, 1.0f);
Run Code Online (Sandbox Code Playgroud)

当我编译这个时,我收到一个错误:

声明与 不兼容std::mt19937

如何正确初始化该成员?

c++ initialization class static-members most-vexing-parse

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