这个简单的C ++类程序有什么问题?

时间:2016-06-19 13:02:08

标签: c++ class namespaces

请帮助我成为C ++初学者 我没有找到合适的解决方案。我还在这个问题中添加了错误图片。请给我答案并给出适当的解决方案。

#include <iostream>
#include <conio.h>

class test
{
    int no;
    static int count;
    public:
        void getval (int);
        void dispcount (void);

};

void test:: getval(int x)
{
    no = x;
    cout << "Number = " << no << endl;;
    count++;
}

void test::dispcount(void)
{
    cout << "Counten = " << count;
}
    int test::count;

int main()
{
    test t1,t2,t3;

    t1.dispcount();
    t2.dispcount();
    t3.dispcount();

    t1.getval(100);
    t2.getval(200);
    t3.getval(300);

    t1.dispcount();
    t2.dispcount();
    t3.dispcount();
    getch();
    return 0;
}

here is error.jpg

1 个答案:

答案 0 :(得分:1)

包含指令

#include <iostream>
#include <conio.h>

using namespace std;
//..

或者包括使用

之类的声明
#include <iostream>
#include <conio.h>

using std::cout;
using std::endl;
//...

或者使用限定名称,例如

void test:: getval(int x)
{
    no = x;
    std::cout << "Number = " << no << std::endl;
    ^^^^^^^^^                         ^^^^^^^^^^
    count++;
}

标识符coutendl在名称空间std中声明,而不是在全局名称空间中声明。

相关问题