468,504 Members | 1,946 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,504 developers. It's quick & easy.

Problem with static data members

19
Hey, guys. I have an assignment to work with static data members with numbers. I've done all the programming needed, but I keep getting this error when I try to compile:

Expand|Select|Wrap|Line Numbers
  1. 1>Number.obj : error LNK2005: "private: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > * Number::tens" (?tens@Number@@0PAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@A) already defined in driver.obj
  2. 1>Number.obj : error LNK2005: "private: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > * Number::lessThan20" (?lessThan20@Number@@0PAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@A) already defined in driver.obj
  3. 1>C:\Users\Tommy\Documents\Visual Studio 2008\Projects\Homework 1_25\Debug\Homework 1_25.exe : fatal error LNK1169: one or more multiply defined symbols found
I've gone through my files one by one and cannot find why these string arrays are being declared twice. It's probably something simple, but I don't see it. Please help, guys. I know you're capable. :-)

Here are my files:

Header file for class:

Expand|Select|Wrap|Line Numbers
  1. // Number Class
  2. // 
  3. // Each instance of Number represents a positive 
  4. // integer in the range of 0 to 99. The method inWords()
  5. // should return a string that represents the value.
  6. //
  7. // For example, if the value is 24
  8. // inWords() should return "twenty four"
  9. //
  10.  
  11. #ifndef H_NUMBER
  12. #define H_NUMBER
  13.  
  14. #include <iostream>
  15. #include <string>
  16.  
  17. using namespace std;
  18.  
  19. // Constants for array sizes
  20. const int LT20_SIZE = 21;
  21. const int TENS_SIZE = 9;
  22.  
  23.  
  24. // Declaration of Number class
  25. class Number
  26. {
  27. private:
  28.     int value;    // To hold a number
  29.  
  30.     // Static arrays to hold words
  31.     static string lessThan20[LT20_SIZE];
  32.     static string tens[TENS_SIZE];
  33.  
  34. public:
  35.  
  36.     // Constructor
  37.     Number(int x);
  38.  
  39.     // Function to return the words for the number
  40.     string inWords();
  41. };
  42.  
  43. // Static member variables must be defined
  44. // outside of the class 
  45. string Number::lessThan20[LT20_SIZE] =
  46. {       "zero", "one", "two", "three", "four", "five",
  47.         "six", "seven", "eight", "nine", "ten", 
  48.         "eleven", "twelve", "thirteen", "fourteen", 
  49.         "fifteen", "sixteen", "seventeen", "eighteen",
  50.         "nineteen"
  51. };
  52.  
  53. string Number::tens[TENS_SIZE] = 
  54. {       "twenty", "thirty", "fourty", "fifty",
  55.         "sixty", "seventy", "eighty", "ninety"
  56. };
  57.  
  58. #endif

Class source:

Expand|Select|Wrap|Line Numbers
  1. #include "Number.h"
  2. #include <string>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. Number::Number( int x )
  8. {
  9.     if ( (x >= 0) && (x <= 99) )
  10.         Number::value = x;
  11.     else
  12.         exit(EXIT_FAILURE);
  13. }
  14.  
  15. string Number::inWords()
  16. {
  17.     int temp = value;
  18.     string a, b;
  19.  
  20.     if ( temp < 20 )
  21.         return lessThan20[value];
  22.     else
  23.     {
  24.         temp /= 10;
  25.  
  26.         a = tens[temp - 2];
  27.  
  28.         if ( (value - (temp * 10)) == 0 )
  29.             return a;
  30.         else
  31.         {
  32.             b = lessThan20[(value - (temp * 10))];
  33.             return a + ' ' + b;
  34.             }
  35.     }
  36. }


Driver file provided by instructor:

Expand|Select|Wrap|Line Numbers
  1. #include "Number.h"
  2. #include <iostream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. // Program to test the Number class
  8. int main()
  9. {
  10.     // create a Number instance, passing an integer to the constructor
  11.     Number n(0);
  12.  
  13.     // get the Number's value in words
  14.     string s;
  15.     s = n.inWords();
  16.  
  17.     // print out a message
  18.     cout << "0 is " << s << endl;
  19.  
  20.  
  21.     // try more test cases
  22.     Number o(3);
  23.     cout << "3 is " << o.inWords() << endl;
  24.  
  25.     Number p(13);
  26.     cout << "13 is " << p.inWords() << endl;
  27.  
  28.     Number q(20);
  29.     cout << "20 is " << q.inWords() << endl;
  30.  
  31.     Number r(23);
  32.     cout << "23 is " << r.inWords() << endl;
  33.  
  34.     Number t(47);
  35.     cout << "47 is " << t.inWords() << endl;
  36.  
  37.     return 0;
  38. }
Thanks.
Jan 25 '08 #1
1 4039
weaknessforcats
9,207 Expert Mod 8TB
Try putting your class in the header file number.h and the member function in number.cpp:

Number.h:
Expand|Select|Wrap|Line Numbers
  1. #ifndef NUMBERH
  2. #define NUMBERH
  3. // Constants for array sizes
  4. const int LT20_SIZE = 21;
  5. const int TENS_SIZE = 9;
  6.  
  7.  
  8. // Declaration of Number class
  9. class Number
  10. {
  11. private:
  12.     int value;    // To hold a number
  13.  
  14.     // Static arrays to hold words
  15.     static string lessThan20[LT20_SIZE];
  16.     static string tens[TENS_SIZE];
  17.  
  18. public:
  19.  
  20.     // Constructor
  21.     Number(int x);
  22.  
  23.     // Function to return the words for the number
  24.     string inWords();
  25. };
  26. #endif
  27.  
Number.cpp:
Expand|Select|Wrap|Line Numbers
  1. #include "number.h"
  2.  /////////////////////////////////////////////////////////////////////////////////
  3. // Static member variables must be defined
  4. // outside of the class 
  5. string Number::lessThan20[LT20_SIZE] =
  6. {       "zero", "one", "two", "three", "four", "five",
  7.         "six", "seven", "eight", "nine", "ten", 
  8.         "eleven", "twelve", "thirteen", "fourteen", 
  9.         "fifteen", "sixteen", "seventeen", "eighteen",
  10.         "nineteen"
  11. };
  12.  
  13. string Number::tens[TENS_SIZE] = 
  14. {       "twenty", "thirty", "fourty", "fifty",
  15.         "sixty", "seventy", "eighty", "ninety"
  16. };
  17. Number::Number( int x )
  18. {
  19.     if ( (x >= 0) && (x <= 99) )
  20.         Number::value = x;
  21.     else
  22.         exit(EXIT_FAILURE);
  23. }
  24.  
  25. string Number::inWords()
  26. {
  27.     int temp = value;
  28.     string a, b;
  29.  
  30.     if ( temp < 20 )
  31.         return lessThan20[value];
  32.     else
  33.     {
  34.         temp /= 10;
  35.  
  36.         a = tens[temp - 2];
  37.  
  38.         if ( (value - (temp * 10)) == 0 )
  39.             return a;
  40.         else
  41.         {
  42.             b = lessThan20[(value - (temp * 10))];
  43.             return a + ' ' + b;
  44.             }
  45.     }
  46. }
  47.  
What was happening was that the static variables were being created each time you included your header. Static variables have internal linkage which means they can only be used in the file that defines them.

All I did was structure your code so that the variables were created once in the number.cpp.
Jan 26 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

8 posts views Thread by Scott J. McCaughrin | last post: by
8 posts views Thread by Srini | last post: by
3 posts views Thread by Jay | last post: by
7 posts views Thread by ankitjain.bvcoe | last post: by
7 posts views Thread by Jon Vaughan | last post: by
3 posts views Thread by gieforce | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.