[Error] cannot call member function ‘‘ without object
程序员文章站
2022-05-30 21:34:41
...
今天遇到一个错误
[Error] cannot call member function ‘int Computer::getCnt()’ without object
意思是不能在没有对象的情况下,调用Computer类的成员函数getCnt();
原代码
#include<iostream>
#include<cstring>
using namespace std;
class Computer{
public:
char *name;
static int cnt;
Computer(char *n){
//使用strcpy将n复制到name中
int l=strlen(n);
name=new char[l+1];
strcpy(name,n);
cnt++;
}
Computer(const Computer&p)//拷贝构造函数
{
int l=strlen(p.name);
name=new char[l+1];
strcpy(name,p.name);
cnt++;
}
~Computer(){
delete []name;
cnt--;
}
Computer setName(char* nn){
int l=strlen(nn);
name=new char[l+1];
strcpy(name,nn);
return *this;//return this返回自身对象的指针
//return *this返回的是当前对象的克隆或者本身(若返回类型为A, 则是克隆, 若返回类型为A&, 则是本身 )
}
void print(){
int ll=strlen(name);
for(int i=0;i<ll;i++){
cout<<name[i];
}
cout<<endl;
// cout<<name<<endl;
}
int getCnt(){
return cnt;
}
};
int Computer::cnt=0;
int main()
{
char str[101];
cout<<"1:"<<Computer::getCnt()<<" computers."<<endl;
Computer com1("Tom"), com2(com1);
cout<<"2:"<<com2.getCnt()<<" computer."<<endl;
cin>>str;
com2.setName(str).print();
com2.print();
cout<<"3:"<<com2.getCnt()<<" computer."<<endl;
return 0;
}
这里在代码第60行其实是想输出static变量sum的初始值,需要调用静态函数输出,否则出现上述错误
修改后代码
#include<iostream>
#include<cstring>
using namespace std;
class Computer{
public:
char *name;
static int cnt;
Computer(char *n){
//使用strcpy将n复制到name中
int l=strlen(n);
name=new char[l+1];
strcpy(name,n);
cnt++;
}
Computer(const Computer&p)//拷贝构造函数
{
int l=strlen(p.name);
name=new char[l+1];
strcpy(name,p.name);
cnt++;
}
~Computer(){
delete []name;
cnt--;
}
Computer setName(char* nn){
int l=strlen(nn);
name=new char[l+1];
strcpy(name,nn);
return *this;//return this返回自身对象的指针
//return *this返回的是当前对象的克隆或者本身(若返回类型为A, 则是克隆, 若返回类型为A&, 则是本身 )
}
void print(){
int ll=strlen(name);
for(int i=0;i<ll;i++){
cout<<name[i];
}
cout<<endl;
// cout<<name<<endl;
}
static int getCnt(){
return cnt;
}
};
int Computer::cnt=0;
int main()
{
char str[101];
cout<<"1:"<<Computer::getCnt()<<" computers."<<endl;
//[Error] cannot call member function 'int Computer::getCnt()' without object
//不能在没有对象的情况下,调用Computer类的成员函数getCnt();
//修改:将getCnt()声明为静态函数
Computer com1("Tom"), com2(com1);
cout<<"2:"<<com2.getCnt()<<" computer."<<endl;
cin>>str;
com2.setName(str).print();
com2.print();
cout<<"3:"<<com2.getCnt()<<" computer."<<endl;
return 0;
}
其实直接将使用静态变量的函数定义为静态函数,就可以避免出现此类错误
参考
1.关于错误信息:cannot call member function ’ ’ without object
2.Error:cannot call member function without object
推荐阅读
-
jQuery的$.get()函数不执行以及php端报错Uncaught Error: Call to a member function bind_param() on boolean in...
-
php用mysqli模块连数据库Fatal error: Call to a member function query() 。解决思路
-
求大神纠错,多谢!Fatal error: Call to a member function query() on a non-object
-
Call to a member function fetch() on a non-object异常
-
Fatal error: Call to a member function fetch_array() on a non-object in 62 line
-
Wordpress PHP Fatal error: Call to a member function get()
-
php中出现“Call to a member function Execute() on a non-object”的有关问题
-
Fatal error: Call to a member function fetch_array() on a non-object in 62 line
-
phpmyadmin出现了Fatal error: Call to a member function getName() 如何都打不开了
-
phpmyadmin出现了Fatal error: Call to a member function getName() 如何都打不开了