Lots of problems with String class
Feb 18, 2014 at 7:59pm UTC
I had an assignment where I had to implement a string class and get two test files to run. Long story short, I think I've completely mangled this class. I'm getting a ton of Linked errors, and I have no idea what to do with this program. Could someone help?
There's some separate files, so it's a little long.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
#ifndef STRING_H
#define STRING_H
// String.h
#include <iostream>
using namespace std;
class String {
int size;
char * buffer;
public :
String();
String(const String &);
String(const char *);
~String();
int length() const ;
void resize(unsigned int , char );
void insert(unsigned int , String&);
char & operator [] (unsigned int );
void operator = (const String&);
void operator += (const String&);
// other methods
friend bool operator ==(const String &, const String &);
friend bool operator <=(const String &, const String &);
friend bool operator <(const String &, const String &);
friend ostream & operator <<(ostream &, const String &);
};
#endif
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
#include "String.h"
#include <cassert>
#include <iostream>
String::String()
{
buffer = 0;
resize(0, ' ' );
}
String::String(const String& s)
{
size = s.size;
buffer = new char [size];
for (int i = 0; i < size; i++){
buffer[i] = s.buffer[i];
}
}
String::String(const char *p)
{
int p_size = 0;
for (const char *q = p; *q; q++){
p_size++;
}
buffer = new char [p_size];
for (int i=0;i<p_size;i++){
size = p_size;
}
}
String::~String()
{
delete [] buffer;
}
int String::length()
{
int i = 0;
while (buffer[i] != '\0' ){
i++;
}
return i;
}
void String::resize(unsigned int newlength, char pad)
{
if (buffer == 0){
size = 0;
}
if (newlength < size){
buffer[newlength = '\0' ];
}
else {
int i;
char * newbuffer = new char [newlength + 1];
assert (newbuffer != 0);
for (i=0; i < size && buffer[i] != '\0' ; i++){
newbuffer[i] = buffer[i];
}
for ( ; i < newlength;i++){
newbuffer[i] = pad;
}
newbuffer[i] = '\0' ;
if (buffer != 0){
delete [] buffer;
}
buffer = newbuffer;
size = newlength;
}
}
void String::insert(unsigned int position, String &text)
{
int lgt = length();
int addlength = text.length();
int newlength = lgt + addlength;
resize(newlength, '\0' );
for (int i = lgt; i >= position; i--){
buffer[i + addlength] = buffer[i];
}
for (int i = 0; i < addlength; i++){
buffer[position + i] = text[i];
}
}
char & String::operator [](unsigned int index)
{
assert (index <= size);
return buffer[index];
}
void String::operator = (String & p){
resize(p.length(), ' ' );
strcpy(buffer, p.buffer);
}
void String::operator += (const String & s){
int temp_size=size+s.size;
char *temp;
temp=new char [temp_size];
int i;
for (i=0;i<size;i++)
{
temp[i]=buffer[i];
}
for (int j=0;j<s.size;j++,i++)
{
temp[i]=s.buffer[j];
}
delete []buffer;
size=temp_size;
buffer=temp;
return *this ;
}
}
bool operator ==(const String &S1, const String &S2)
{
if (S1.size == S2.size)
{
int counter1 = S1.size;
int counter2 = 0;
int i = 0;
while (i != counter1)
{
if (S1.buffer[i] != S2.buffer[i]){
counter2++;
}
i++;
}
if (counter2 != 0) {
return false ;
}
else {
return true ;
}
}
else {
return false ;
}
}
bool operator <=(const String &S1, const String &S2){
if (S1.size <= S2.size){
int counter1 = S1.size;
int counter2 = 0;
int i = 0;
while (i != counter1){
if (S1.buffer[i] <= S2.buffer[i]){
counter2++;
}
i++;
}
if (counter2 != 0){
return true ;
}
else {
return false ;
}
}
else {
return false ;
}
}
ostream& operator << ( ostream& os, const String& s)
{
os << s.size;
return os;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
// String_test.cpp
#include <iostream>
#include <cassert>
#include "String.h"
using namespace std;
int main()
{
String s1; // s1 == ""
assert(s1.length() == 0);
String s2("hi" ); // s2 == "hi"
assert(s2.length() == 2);
String s3(s2); // s3 == "hi"
assert(s3.length() == 2);
assert(s3[0] == 'h' );
assert(s3[1] == 'i' );
s1 = s2; // s1 == "hi"
assert(s1 == s2);
s3 = "bye" ; // s3 == "bye"
assert(s3.length() == 3);
assert(s3[0] == 'b' );
assert(s3[1] == 'y' );
assert(s3[2] == 'e' );
s1 += "re" ; // s1 == "hire"
assert(s1 == "hire" );
s1 += "d" ; // s1 == "hired"
assert(not (s1 == "hire" ));
cout << "SUCCESS" << endl;
}
And the errors it's giving me:
[Linker Error] undefined reference to `String::String(char const*)'
[Linker Error] undefined reference to `String::String(char const*)'
[Linker Error] undefined reference to `operator==(String const&, String const&)'
[Linker Error] undefined reference to `operator<=(String const&, String const&)'
[Linker Error] undefined reference to `operator<<(std::ostream&, String const&)'
[Linker Error] undefined reference to `operator<<(std::ostream&, String const&)'
[Linker Error] undefined reference to `operator<<(std::ostream&, String const&)'
[Linker Error] undefined reference to `operator<<(std::ostream&, String const&)'
[Linker Error] undefined reference to `operator<<(std::ostream&, String const&)'
more undefined references to `operator<<(std::ostream&, String const&)' follow
[Linker Error] undefined reference to `String::~String()'
[Linker Error] undefined reference to `String::~String()'
[Linker Error] undefined reference to `String::~String()'
[Linker Error] undefined reference to `String::~String()'
ld returned 1 exit status
Feb 18, 2014 at 8:09pm UTC
Your String_test.cpp should include String.cpp rather than String.h.
After that is fixed, there are a lot of syntax errors in your String class that you need to fix. To mention a few:
51 52 53
if (newlength < size){
buffer[newlength = '\0' ];
}
98 99 100 101
void String::operator += (const String & s){
...
return *this ;
}
Last edited on Feb 18, 2014 at 8:14pm UTC
Feb 18, 2014 at 8:21pm UTC
Smac89 wrote:Your String_test.cpp should include String.cpp rather than String.h.
Don't do that. Learn how to link a program properly instead. If you use an IDE all you normally have to do is to make sure all the files have been added to the same project.
Topic archived. No new replies allowed.