1

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)

2
  • 1
    Honestly, it would be easier to comment on what is being done right in this code, as the list is substantially shorter. Regarding doing something wrong, from unchecked pointer dereferences, uninitialized variables, race conditions, unchecked IO, just to name a few. Commented Nov 13, 2012 at 18:37
  • i am learning c++ so i just wanna make main as small and easy to write and classes complex as possible. Commented Nov 13, 2012 at 18:43

2 Answers 2

1

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];
Sign up to request clarification or add additional context in comments.

2 Comments

yup that fixed one of the linker error. now how do initialize "a" variable once?
Exact same way, with an =0 at the end
0

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.

5 Comments

you will also need to initialize the static a to 0
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)
i thought static variables are default 0 that is why i used //static a and dont know how to initialize it to 0 if it is not the case loop maybe?
You have declared these static member variables, but not defined them. You need to define them outside of the class. Put the class declaration in a header (.h) file and the definitions (member functions and static variables) in a .cpp file.
Gabe i didn't think that much about it yeah now i now have kinda similar problem when i add inherited classes, compiles but memory access violation :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.