CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Apr 2010
    Posts
    25

    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.

  2. #2
    Join Date
    Dec 2008
    Posts
    144

    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?

  3. #3
    Join Date
    Apr 2010
    Posts
    25

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

  4. #4
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: linker error caused by class - help needed

    Code:
    	Item();
    	Item(double price, int gramsOfFat);
    These 2 functions are only declared, not implemented.

  5. #5
    Join Date
    Apr 2010
    Posts
    25

    Re: linker error caused by class - help needed

    Quote Originally Posted by Skizmo View Post
    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?

  6. #6
    Join Date
    Aug 2002
    Posts
    756

    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.

  7. #7
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    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.

  8. #8
    Join Date
    Apr 2010
    Posts
    25

    Re: linker error caused by class - help needed

    Quote Originally Posted by Skizmo View Post
    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.

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: linker error caused by class - help needed

    Quote Originally Posted by turlisk View Post
    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

  10. #10
    Join Date
    Apr 2010
    Posts
    25

    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
  •  





Click Here to Expand Forum to Full Width

Featured