简体   繁体   中英

pointer array C++

I wanna make a pointer array that holds address of objects in that class so when i call scanner function it ll read pcode and search for objects has the same pcode. am i declaring array wrong? or did i misunderstand static concept? or something else ?

anyways i guess have to posting whole code

#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;
}

i know it can be done a lot more easily i am just learning so just wanna fix scanner function. it doesn't compile

1>product.obj : error LNK2001: unresolved external symbol "public: static class product * * product::point" (?point@product@@2PAPAV1@A) 1>product.obj : error LNK2001: unresolved external symbol "public: static int product::a" (?a@product@@2HA)

The code looks like a mess.

The solution to your linker problem is in defining the already declared static point member:

product* product::point[3];

Is it not compiling, or is it compiling and crashing? Always say exactly what the problem is when posting. I can see some runtime problems in it easily though.

In your loop, you're always touching the pointers at point[0], point[1], and point[2]. However, you never initialize these to null or do null checks. So if you haven't called the constructor 3 times before calling scanner, you will segfault as one or more of these pointers will be invalid.

Also, your constructor never checks for overflow, so if you call the constructor more than 3 times it will segfault. And if you're ever passing objects back and forth directly from functions remember that the compiler may insert temporary object constructors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM