在类

时间:2017-04-27 15:07:28

标签: c++ c++11 explicit explicit-conversion

我在类

之外定义operator bool()函数时遇到问题
class A{

public:
    explicit operator bool() const; 
};

我将类外的函数定义为......

explicit A::operator bool() const {
    ...
}

我收到此错误 - error: ‘explicit’ outside class declaration

做错了什么?

1 个答案:

答案 0 :(得分:6)

如果您已经为声明编写了inline,那么就不应该为该定义编写/*explicit*/ A::operator bool() const { // ... } ,不允许在类定义之外编写explicit

  

它可能只出现在其类定义中此类函数声明的 decl-specifier-seq 中。

所以,只需删除它:

// This script list all the collections from the database
// and execute a mongoimport command corresponding to the collection name

// npm install --save shelljs
// npm install --save mongojs

var shell = require('shelljs')
var mongojs = require('mongojs')
var db = mongojs('username:password@localhost:27017/sampleDB')
db.getCollectionNames(function (err, names) {
  if (err) {
    console.log(err)
    process.exit(1)
  }
  names.forEach((name) => {
    var cmd = 'mongoimport --db sampleDB --collection ' + name + ' --type json --file D:\\' + name + '.json'
    console.log('executing: ' + cmd)
    shell.exec(cmd, function (code, stdout, stderr) {
      if (code != 0) {
        console.error('Error: ' + code)
        console.log('Program output:', stdout)
        console.log('Program stderr:', stderr)
      }
    })
  })
  process.exit(0)
})
相关问题