Reputation: 165
I just encountered the following problem:
#include "stdafx.h"
#include <string>
#include <iostream>
class transaction{
protected:
transaction(const std::string& log) { printLog(log); }
private:
void printLog(const std::string& log) const { std::cout << log << "\n"; }
};
class inTrans : public transaction {
public:
inTrans() : transaction( std::string("input") ) { }
};
class outTrans : public transaction {
public:
outTrans() : transaction{ std::string("output") } { } //This doesn't work
};
Visual Studio 2013 marks the first "{"-red and shows the following error:
"Error protected function "transaction::transaction(const std::string &log)" (declared at line 7) is not accessible through a "transaction" pointer or object."
The thing is that I'm still able to compile the file and everything seems to be running just fine. So why do I get this strange error?
Upvotes: 4
Views: 670
Reputation: 158529
If we look at the draft C++ standard the grammar in section 12.6.2
Initializing bases and members indicates what you have is valid syntax so the error is a bug:
ctor-initializer:
: mem-initializer-list
mem-initializer-list:
mem-initializer ...opt
mem-initializer , mem-initializer-list ...opt
mem-initializer:
mem-initializer-id ( expression-listopt)
mem-initializer-id braced-init-list <-- this applies to this case
The code also compiles fine with both gcc
and clang
Upvotes: 4