4

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?

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
Maximal
  • 165
  • 6
  • Is it intentional that you use () for inTrans and {} for outTrans initialization list? Maybe C++11 supports some {} fanciness for initializion list, I don't know. – Neil Kirk Oct 01 '14 at 14:10
  • 1
    Your code compiles fine with GCC. Maybe a bug / missing feature in Visual Studio? – 5gon12eder Oct 01 '14 at 14:11
  • Works fine with gcc and clang. Sounds like a bug. – T.C. Oct 01 '14 at 14:13
  • "marks the first "{"-red" - are you getting an actual compiler error, or just seeing intellisense markup in the Studio IDE editor? You can't necessarily trust intellisense, and if you can still compiler successfully - best to just forget about it. – Tony Delroy Oct 01 '14 at 14:23
  • It's just intellisense. I can compile it though, so i think I'll just forget about it. Thanks you all :) – Maximal Oct 01 '14 at 14:25
  • 2
    You should submit a [bug report](https://connect.microsoft.com/visualstudio) although it seems minor, it led to you asking an SO question b/c you thought you had a problem and clearly creates confusion. If you could update your question with a link to the bug report that would be awesome, so if users come upon your question they can see the status. – Shafik Yaghmour Oct 01 '14 at 14:27
  • @Maximal: So basically your question is about an IntelliSense warning, since the compiler proper has no issues with the code. IntelliSense is IntelliSense. It is not intended to be a full-blown compiler. Everything it says is intended to be a suggestion. And it will get confused from time to time. There's not much point in asking why IntelliSense does something or don't do something. Maybe they will update it in the next release. – AnT stands with Russia Oct 01 '14 at 14:43

1 Answers1

4

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

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740