473,480 Members | 1,943 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

undefined reference

21 New Member
When I try to compile the program the re is warning: undefined reference to 'operator<<(std::basic_ostream<char, std::char_traits<char> >&, MyString const)'
And there are more similar warnings. Can anyone help me, please

Hi, here is the class definition:
Expand|Select|Wrap|Line Numbers
  1. #ifndef MYSTRING_H
  2. #define MYSTRING_H
  3. #include <iostream>
  4. #include <cstring>
  5. using namespace std;
  6. class MyString
  7. {
  8. //friend functions
  9. friend ostream& operator << (ostream& leftOp, const MyString& rightOp);
  10. friend istream& operator >> (istream& leftOp, MyString& rightOp);
  11. friend bool operator == (const char* leftOp, const MyString& rightOp);
  12. public:
  13. //methods
  14. MyString();
  15. MyString(const char* s);
  16. bool operator == (const MyString& rightOp) const;
  17. bool operator == (const char* rightOp) const;
  18. char operator [] (int sub) const;
  19. char& operator [] (int sub);
  20. const char* c_str() const;
  21. bool empty() const;
  22. int size() const;
  23. void clear();
  24. private:
  25. //data memebers
  26. char StringArray[81]; //to store string
  27. int StringSize; //current size of the string
  28. static const int StringCapacity = 80; //maximum size
  29. };
  30. #endif
Here is the class implementation:

Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. #include "MyString.h"
  3. //no argument constructor
  4. //initializes the string array with null value
  5. //and size to zero
  6. MyString::MyString()
  7. {
  8. strcpy(StringArray, "");
  9. StringSize = 0;
  10. }
  11. //argumented constructor
  12. //initializes string array with passed argument
  13. MyString::MyString(const char* s)
  14. {
  15. int len = strlen(s);
  16. if(len <= StringCapacity) //size <= 80
  17. {
  18. strcpy(StringArray, s);
  19. StringSize = len;
  20. }
  21. else //size > 80
  22. {
  23. int i;
  24. for(i=0;i<=StringCapacity;i++)
  25. StringArray[i] = s[i];
  26. StringArray[i] = '\0';
  27. StringSize = StringCapacity;
  28. }
  29. }
  30. //overloded equal operator with MyString argument
  31. bool MyString::operator==(const MyString& rightOp)const
  32. {
  33. return (strcmp(this->StringArray, rightOp.StringArray) == 0);
  34. }
  35. //overloded equal operator with charcter string
  36. bool MyString::operator==(const char* rightOp)const
  37. {
  38. return (strcmp(this->StringArray, rightOp) == 0);
  39. }
  40. //overloaded indexing operator
  41. //returns character at the specified position in the array
  42. char MyString::operator[](int sub) const
  43. {
  44. if ( sub >= 0 && sub <= StringSize)
  45. return StringArray[sub];
  46. else
  47. return ' ';
  48. }
  49. //overloaded indexing operator
  50. //returns reference of the character at the specified position in the array
  51. char& MyString::operator [] (int sub)
  52. {
  53. return StringArray[sub];
  54. }
  55. //returns StringArray
  56. const char* MyString::c_str()const
  57. {
  58. return StringArray;
  59. }
  60. //returns true if size of StringArray is zero
  61. //otherwise false
  62. bool MyString::empty()const
  63. {
  64. return (StringSize == 0);
  65. }
  66. //returns size of the StringArray
  67. int MyString::size() const
  68. {
  69. return StringSize;
  70. }
  71. //clears the StringArray
  72. void MyString::clear()
  73. {
  74. strcpy(StringArray, "");
  75. StringSize = 0;
  76. }
  77. //friend function definitions
  78. //overloaded extraction operator
  79. ostream& operator <<(ostream& leftOp, const MyString& rightOp)
  80. {
  81. leftOp << rightOp.c_str() <<endl;
  82. return leftOp;
  83. }
  84. //overloaded insertion operator
  85. istream& operator >> (istream& leftOp, MyString& rightOp)
  86. {
  87. char st[81];
  88. leftOp >> st;
  89. MyString s(st);
  90. rightOp = s;
  91. return leftOp;
  92. }
  93. //overloaded equality operator with reference of MyString argument
  94. //and a charcter string
  95. bool operator == (const char* leftOp, const MyString& rightOp)
  96. {
  97. return (strcmp(leftOp, rightOp.c_str()) == 0);
Here is the main program:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <cstring>
  3. #include "MyString.h"
  4.  
  5. using std::cin;
  6. using std::cout;
  7. using std::endl;
  8.  
  9. int main()
  10.    {
  11.    cout << "Testing default constructor\n\n";
  12.  
  13.    MyString s1;
  14.  
  15.    cout << "s1: " << s1 << endl;   
  16.    cout << "s1 size: " << s1.size() << endl;
  17.    cout << "s1 is " << ((s1.empty()) ? "empty\n" : "not empty\n");
  18.    cout << endl;
  19.  
  20.    cout << "Testing second constructor\n\n";
  21.  
  22.    MyString s2 = "some text";
  23.  
  24.    cout << "s2: " << s2 << endl;   
  25.    cout << "s2 size: " << s2.size() << endl;
  26.    cout << "s2 is " << ((s2.empty()) ? "empty\n" : "not empty\n");
  27.    cout << endl;
  28.  
  29.    cout << "Testing size limit on second constructor\n\n";
  30.  
  31.    MyString s3 = "This is a really long string and not all of it will actually end up in the array, but that is okay";
  32.  
  33.    cout << "s3: " << s3 << endl;   
  34.    cout << "s3 size: " << s3.size() << endl;
  35.    cout << "s3 is " << ((s3.empty()) ? "empty\n" : "not empty\n");
  36.    cout << endl;
  37.  
  38.    cout << "Testing write form of subscript operator\n\n";
  39.  
  40.    s2[0] = 'S';
  41.    s2[5] = 'T';
  42.  
  43.    cout << "Testing read form of subscript operator\n\n";
  44.  
  45.    cout << "s2: ";
  46.    for (int i = 0; i < s2.size(); i++)
  47.       cout << s2[i];
  48.    cout << endl << endl;
  49.  
  50.    cout << "Testing conversion to C string\n\n";
  51.  
  52.    char ar[81];
  53.  
  54.    strcpy(ar, s2.c_str());
  55.  
  56.    cout << "s2 as C string: " << ar << endl << endl;
  57.  
  58.    cout << "Testing equality operators\n\n";
  59.  
  60.    MyString s4 = "Some Text";
  61.  
  62.    cout << "s2 and s4 are " << ((s2 == s4) ? "equal\n" : "not equal\n");
  63.    cout << "s3 and s4 are " << ((s3 == s4) ? "equal\n" : "not equal\n");
  64.    cout << "s4 and \"Some Text\" are " << ((s4 == "Some Text") ? "equal\n" : "not equal\n");
  65.    cout << "\"More Text\" and s4 are " << (("More Text" == s4) ? "equal\n" : "not equal\n");
  66.    cout << endl;
  67.  
  68.    cout << "Testing clear() method\n\n";
  69.  
  70.    s3.clear();
  71.  
  72.    cout << "s3: " << s3 << endl;   
  73.    cout << "s3 size: " << s3.size() << endl;
  74.    cout << "s3 is " << ((s3.empty()) ? "empty\n" : "not empty\n");
  75.    cout << endl;
  76.  
  77.    cout << "Testing stream extraction operator\n\n";
  78.  
  79.    cout << "Type a word: ";
  80.    cin >> s1;
  81.  
  82.    cout << "\ns1: " << s1 << endl;   
  83.    cout << "s1 size: " << s1.size() << endl;
  84.    cout << "s1 is " << ((s1.empty()) ? "empty\n" : "not empty\n");
  85.  
  86.    return 0;
  87.    }
  88.  
Oct 7 '11 #1
4 1527
weaknessforcats
9,208 Recognized Expert Moderator Expert
I compiled this code using Visual Studio.NET 2008 and there were no errors.

Your undefined reference to operator<< is somewhere else.

Are you sure your file with the class implementation and the file with main() are both compiled and linked during your build?
Oct 7 '11 #2
Barbara Baby
21 New Member
I have the make file to link and compile the program. It looks like this:

# Compiler variables
CCFLAGS = -ansi -Wall

# Rule to link object code files to create executable file
assign4: assign4.o MyString.o
g++ $(CCFLAGS) -o assign4 assign4.o MyString.o

# Rule to compile source code files to object code
assign4.o: assign4.cpp MyString.h
g++ $(CCFLAGS) -c assign4.cpp

MyString.o: MyString.cpp MyString.h
g++ $(CCFLAGS) -c MyString.cpp

# Pseudo-target to remove object code and executable files
clean:
-rm *.o assign4
Oct 7 '11 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
Try this:

Make a copy of assign4.cpp and at the end of the file #include "MyString.cpp"

Then just build and link the combined file. There should be no errors.

If there are the problem is in the makefile.
Oct 7 '11 #4
Barbara Baby
21 New Member
Thank you very much weknessforcats. It works properly this time
Oct 8 '11 #5

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

Similar topics

2
13102
by: RU | last post by:
Hi, I am working on a porting project to port C/C++ application from unixware C++, AT&T Standard components to g++ with STL on Linux. This application has been working properly on...
1
3128
by: Codemutant | last post by:
**** Post for FREE via your newsreader at post.usenet.com **** I just cannot find what is undefined in this code.
1
30977
by: Dom | last post by:
I'm new to c++. Just started learning it 24 hours ago. Am running into a compile problem. Please, no one waste the effort telling me to google it. I've been researching it for quite a while with no...
1
4261
by: Andre Janssen | last post by:
Hi.... I tried to compile the following src with this command: "g++ -Wall -o bla alsaswitch.cpp". The src is an example src of xosd package. #include <xosd.h> int main (int argc, char...
3
789
by: Michael Sgier | last post by:
Hi i get thousands of messages like below. How shall i resolve that? Thanks Mcihael Release/src/Utility/RawImage.o: In function `CMaskImage::CMaskImage(int, int, char const*)':...
5
11329
by: druberego | last post by:
I read google and tried to find the solution myself. YES I do know that you can get undefined references if you: a) forget to implement the code for a prototype/header file item, or b) you forget...
3
11581
by: prakash.mirji | last post by:
Hello, I am getting below mention linker error when I tried to link my class test.C I use below command to compile test.C /usr/bin/g++ -g -fpic -fvisibility=default -D_POSIX_SOURCE...
1
10882
by: taiyang902 | last post by:
i program under linux ,and using kdevelop c/c++. the code follow, #ifdef HAVE_CONFIG_H #include <config.h> #endif #include <iostream> #include <cstdlib> using namespace std;
2
6209
by: zqiang320 | last post by:
Hello: I execute make ,then get error: $ make Making all in libsbml/src make: Entering directory `/home/internet/mydoc/test_pj/libsbml/src' ........ /bin/sh ./libtool --tag=CC --mode=link...
3
35522
by: tvnaidu | last post by:
I compiled tinyxml files (.cpp files) and when I am linking to create final exe in Linux, I am getting lot of errors saying "undefiend reference" to "operator new" and "delete", any idea?. ...
0
7037
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6904
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...
0
7076
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6732
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...
0
6886
tracyyun
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...
0
5324
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4768
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4472
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2990
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.