标签: most-vexing-parse

尝试创建临时对象时出现奇怪的编译器错误

发布此问题后,我尝试重现创建范围RAII对象时意外右值创建的问题.现在看来我没有编译器错误就无法重现它!

在下面的代码示例中,在Test::foo()第二个ScopedLock创建中没有编译.gcc编译器错误似乎完全错误.谁能解释一下?

struct Mutex
{
    void lock() { }

    void unlock() { }
};


struct ScopedLock
{
    ScopedLock(Mutex & inMutex) : mMutex(inMutex)
    { mMutex.lock(); }

    ~ScopedLock()
    { mMutex.unlock(); }

private:
    ScopedLock(const ScopedLock&);
    ScopedLock& operator=(const ScopedLock&);

    Mutex mMutex;
};


struct Test
{
    void foo()
    {
        // Compiles fine
        ScopedLock lock(mMutex);

        // Error: no matching function for
        // call to ‘ScopedLock::ScopedLock()’
        ScopedLock(mMutex);
    }

    Mutex mMutex;
};
Run Code Online (Sandbox Code Playgroud)

我在Mac上使用GCC 4.2.1.

更新

我查看了原始代码,发现该成员是通过this指针引用的:

ScopedLock(this->mMutex); // short-lived temporary and compiles …
Run Code Online (Sandbox Code Playgroud)

c++ most-vexing-parse

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

为什么这不是一个令人烦恼的解析?

基本上这是关于最烦恼的解析的这个问题的后续.我可以理解这是由于函数声明和变量定义之间的模糊性.

但在Comeau在线,我只是厌倦了以下.

class T{

public:

    T(int i){
    }

    int fun1(){
        return 1;
    }

};

int main()
{
    T myT(10); // I thought it'd be a function declaration that takes an int and returns a type T
    myT.fun1(); // and a compiler error out here.
} 
Run Code Online (Sandbox Code Playgroud)

但它编译得很好而且没有错误.我查看了标准文档但无法进行推理.

那么,我在这里错过了什么?

c++ most-vexing-parse

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

为什么这段代码可以删除副本?

可能重复:
构造函数调用机制
为什么使用空的括号集来调用不带参数的构造函数是错误的?

为什么这段代码可以删除A的所有副本?

#include <iostream>

class A
{
public:
  A() {}
  A(const A&) { std::cout << "Copy" << std::endl; }
};

class B
{
public:
  B(const A& a_) : a(a_) {}
private:
  A a;
};

int main()
{
  B b(A());
}
Run Code Online (Sandbox Code Playgroud)

这段代码显然没有复制A,并且在ideone的gcc 3.4下没有输出任何内容.

c++ most-vexing-parse

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

无参数构造函数

我是一个非常有经验的.net开发人员,但是对Arduino和C / C ++还是陌生的,我正在尝试创建我的第一个库,该库是7段LED显示屏的简单驱动程序。我有很多钝的编译器错误,但本着一件事的精神,这是第一次。我想在类中添加一个无参数的构造函数,当我进行库编译时很好,但是当我尝试在草图中使用该类时,编译器为我提供了一个相当晦涩的“请求'sevenSegmentLed'中的成员'setDigit',非类类型'SevenSegmentLed()“

最简单的示例代码如下:

#ifndef SevenSegmentLed_h
#define SevenSegmentLed_h
#include "Arduino.h"

class SevenSegmentLed
{
  public:
    void setDigit(int digit);
    SevenSegmentLed();
};

#endif

#include "Arduino.h"
#include "SevenSegmentLed.h"

SevenSegmentLed::SevenSegmentLed()
{

}

void SevenSegmentLed::setDigit(int digit)
{       

}

#include "SevenSegmentLed.h"
SevenSegmentLed sevenSegmentLed();

void setup() {
  sevenSegmentLed.setDigit(4);
}

void loop() {
  // put your main code here, to run repeatedly:

}
Run Code Online (Sandbox Code Playgroud)

但是,如果我将构造函数签名更改为:SevenSegmentLed(int wtf);并实例化它:SevenSegmentLed sevenSegmentLed(1);编译就很好。如参数所示,WTF?

c++ arduino most-vexing-parse arduino-ide

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

为什么在if条件下烦恼解析?

考虑一下代码:

#include <iostream>

struct Foo
{
    Foo(int){}
    operator bool() const
    {
        return true;
    }
};

int main()
{
    if(Foo foo{42})
    {
        std::cout << "ok\n";
    }
}
Run Code Online (Sandbox Code Playgroud)

它在gcc5下编译得很好.但是,如果我更换线if(Foo foo{42})

if(Foo foo(42))
Run Code Online (Sandbox Code Playgroud)

我得到一个编译时错误:

错误:'foo'之前的预期primary-expression

这里发生了什么?没有令人烦恼的解析imo,为什么使用大括号工作?

c++ most-vexing-parse c++11

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

构造函数不抛出异常

在我下面的代码中,我想测试如果我有一个对象,会包含另一个构造函数抛出异常的对象会发生什么.但是下面的代码绝对没有.控制台上根本没有打印任何内容.

class A
{
    public:
        A()
        {
            cout << "in A constructor" << endl;
            throw "error creating A";
        }
        ~A()
        {
            cout << "destructing A" << endl;
        }
};

class C
{
    public:
    C()
    {
        cout <<"in C constructor" << endl;
    }
    ~C()
    {
        cout << "in C destructor " <<  endl;
    }

};

class B
{
    public:
    C c;
    A a;
    B(A a_, C c_): a(a_), c(c_){}
    B(){}
};


int main()
{
    try{
    B b(A, C);
    //B b;
    } …
Run Code Online (Sandbox Code Playgroud)

c++ constructor exception most-vexing-parse

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

显式使用main中的构造函数调用作为函数调用参数

我试图使用以下代码了解主要工作中的显式构造函数调用.

#include<iostream>

using namespace std;

class Dependency1
{
      bool init;
public:
  Dependency1() : init(true) {
    std::cout << "Dependency1 construction"
              << std::endl;
  }
  void print() const {
    std::cout << "Dependency1 init: "
              << init << std::endl;
  }



};


class Dependency2 {
   Dependency1 d1;
public:
   Dependency2(const Dependency1& dep1): d1(dep1){
     std::cout << "Dependency2 construction ";
     print();
   }
   void print() const { d1.print(); }
};

void test( const Dependency1& dd1)
{
    cout  << " inside Test \n";
    dd1.print();
}



int main()
{

    test(Dependency1());
    Dependency2 …
Run Code Online (Sandbox Code Playgroud)

c++ constructor temporary most-vexing-parse

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

使用意外声明为函数的对象后,解释GCC错误

以下是语言新手的常见拼写错误,他们认为他们正在定义一个对象,但实际上是在声明一个函数:

struct T
{
   void foo() {}
};

int main()
{
   T obj();
   obj.foo();
}
Run Code Online (Sandbox Code Playgroud)

GCC 4.1.2的错误是:

In function 'int main()':
Line 9: error: request for member 'foo' in 'obj', which is of non-class type 'T ()()'
compilation terminated due to -Wfatal-errors.
Run Code Online (Sandbox Code Playgroud)

为什么消息中报告的类型T ()()?我曾经期待过T ().

c++ gcc most-vexing-parse

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

没有调用C++构造函数

我是C++的新手,这是我第一次使用它的类,我想知道,我如何调用构造函数?我已经阅读了一些关于C++课程的文档,这就是我想出的东西.构造函数调用私有方法来设置服务器.

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sstream>
#include <string>
#include "SimpleIni.h"
#include "MySQL.cpp"
#include <thread>

class LoginServer {
    int resSocket;
    MySQL mysql;
    struct sockaddr_in strctAddr;

    private:
        void log(std::string strText, std::string strType = "INFO"){
            time_t rawtime;
            struct tm * timeinfo;
            char buffer[50];
            time(&rawtime);
            timeinfo = localtime(&rawtime);
            strftime(buffer, 50, "%c",timeinfo);
            std::cout << "[" << buffer << "][" << strType << "] > " << strText << std::endl;
        } …
Run Code Online (Sandbox Code Playgroud)

c++ syntax constructor variable-declaration most-vexing-parse

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

Visual C++:没有默认构造函数

我已经看了几个问这个问题的其他问题,但我的情况似乎比我经历过的情况简单得多,所以我会问这个问题.

Learn.h:

#ifndef LEARN_H
#define LEARN_H

class Learn
{
public:
    Learn(int x);
    ~Learn();

private:
    const int favourite;
};

#endif
Run Code Online (Sandbox Code Playgroud)

Learn.cpp:

#include "Learn.h"
#include <iostream>
using namespace std;

Learn::Learn(int x=0): favourite(x)
{
    cout << "Constructor" << endl;
}

Learn::~Learn()
{
    cout << "Destructor" << endl;
}
Run Code Online (Sandbox Code Playgroud)

Source.cpp:

#include <iostream>
#include "Learn.h"
using namespace std;

int main() {
    cout << "What's your favourite integer? "; 
    int x; cin >> x;
    Learn(0);

    system("PAUSE");
}
Run Code Online (Sandbox Code Playgroud)

上面的代码本身不会输出任何错误.

不过,我后,我得到更换一对夫妇的错误Learn(0)Learn(x).他们是:

  • 错误E0291: no default …

c++ constructor compiler-errors most-vexing-parse

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