急+简单的c++编程问题

来源:百度知道 编辑:UC知道 时间:2024/06/17 15:38:54
本人为了试一下编写类,所以写了以下代码
test.h

class testlei{
public:
void print();
}

test.cpp

#include<iostream>
using std::cout;
using std::ostream;
#include"test.h"

void testlei::print()
{
cout<<"!";
}

test2.cpp

#include"test.h"

int main()
{
testlei.print();
return 0;
}

有什么问题??

int main()
{
testlei.print(); <---这里
return 0;
}

不能用类名直接调用非静态函数
应该实例化一个类的对象
然后用对象调用

你那个不是静态方法,
所以得用一个对象调用
比如
testlei a;
a.print();
return 0;

和变量一样,类也需要定义
main中,先要testlei t;再调用t.print才行