发布此问题后,我尝试重现创建范围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) 基本上这是关于最烦恼的解析的这个问题的后续.我可以理解这是由于函数声明和变量定义之间的模糊性.
但在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)
但它编译得很好而且没有错误.我查看了标准文档但无法进行推理.
那么,我在这里错过了什么?
为什么这段代码可以删除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下没有输出任何内容.
我是一个非常有经验的.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?
考虑一下代码:
#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,为什么使用大括号工作?
在我下面的代码中,我想测试如果我有一个对象,会包含另一个构造函数抛出异常的对象会发生什么.但是下面的代码绝对没有.控制台上根本没有打印任何内容.
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) 我试图使用以下代码了解主要工作中的显式构造函数调用.
#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) 以下是语言新手的常见拼写错误,他们认为他们正在定义一个对象,但实际上是在声明一个函数:
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++的新手,这是我第一次使用它的类,我想知道,我如何调用构造函数?我已经阅读了一些关于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
我已经看了几个问这个问题的其他问题,但我的情况似乎比我经历过的情况简单得多,所以我会问这个问题.
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).他们是:
no default …c++ ×10
constructor ×4
arduino ×1
arduino-ide ×1
c++11 ×1
exception ×1
gcc ×1
syntax ×1
temporary ×1