我想创建一个指针数组来保存该类中对象的地址,因此当我调用扫描仪函数时,它会读取 pcode 并搜索具有相同 pcode 的对象。我声明数组错误吗?还是我误解了静态概念?或者是其他东西 ?
无论如何,我想必须发布整个代码
#include <string>
using namespace std;
class product{
public:
product();
product(long&,string&);
void setCode();
void getCode(long);
void static scanner();
void const printer();
static product *point[3];
static int a;
private:
string pname;
long pcode;
};/*
class PrepackedFood:public product{
public:
PrepackedFood(long&, string&,double);
private:
double uPrice;
};
class FreshFood:public product{
public:
FreshFood(long&,string&,double,double);
private:
double weight;
double pricepk;
};*/
#include "product.h"
#include <iostream>
product::product(){pcode=0;pname="unknown";
point[a]= this;
a++;}
product::product(long& c,string&n){pcode=c;pname=n;
}
//void const product::printer(){cout<<getCode()}
void product::setCode(){ cout<<"enter product name\n ";cin>>pname;
cout<<"enter product code _____\b\b\b\b\b";cout<<"\a";
cin>>pcode;cout<<endl;
cout<<pname<<endl;
cout<<pcode<<endl;
}
void product::getCode(long s){
if ((*this).pcode=s){
printer();
}
}
void product::scanner(){
long a;
cout<<"SCANNING!\a_____\b\b\b\b\b";cin>>a;
int i=0;
while(i<3){
if (point[i]->pcode==a){point[i]->printer();
break;
}
i++;
//(i==3)?cout<<"try again\n":"\a";
}
}
void const product::printer(){
cout<<pname<<endl;
cout<<pcode<<endl;
}
#include "product.h"
int main(){
product a[3];
int i=0;
while(i<3){
a[i].setCode();
i++;
}
product::scanner();
return 0;
}
我知道它可以更容易地完成我只是在学习所以只想修复扫描仪功能。它不编译
1>product.obj:错误 LNK2001:未解析的外部符号“public:静态类产品 * * product::point”(?point@product@@2PAPAV1@A) 1>product.obj:错误 LNK2001:未解析的外部符号“public : 静态整数产品::a" (?a@product@@2HA)