Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2010
    Location
    Denmark
    Posts
    233

    C++ homework 4 problems - LINK2005 errors

    Following the homework of C++ 101 week 4, I have put in my stuff into separate functions and headers.

    My structure looks like this:

    Main.cpp
    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    #include "Products.h"
    using namespace std;
    
    int main()
    {
    	cout << "Hello. Please enter 4 products - first name and then price.\n\n";
    	GetNamesAndPrices();
    	cout << "\n\nNow tell me how many Buzz Coins you have?\n> ";
    	cin >> userMoney;
    	ShopLoop();
    
    	return 0;
    }
    Products.h
    Code:
    #ifndef PRODUCTS_H
    #define PRODUCTS_H
    
    #pragma once
    
    #include <string>
    using namespace std;
    
    	extern double productPrices[4];
    	extern string productNames[4];
    
    	extern double userMoney = 0;
    	extern int userChoice = 1;
    	extern bool continueShopping = true;
    
    	extern int productIndex = 0;
    
    	// Prototypes
    	void GetNamesAndPrices();
    	void BubbleSort();
    	void CheckIfMoneyIsZero();
    	void ShopLoop();
    
    #endif
    ProductsAndNames.cpp
    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    #include "Products.h"
    using namespace std;
    
    void GetNamesAndPrices()
    {
    	// Stuff
    }
    
    void BubbleSort()
    {
    	// Stuff
    }
    
    void CheckIfMoneyIsZero()
    {
    	// Stuff
    }
    
    void ShopLoop()
    {
    	// Stuff
    }
    However, I get these the following errors. I guess it has something to do with the order/way I declare variables, but I am unable to solve the problem. Can you help me?

    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	PLEASE SEE THIS PICTURE FIRST.jpg 
Views:	256 
Size:	332.4 KB 
ID:	72715  

  2. #2
    Join Date
    Oct 2009
    Location
    The In Between. US
    Posts
    2,030
    when using extern do you not need to also declare it in main? just like with functions, you let the compiler know that it does exist somewhere else
    elevating to new levels of knowledge (that's the plan at least )

  3. #3
    Join Date
    Aug 2010
    Location
    Denmark
    Posts
    233
    If I try to put my variables in Main.cpp as well, I get this error message:

    Code:
    Error	1	error C2374: 'userMoney' : redefinition; multiple initialization	c:\users\wikzo\documents\visual studio 2010\projects\cpp101\week4\homework4-wikzo\homework4-wikzo\main.cpp	22
    [

  4. #4
    Join Date
    Oct 2009
    Location
    The In Between. US
    Posts
    2,030
    try leaving the extern one empty with no value
    elevating to new levels of knowledge (that's the plan at least )

  5. #5
    Join Date
    Nov 2008
    Location
    41° 28′ 56″ N, 81° 40′ 11″ W
    Posts
    2,399
    Hmm, yes, I've had these troubles with mine until a few days ago when I realized I needed to make more use of externs and anonymous namespaces, these are key when working with programs like these. Did you make sure to wrap your global variables in anonymous namespaces in each CPP file?
    PC Specs: Antec 900 ATX Case, Gigabyte 790GX Mobo, AMD Phenom II x4 955, Corsair HX620W PSU, Corsair Vengeance 16GB (4x4GB) DDR3-1600, EVGA GTX 680 2GB GDDR5, 3D Vision Kit 2, LG 8x Blu-ray Burner, Intel X25-M SSD, 500GB+640GB WD Caviar Blacks, Win 7 Pro x64, DAS Pro K/B, MX518 Mouse, Logitech Z-2300 Speakers, Toshiba 32TL515U 32" 3DTV, Wacom Bamboo Fun Large Tablet.

  6. #6
    mantragora Guest
    Declare those variables in products.cpp. You than extern them in products.h. By the way. Use Inlude Guards or #pragma Once, you don't need them both.

  7. #7
    Join Date
    Jun 2003
    Location
    Trier, Germany
    Posts
    1,350
    gcc will even give a warning hinting at what has been mentioned by elevating at mantragora:
    Code:
    Products.h:12: warning: ‘userMoney’ initialized and declared ‘extern’
    The whole point of using extern is to maintain the one-definition-rule of C++. In order for this to work, the variables must be defined out-of-line in one of the .cpp files.

    Btw, if all of this seems unnecessary tedious and complicated, you are on the right track. In real-world code it is considered very bad practice to share data between compilation units. Instead of using extern, data is exposed through functions. This approach is somewhat naturally enforced once you start using classes.

    Quote Originally Posted by mantragora View Post
    By the way. Use Inlude Guards or #pragma Once, you don't need them both.
    Yes you do. To be exact: You can either use include guards only or both.
    #pragma once is non-standard but may be faster if the compiler supports it. But you might have to put the #pragma once *before* the include guard to benefit from this.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •