我想打一個指針數組保存該類的實例的地址,所以當我把我的scanner
功能將搜索具有相同pcode
,並打印出來的對象。我這樣做,但現在當我嘗試使用不同的繼承類對象時,我得到了「內存訪問衝突」,我無法通過基本靜態掃描函數訪問繼承的類函數。動態對象指針數組C++
對不起,這裏的混亂是我的代碼
using namespace std;
class product{
public:
product();
product(long&,string&);
void const printer();
void setCode();
void getCode(long);
void static scanner();
static product *point; //when this was static did compile but not now
static int a;
private:
string pname;
long pcode;
};
class PrepackedFood:public product{
//snip
private:
double uPrice;
};
class FreshFood:public product{
//snip
private:
double weight;
double pricepk;
};
product.cpp
product *product::point=new product [15]; //when i use try to use dynamic cant
int product::a(0);
product::product(){
pcode=0;
pname="unknown";
point[a]= this; //here works for normal arrays not now
a++;
}
product::product(long& c,string&n){
pcode=c;
pname=n;
}
void product::scanner(){
long a;
int i=0;
while(i<3){
if (point[i]->pcode==a){
point[i]->printer();
break;
}
i++;
}
}
void product::setCode(){
cout<<"enter product name\n ";
cin>>pname;
cout<<"enter product code _____\b\b\b\b\b\a";
cin>>pcode;
}
//blah blah for other members
的main.cpp
#include "product.h"
#include <iostream>
int main(){
int i=0;
cout<<"enter fresh foods"<<endl;
FreshFood f[3];
for(int a=0;a<3;a++)
f[i].setCode();
product::scanner();
return 0;
}
是它的內存地址的問題或完全不同的東西?爲什麼scan{this->print()}
調用基本函數?有沒有辦法調用繼承的print()
函數?
請將您的代碼修剪到最小的可重複樣本。在那裏有很多代碼是完全不相關的。 –
對不起,我真的很累,不知道哪一個是相關的 – meh
那是你的工作,你討價還價的結束。你需要幫助,而不是我們,所以你應該讓我們很容易幫助你。你不會把完全測試的工作交給我們。期待您在發佈之前完成一些工作是完全合理的。 –