errors with pointers

I am a beginner and I came across a problem that may be a step too big for my legs. And if it is I would be very grateful if I was told so.

I defined a a class like this:
"price::price(string id, double u_price, double p_price, double quant):ident_(id), u_price_(u_price), p_price_(p_price), quant_(quant) {};

string price::identifier() const {return ident_;};
double price::unitary_price() const {return u_price_;};
double price::promotional_price() const {return p_price_;};
double price::quantity() const {return quant_;};"

This class creates me objects with a letter and three numbers. My idea now was create a function to which I can give a string and it will return the unitary_price() of the corresponding price object.

Based on code I saw, but perhaps I did not understood it entirely, this is the function I added to the class I created:
" double price::relate_identifier_u_price(string fake){
price * product;
fake = product->identifier();
double charge = product->unitary_price();
return charge;
} "

When I try to use this function on the file that contains the int main() function:

int main(){
price producta("a", 50,130,3);
double dice(price relate_identifier_u_price("a"));
cout << dice << endl;
}

I get the following error message:
"undefined reference to 'dice()'." and I made sure I entered the header file with class declaration.

I would be very grateful if I have an opinion on the code I wrote.
This is not a problem with pointers...

1
2
double dice(price relate_identifier_u_price("a"));
cout << dice << endl;

is incorrect.

First answer these questions for yourself:
Is dice a function or a variable?
Where is it defined?

Ask again if you still cant understand.

If you are an absolute beginner in programming, for at least once, read tutorial such as this before pasting random code without understanding
http://www.cplusplus.com/doc/tutorial/

Use [Code] tags :)
I thank you for the answers, but perhaps I did not make my self clear. In the following piece of code, I have:
[

int main(){
price producta("a", 50,130,3);
price * mypointer;
mypointer = &producta;
string relate = mypointer->identifier();
double s1 = mypointer->unitary_price();
cout << relate << s1 << endl;
}
]

In this piece, I have a pointer to "producta" and, throw it, I can obtain all the information contained in the object. Here, I give the object, and I extract all about it.

Would it be possible to recover all the information of the object by just giving one of its properties? Would it be possible to write a function where I could give the string "a" and obtain the value of s1 (50)?

I tried to write such a function, but is obviously wrong. Could some tell me how this is done?
Thank you.
It doesn't work the way you're thinking.
However, you can do it by having each price object register itself with a static map that maps the identifiers to the actual objects.
Albionion wrote:
My idea now was create a function to which I can give a string
and it will return the unitary_price() of the corresponding price object.

This looks very similar to something I replied to recently...

http://cplusplus.com/forum/general/25868/
Last edited on
Topic archived. No new replies allowed.