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: - 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
-
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
-
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: - // Number Class
-
//
-
// Each instance of Number represents a positive
-
// integer in the range of 0 to 99. The method inWords()
-
// should return a string that represents the value.
-
//
-
// For example, if the value is 24
-
// inWords() should return "twenty four"
-
//
-
-
#ifndef H_NUMBER
-
#define H_NUMBER
-
-
#include <iostream>
-
#include <string>
-
-
using namespace std;
-
-
// Constants for array sizes
-
const int LT20_SIZE = 21;
-
const int TENS_SIZE = 9;
-
-
-
// Declaration of Number class
-
class Number
-
{
-
private:
-
int value; // To hold a number
-
-
// Static arrays to hold words
-
static string lessThan20[LT20_SIZE];
-
static string tens[TENS_SIZE];
-
-
public:
-
-
// Constructor
-
Number(int x);
-
-
// Function to return the words for the number
-
string inWords();
-
};
-
-
// Static member variables must be defined
-
// outside of the class
-
string Number::lessThan20[LT20_SIZE] =
-
{ "zero", "one", "two", "three", "four", "five",
-
"six", "seven", "eight", "nine", "ten",
-
"eleven", "twelve", "thirteen", "fourteen",
-
"fifteen", "sixteen", "seventeen", "eighteen",
-
"nineteen"
-
};
-
-
string Number::tens[TENS_SIZE] =
-
{ "twenty", "thirty", "fourty", "fifty",
-
"sixty", "seventy", "eighty", "ninety"
-
};
-
-
#endif
Class source: - #include "Number.h"
-
#include <string>
-
#include <iostream>
-
-
using namespace std;
-
-
Number::Number( int x )
-
{
-
if ( (x >= 0) && (x <= 99) )
-
Number::value = x;
-
else
-
exit(EXIT_FAILURE);
-
}
-
-
string Number::inWords()
-
{
-
int temp = value;
-
string a, b;
-
-
if ( temp < 20 )
-
return lessThan20[value];
-
else
-
{
-
temp /= 10;
-
-
a = tens[temp - 2];
-
-
if ( (value - (temp * 10)) == 0 )
-
return a;
-
else
-
{
-
b = lessThan20[(value - (temp * 10))];
-
return a + ' ' + b;
-
}
-
}
-
}
Driver file provided by instructor: - #include "Number.h"
-
#include <iostream>
-
#include <string>
-
-
using namespace std;
-
-
// Program to test the Number class
-
int main()
-
{
-
// create a Number instance, passing an integer to the constructor
-
Number n(0);
-
-
// get the Number's value in words
-
string s;
-
s = n.inWords();
-
-
// print out a message
-
cout << "0 is " << s << endl;
-
-
-
// try more test cases
-
Number o(3);
-
cout << "3 is " << o.inWords() << endl;
-
-
Number p(13);
-
cout << "13 is " << p.inWords() << endl;
-
-
Number q(20);
-
cout << "20 is " << q.inWords() << endl;
-
-
Number r(23);
-
cout << "23 is " << r.inWords() << endl;
-
-
Number t(47);
-
cout << "47 is " << t.inWords() << endl;
-
-
return 0;
-
}
Thanks.
1 4317
Try putting your class in the header file number.h and the member function in number.cpp:
Number.h: -
#ifndef NUMBERH
-
#define NUMBERH
-
// Constants for array sizes
-
const int LT20_SIZE = 21;
-
const int TENS_SIZE = 9;
-
-
-
// Declaration of Number class
-
class Number
-
{
-
private:
-
int value; // To hold a number
-
-
// Static arrays to hold words
-
static string lessThan20[LT20_SIZE];
-
static string tens[TENS_SIZE];
-
-
public:
-
-
// Constructor
-
Number(int x);
-
-
// Function to return the words for the number
-
string inWords();
-
};
-
#endif
-
Number.cpp: -
#include "number.h"
-
/////////////////////////////////////////////////////////////////////////////////
-
// Static member variables must be defined
-
// outside of the class
-
string Number::lessThan20[LT20_SIZE] =
-
{ "zero", "one", "two", "three", "four", "five",
-
"six", "seven", "eight", "nine", "ten",
-
"eleven", "twelve", "thirteen", "fourteen",
-
"fifteen", "sixteen", "seventeen", "eighteen",
-
"nineteen"
-
};
-
-
string Number::tens[TENS_SIZE] =
-
{ "twenty", "thirty", "fourty", "fifty",
-
"sixty", "seventy", "eighty", "ninety"
-
};
-
Number::Number( int x )
-
{
-
if ( (x >= 0) && (x <= 99) )
-
Number::value = x;
-
else
-
exit(EXIT_FAILURE);
-
}
-
-
string Number::inWords()
-
{
-
int temp = value;
-
string a, b;
-
-
if ( temp < 20 )
-
return lessThan20[value];
-
else
-
{
-
temp /= 10;
-
-
a = tens[temp - 2];
-
-
if ( (value - (temp * 10)) == 0 )
-
return a;
-
else
-
{
-
b = lessThan20[(value - (temp * 10))];
-
return a + ' ' + b;
-
}
-
}
-
}
-
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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?...
|
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:...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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;
};
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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...
|
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: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |