473,381 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,381 software developers and data experts.

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 4316
weaknessforcats
9,208 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

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

Similar topics

2
by: Tim | last post by:
Please advise if you can. Presumably initialisation of members in member initialisation lists is perfomed by 'C' run-time startup. If the CRT was never started-up would those members be garbage?...
8
by: Scott J. McCaughrin | last post by:
The following program compiles fine but elicits this message from the linker: "undefined reference to VarArray::funct" and thus fails. It seems to behave as if the static data-member:...
8
by: Srini | last post by:
Hello all, I was just wondering about this. A const member function guarantees constness of the object within the function body. But there's no way for a member function to guarantee the...
6
by: lovecreatesbeauty | last post by:
Hello Experts, Why static data members can be declared as the type of class which it belongs to? Inside a class, non-static data members such as pointers and references can be declared as...
3
by: Jay | last post by:
Why are there static methods in C#. In C++ static was applied to data only (I believe) and it meant that the static piece of data was not a part of the object but only a part of the class (one...
9
by: Clint | last post by:
Hey all - Excuse the cross-post ... I'm not sure what the appropriate newsgroup would be for this question. I have a question that I'm not quite sure how to ask. For all I know, I have the...
7
by: ankitjain.bvcoe | last post by:
Hi i have the following problem in my design :::: i want to define an abstract class LogBuffer and derive two singleton classes from it i.e AlarmBuffer and FireWallBuffer.For this my design is...
7
by: Jon Vaughan | last post by:
I have a piece of code that I want to run on a Pocket Pc, I have written a data class that will store the small amount of data that is required for the program. As this class will be used via a few...
8
by: crjjrc | last post by:
Hi, I've got a base class and some derived classes that look something like this: class Base { public: int getType() { return type; } private: static const int type = 0; };
5
by: YouPoP | last post by:
I have an MFC application (VS2005) with splitter window (one is the view other is a formview). you can select from the main menu different math operation on polynoms and polynoms shows on the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.