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: - #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: - #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: - #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;
-
}
-
4 1527
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?
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
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.
Thank you very much weknessforcats. It works properly this time
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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.
|
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...
|
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...
|
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*)':...
| |
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...
|
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...
|
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;
|
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...
|
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?.
...
|
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,...
| |
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: 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...
|
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...
|
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,...
|
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...
| |
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...
|
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...
| |