c++ - 为什么这个程序不能在VC++2008Express中编译?

标签 c++ nested-class

此程序在 TC++ 3.0 上编译和运行。

为什么这个程序不能用VC++2008Express编译?

#include <iostream>

using namespace std;

class MyClass
{
private:
    class MyNestedClass
    {
    private:
        int myInteger;

    public:
        MyNestedClass(int a)
        {
            myInteger = a;
        }
        void Show()
        {
            cout<<myInteger;
        }
    };
};

int main()
{
    MyClass::MyNestedClass myNestedClassObject(100);

    myNestedClassObject.Show();
}

显示以下消息:

1>------ Rebuild All started: Project: Test, Configuration: Debug Win32 ------
1>Deleting intermediate and output files for project 'Test', configuration 'Debug|Win32'
1>Compiling...
1>Static_Data_Member.c
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(39) : error C2143: syntax error : missing '{' before ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(39) : error C2059: syntax error : ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(41) : error C2143: syntax error : missing '{' before ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(41) : error C2059: syntax error : ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(41) : error C2143: syntax error : missing '{' before ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(41) : error C2059: syntax error : ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(41) : error C2143: syntax error : missing '{' before ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(41) : error C2059: syntax error : ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(42) : error C2143: syntax error : missing '{' before ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(42) : error C2059: syntax error : ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(42) : error C2143: syntax error : missing '{' before ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(42) : error C2059: syntax error : ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(42) : error C2143: syntax error : missing '{' before ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(42) : error C2059: syntax error : ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(43) : error C2143: syntax error : missing '{' before ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(43) : error C2059: syntax error : ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(43) : error C2143: syntax error : missing '{' before ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(43) : error C2059: syntax error : ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(43) : error C2143: syntax error : missing '{' before ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(43) : error C2059: syntax error : ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(44) : error C2143: syntax error : missing '{' before ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(44) : error C2059: syntax error : ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(44) : error C2143: syntax error : missing '{' before ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(44) : error C2059: syntax error : ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(44) : error C2143: syntax error : missing '{' before ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(44) : error C2059: syntax error : ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(45) : error C2143: syntax error : missing '{' before ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(45) : error C2059: syntax error : ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(45) : error C2143: syntax error : missing '{' before ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(45) : error C2059: syntax error : ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(45) : error C2143: syntax error : missing '{' before ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(45) : error C2059: syntax error : ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(46) : error C2143: syntax error : missing '{' before ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(46) : error C2059: syntax error : ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(46) : error C2143: syntax error : missing '{' before ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(46) : error C2059: syntax error : ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(46) : error C2143: syntax error : missing '{' before ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(46) : error C2059: syntax error : ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(47) : error C2143: syntax error : missing '{' before ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(47) : error C2059: syntax error : ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(47) : error C2143: syntax error : missing '{' before ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(47) : error C2059: syntax error : ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(47) : error C2143: syntax error : missing '{' before ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(47) : error C2059: syntax error : ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(48) : error C2143: syntax error : missing '{' before ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(48) : error C2059: syntax error : ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(48) : error C2143: syntax error : missing '{' before ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(48) : error C2059: syntax error : ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(48) : error C2143: syntax error : missing '{' before ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(48) : error C2059: syntax error : ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(49) : error C2143: syntax error : missing '{' before ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(49) : error C2059: syntax error : ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(49) : error C2143: syntax error : missing '{' before ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(49) : error C2059: syntax error : ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(50) : error C2143: syntax error : missing '{' before ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(50) : error C2059: syntax error : ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(50) : error C2143: syntax error : missing '{' before ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(50) : error C2059: syntax error : ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(51) : error C2143: syntax error : missing '{' before ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(51) : error C2059: syntax error : ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(51) : error C2143: syntax error : missing '{' before ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(51) : error C2059: syntax error : ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(51) : error C2143: syntax error : missing '{' before ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(51) : error C2059: syntax error : ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(52) : error C2143: syntax error : missing '{' before ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(52) : error C2059: syntax error : ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(52) : error C2143: syntax error : missing '{' before ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(52) : error C2059: syntax error : ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(52) : error C2143: syntax error : missing '{' before ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(52) : error C2059: syntax error : ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(53) : error C2143: syntax error : missing '{' before ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(53) : error C2059: syntax error : ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(53) : error C2143: syntax error : missing '{' before ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(53) : error C2059: syntax error : ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(53) : error C2143: syntax error : missing '{' before ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(53) : error C2059: syntax error : ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(54) : error C2143: syntax error : missing '{' before ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(54) : error C2059: syntax error : ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(54) : error C2143: syntax error : missing '{' before ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(54) : error C2059: syntax error : ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(54) : error C2143: syntax error : missing '{' before ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(54) : error C2059: syntax error : ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(55) : error C2143: syntax error : missing '{' before ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(55) : error C2059: syntax error : ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(55) : error C2143: syntax error : missing '{' before ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(55) : error C2059: syntax error : ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(55) : error C2143: syntax error : missing '{' before ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(55) : error C2059: syntax error : ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(56) : error C2143: syntax error : missing '{' before ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(56) : error C2059: syntax error : ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(21) : error C2143: syntax error : missing '{' before ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(21) : error C2059: syntax error : ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(21) : error C2143: syntax error : missing '{' before ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(21) : error C2059: syntax error : ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(21) : error C2143: syntax error : missing '{' before ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(21) : error C2059: syntax error : ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(23) : error C2143: syntax error : missing '{' before ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(23) : error C2059: syntax error : ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(23) : error C2143: syntax error : missing '{' before ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(23) : error C2059: syntax error : ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(23) : error C2143: syntax error : missing '{' before ':'
1>c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(23) : fatal error C1003: error count exceeds 100; stopping compilation
1>Build log was saved at "file://h:\Test\Debug\BuildLog.htm"
1>Test - 102 error(s), 0 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

最佳答案

这是一个 C++ 程序,但您为源文件指定了 .c 扩展名。

默认情况下,Visual C++ 编译器将 .c 文件编译为 C。使用 .cpp 扩展名或使用/TP 标志编译以将源代码编译为 C++。

但是,即使您将源代码编译为C++,程序仍然无效。 MyNestedClassMyClass 的私有(private)成员类型,因此您不能在 main() 中构造它的实例。

关于c++ - 为什么这个程序不能在VC++2008Express中编译?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7878620/

相关文章:

java - 为什么外部类不能扩展内部类?

c++ - 使用 C++ Windows 应用程序加载 Linux 格式的文本文件

c++ - 混合 C++ ABI 以针对遗留库进行构建

c++ - C++ 析构函数中的堆栈溢出

c++ - OpenCV 中的直方图函数

c++ - 抽象类中的嵌套类

ruby - 为什么在尝试引用 Ruby 中的嵌套类时会出错?

Java:返回静态嵌套类

C++ : Fastest way to read string from stdin

c++ - 嵌套在模板化外部类中的非模板化结构如何访问外部类的静态成员?