C2248编译错误的原因和解决VC6向VC7.1迁移真是累死人,N多编译错误

把以前VC6先写好的类加入VC7中,编译时竟然错误多到编译器无法接受而停止,hooooo.真的好烦,要是有个工具修改外加的.h和cpp文件就好了...

遇到最多的是C2248编译错误,才发现VC7提升了对基类成员访问的检测。

下面记录一些MSDN里提到的

VC++编译器重大修改

This topic summarizes the compile-time errors and warnings that will now be issued on code that compiled without errors or warnings in Visual Studio .NET or earlier versions.

  • Closing parentheses now required for the defined preprocessor directive (C2004).
  • Explicit specializations no longer find template parameters from primary template (C2146).
  • A protected member (n) can only be accessed through a member function of a class (B) that inherits from the class (A) of which it (n) is a member (C2247).
  • Improved accessibility checks in compiler now detect inaccessible base classes (C2248).
  • An exception cannot be caught if the destructor and/or copy constructor is inaccessible (C2316).
  • Default arguments on pointers to functions no longer allowed (C2383).
  • A static data member cannot be initialized via derived class (C2477).
  • The initialization of a typedef is not allowed by the standard and now generates a compiler error (C2513).
  • bool is now a proper type (C2632).
  • A UDC can now create ambiguity with overloaded operators (C2666).
  • More expressions are now considered valid null pointer constants (C2668).
  • template<> is now required in places where the compiler would previously imply it (C2768).
  • The expilicit specialization of a member function ourside the class is not valid if the function has already been explicitly specialized via a template class specialization. (C2910).
  • Floating point non-type template parameters are no longer allowed (C2993).
  • Class templates are not allowed as template type arguments (C3206).
  • Friend function names are no longer introduced into containing namespace (C3767).
  • The compiler will no longer accept extra commas in a macro (C4002).
  • An object of POD type constructed with an initializer of the form () will be default-initialized (C4345).
  • typename is now required if a dependent name is to be treated as a type (C4346).
  • Functions that were incorrectly considered template specializations are no longer considered so (C4347).
  • Static data members cannot be initialized via derived class (C4356).
  • A class template specialization needs to be defined before it was used in a return type (C4686).
  • The compiler now reports unreachable code (C4702).

 最烦人的在VC7里是无法访问 private typedef!(C2248)但6.0却是可以,累了。。。
Improved accessibility checks in compiler now detect inaccessible base classes (C2248).

C2248的产生原因之一:从“类”创建的对象无法访问该“类”的 protected private 成员。

为使代码在 Visual C++ 的 Visual Studio .NET 2003 和 Visual Studio .NET 版本中都有效,使用范围运算符

// C2248c.cpp
// compile with: /LD
struct A
{
};

struct B: private A
{
};

struct C: B
{
   void f()
   {
      A *p1 = (A*) this;   // C2248
      ::A *p2 = (::A*) this;   // OK
   }
};







如果调试器表达式引用不明确的成员名称,必须使用类名称来限定它。
例如,如果 CObjectCClass 实例,后者从 AClassBClass
二者中继承了名为 expense 的成员函数,则 CObject.expense 是不明确的。
可以按如下方式化解多义性:

CObject.BClass::expense
原文地址:https://www.cnblogs.com/babyblue/p/164210.html