-
May 1st, 2010, 12:36 AM
#1
linker error caused by class - help needed
i'm working on a small project and i created a class called item that takes two arguments one double and one int,
i've i have a default blank build for that class with no arguments as well where the user can set them if needed.
i'm trying to make an array of them and i'm getting a linker error for unresolved externals
here is a segment of my code.
[code]
class Item
{
public:
// Constructors
Item();
Item(double price, int gramsOfFat);
private:
double price;
int gramsOfFat;
}
void main ()
{
const int MAX_ITEMS = 10;
Item vendingMachine[MAX_ITEMS];
// other stuff
double Price = 0;
int GramsOfFat = 0;
}
I have other code but i think this is the problem, any help would be great.
-
May 1st, 2010, 03:26 AM
#2
Re: linker error caused by class - help needed
First of all, it would be helpful to see the exact error message.
Second, you implemented both of your constructors, didn't you?
-
May 1st, 2010, 12:39 PM
#3
Re: linker error caused by class - help needed
main.obj : error LNK2019: unresolved external symbol "public: __thiscall Item::Item(void)" (??0Item@@QAE@XZ) referenced in function _main
F:\VendingMachineQuizStudent\Debug\VendingMachineQuiz.exe : fatal error LNK1120: 1 unresolved externals
here is a little more of my code
Code:
class Item
{
public:
// Constructors
Item();
Item(double price, int gramsOfFat);
// Methods
void setPrice(double pPrice)
{
price = pPrice;
}
void setGramsOfFat(int pGramsOfFat)
{
gramsOfFat= pGramsOfFat;
}
double getPrice()
{
return price;
}
int getGramsOfFat()
{
return gramsOfFat;
}
double raisePrice(double raisedPrice)
{
price += raisedPrice;
}
private:
double price;
int gramsOfFat;
double raisedPrice;
};
void main()
{
//constants and classes
const int MAX_ITEMS = 10;
Item vendingMachine[MAX_ITEMS];
// other variables
double Price = 0;
int GramsOfFat = 0;
string textSpacer = " ";
string vendStrPrice = "Price";
string vendStrFatGrams = "Fat Grams";
srand( (unsigned)time( NULL ) );
for (int i = 0; i <= MAX_ITEMS; i++)
{
vendingMachine[i].setPrice((rand()%175 + 75) / 100);
vendingMachine[i].setGramsOfFat(rand()%30 + 5);
}
cout.setf(ios::showpoint);
cout.precision(2);
cout << left << vendStrPrice << textSpacer << vendStrFatGrams << endl;
for (int i = 0; i <= MAX_ITEMS; i++)
{
cout << left << cout << vendingMachine[i].getPrice() << textSpacer
<< vendingMachine[i].getGramsOfFat() << endl;
}
-
May 1st, 2010, 03:20 PM
#4
Re: linker error caused by class - help needed
Code:
Item();
Item(double price, int gramsOfFat);
These 2 functions are only declared, not implemented.
-
May 1st, 2010, 04:24 PM
#5
Re: linker error caused by class - help needed
Originally Posted by Skizmo
Code:
Item();
Item(double price, int gramsOfFat);
These 2 functions are only declared, not implemented.
down in my main function i have declared my item class as an array, is there a different way to declare this?
-
May 1st, 2010, 05:31 PM
#6
Re: linker error caused by class - help needed
Hi,
You need to do this:
Code:
Item::Item()
{
}
Item::Item(double price, int gramsOfFat)
{
m_price = price;
m_gramsOfFat = gramsOfFat;
}
or put this in the class definition.
and then in your class definition you need this:
Code:
class Item
{
...........
private:
double m_price;
int m_gramsOfFat;
};
Notice the "m_" prefix here.
HTH.
Thank you.
Last edited by OneEyeMan; May 1st, 2010 at 05:33 PM.
-
May 1st, 2010, 06:14 PM
#7
Re: linker error caused by class - help needed
down in my main function i have declared my item class as an array, is there a different way to declare this?
What you are doing in the main function has nothing to do with this. You declared 2 constructors, but never implemented the functions itself.
-
May 1st, 2010, 06:43 PM
#8
Re: linker error caused by class - help needed
Originally Posted by Skizmo
What you are doing in the main function has nothing to do with this. You declared 2 constructors, but never implemented the functions itself.
cool got it thanks guys,
instead of starting a new topic i'm going to append it to this one if that is okay..
I think i have a logic math error.
i'm trying get a value between 0.75 and 1.75 randomly set i keep getting 2 and 3's and not sure why.
i've set up the line of code as follows
Code:
for (int i = 0; i < MAX_ITEMS; i++)
{
vendingMachine[i].setPrice((rand()%175 + 75.0 / 100.0));
vendingMachine[i].setGramsOfFat(rand()%30 + 5);
}
now if i'm doing this right if the cap is 175 on the random number then i'm adding 75 to it incase it is 0.
i keep getting odd results even numbers like 1.3e+002
i have placed in a 2 point precision and later cout the results i think i may be messing something up or overlooked something.
-
May 1st, 2010, 07:17 PM
#9
Re: linker error caused by class - help needed
Originally Posted by turlisk
cool got it thanks guys,
instead of starting a new topic i'm going to append it to this one if that is okay..
I think i have a logic math error.
i'm trying get a value between 0.75 and 1.75 randomly set i keep getting 2 and 3's and not sure why.
1) Generate a random number between 0 and 100.
2) add 75 to 1).
3) divide 2) by 100.
Regards,
Paul McKenzie
-
May 1st, 2010, 08:03 PM
#10
Re: linker error caused by class - help needed
thanks guys got it all figured out now.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|