undefined reference

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Barbara Baby
    New Member
    • Oct 2011
    • 21

    undefined reference

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

    Hi, here is the class definition:
    Code:
    #ifndef MYSTRING_H
    #define MYSTRING_H
    #include <iostream>
    #include <cstring>
    using namespace std;
    class MyString
    {
    //friend functions
    friend ostream& operator << (ostream& leftOp, const MyString& rightOp);
    friend istream& operator >> (istream& leftOp, MyString& rightOp);
    friend bool operator == (const char* leftOp, const MyString& rightOp);
    public:
    //methods
    MyString();
    MyString(const char* s);
    bool operator == (const MyString& rightOp) const;
    bool operator == (const char* rightOp) const;
    char operator [] (int sub) const;
    char& operator [] (int sub);
    const char* c_str() const;
    bool empty() const;
    int size() const;
    void clear();
    private:
    //data memebers
    char StringArray[81]; //to store string
    int StringSize; //current size of the string
    static const int StringCapacity = 80; //maximum size
    };
    #endif
    Here is the class implementation:

    Code:
    #include "stdafx.h"
    #include "MyString.h"
    //no argument constructor
    //initializes the string array with null value
    //and size to zero
    MyString::MyString()
    {
    strcpy(StringArray, "");
    StringSize = 0;
    }
    //argumented constructor
    //initializes string array with passed argument
    MyString::MyString(const char* s)
    {
    int len = strlen(s);
    if(len <= StringCapacity) //size <= 80
    {
    strcpy(StringArray, s);
    StringSize = len;
    }
    else //size > 80
    {
    int i;
    for(i=0;i<=StringCapacity;i++)
    StringArray[i] = s[i];
    StringArray[i] = '\0';
    StringSize = StringCapacity;
    }
    }
    //overloded equal operator with MyString argument
    bool MyString::operator==(const MyString& rightOp)const
    {
    return (strcmp(this->StringArray, rightOp.StringArray) == 0);
    }
    //overloded equal operator with charcter string
    bool MyString::operator==(const char* rightOp)const
    {
    return (strcmp(this->StringArray, rightOp) == 0);
    }
    //overloaded indexing operator
    //returns character at the specified position in the array
    char MyString::operator[](int sub) const
    {
    if ( sub >= 0 && sub <= StringSize)
    return StringArray[sub];
    else
    return ' ';
    }
    //overloaded indexing operator
    //returns reference of the character at the specified position in the array
    char& MyString::operator [] (int sub)
    {
    return StringArray[sub];
    }
    //returns StringArray
    const char* MyString::c_str()const
    {
    return StringArray;
    }
    //returns true if size of StringArray is zero
    //otherwise false
    bool MyString::empty()const
    {
    return (StringSize == 0);
    }
    //returns size of the StringArray
    int MyString::size() const
    {
    return StringSize;
    }
    //clears the StringArray
    void MyString::clear()
    {
    strcpy(StringArray, "");
    StringSize = 0;
    }
    //friend function definitions
    //overloaded extraction operator
    ostream& operator <<(ostream& leftOp, const MyString& rightOp)
    {
    leftOp << rightOp.c_str() <<endl;
    return leftOp;
    }
    //overloaded insertion operator
    istream& operator >> (istream& leftOp, MyString& rightOp)
    {
    char st[81];
    leftOp >> st;
    MyString s(st);
    rightOp = s;
    return leftOp;
    }
    //overloaded equality operator with reference of MyString argument
    //and a charcter string
    bool operator == (const char* leftOp, const MyString& rightOp)
    {
    return (strcmp(leftOp, rightOp.c_str()) == 0);
    }
    Here is the main program:

    Code:
    #include <iostream>
    #include <cstring>
    #include "MyString.h"
    
    using std::cin;
    using std::cout;
    using std::endl;
    
    int main()
       {
       cout << "Testing default constructor\n\n";
    
       MyString s1;
       
       cout << "s1: " << s1 << endl;   
       cout << "s1 size: " << s1.size() << endl;
       cout << "s1 is " << ((s1.empty()) ? "empty\n" : "not empty\n");
       cout << endl;
       
       cout << "Testing second constructor\n\n";
       
       MyString s2 = "some text";
       
       cout << "s2: " << s2 << endl;   
       cout << "s2 size: " << s2.size() << endl;
       cout << "s2 is " << ((s2.empty()) ? "empty\n" : "not empty\n");
       cout << endl;
       
       cout << "Testing size limit on second constructor\n\n";
       
       MyString s3 = "This is a really long string and not all of it will actually end up in the array, but that is okay";
       
       cout << "s3: " << s3 << endl;   
       cout << "s3 size: " << s3.size() << endl;
       cout << "s3 is " << ((s3.empty()) ? "empty\n" : "not empty\n");
       cout << endl;
    
       cout << "Testing write form of subscript operator\n\n";
       
       s2[0] = 'S';
       s2[5] = 'T';
          
       cout << "Testing read form of subscript operator\n\n";
       
       cout << "s2: ";
       for (int i = 0; i < s2.size(); i++)
          cout << s2[i];
       cout << endl << endl;
    
       cout << "Testing conversion to C string\n\n";
       
       char ar[81];
       
       strcpy(ar, s2.c_str());
       
       cout << "s2 as C string: " << ar << endl << endl;
    
       cout << "Testing equality operators\n\n";
    
       MyString s4 = "Some Text";
       
       cout << "s2 and s4 are " << ((s2 == s4) ? "equal\n" : "not equal\n");
       cout << "s3 and s4 are " << ((s3 == s4) ? "equal\n" : "not equal\n");
       cout << "s4 and \"Some Text\" are " << ((s4 == "Some Text") ? "equal\n" : "not equal\n");
       cout << "\"More Text\" and s4 are " << (("More Text" == s4) ? "equal\n" : "not equal\n");
       cout << endl;
    
       cout << "Testing clear() method\n\n";
    
       s3.clear();
       
       cout << "s3: " << s3 << endl;   
       cout << "s3 size: " << s3.size() << endl;
       cout << "s3 is " << ((s3.empty()) ? "empty\n" : "not empty\n");
       cout << endl;
       
       cout << "Testing stream extraction operator\n\n";
       
       cout << "Type a word: ";
       cin >> s1;
       
       cout << "\ns1: " << s1 << endl;   
       cout << "s1 size: " << s1.size() << endl;
       cout << "s1 is " << ((s1.empty()) ? "empty\n" : "not empty\n");
    
       return 0;
       }
    Last edited by Niheel; Oct 7 '11, 08:28 PM.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    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?

    Comment

    • Barbara Baby
      New Member
      • Oct 2011
      • 21

      #3
      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

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Try this:

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

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

        If there are the problem is in the makefile.

        Comment

        • Barbara Baby
          New Member
          • Oct 2011
          • 21

          #5
          Thank you very much weknessforcats. It works properly this time

          Comment

          Working...