Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 这个程序为什么不编译_C++ - Fatal编程技术网

C++ 这个程序为什么不编译

C++ 这个程序为什么不编译,c++,C++,我曾尝试将显示函数设置为public int main,但它给出了如下错误: #include <iostream> class Abc // created class with name Abc { void display()//member function { cout<<"Displaying NO."<<endl; } }; int main() { Abc obj;//creating objec

我曾尝试将显示函数设置为public int main,但它给出了如下错误:

#include <iostream>
class Abc // created class with name Abc
{
    void display()//member function
    {
        cout<<"Displaying NO."<<endl;
    }
};
int main()
{
    Abc obj;//creating object of the class
    obj.display();//calling member function inside class
}
main.cpp: In function 'int main()':
main.cpp:5:10: error: 'void Abc::display()' is private
     void display()
          ^
main.cpp:13:17: error: within this context
     obj.display();
                 ^
声明如下:

main.cpp:5:11: error: expected ':' before 'void'
    public void display()
           ^
或:


结构的默认保护是公共的,类的默认保护是私有的

错误消息非常清楚。不能在类外调用私有成员函数。默认情况下,带有关键字class的类默认具有私有访问控制。要么写

struct Abc
{
    void display()
    {
        cout<<"Displaying NO."<<endl;
    }
};
您必须对std:中声明的实体使用限定名称。比如说

using namespace std;

错误消息非常具有描述性。类中的默认访问说明符是私有的。请仔细阅读相关书籍,不要只看一眼就开始编程或将Abc改为struct;你能告诉我下面这行有什么用吗:使用名称空间std;endl也是std@Paranaix谢谢,我更新了我的帖子。@vivek321头中声明的所有名称都属于名称空间std::。该指令告诉编译器,如果它满足这样一个名称cout,它还必须查看此头中声明的名称。
class Abc // created class with name Abc
{
public:
 void display()//member function
    {
        cout<<"Displaying NO."<<endl;
    }
};
struct Abc // created class with name Abc
{
 void display()//member function
    {
        cout<<"Displaying NO."<<endl;
    }
};
 void display() const//member function
 {
        std::cout<<"Displaying NO."<<endl;
 }
using namespace std;
        std::cout<<"Displaying NO."<<std::endl;