string class

Pages: 123
ok this looks great , i got the destructor done. thanks for all the help so far!
hey im getting some funk errors here, any idea?

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
MyString::MyString()
{
	length = 0;
	pData = new char[100];
}

MyString::MyString(char *cString)
{
	pData = cString;
}

MyString::MyString(MyString const& s)
{
	pData = s.pData;
}

MyString::~MyString()
{
	delete pData;
	*pData = NULL;
}

void MyString::Read()
{
	string s1;

	cout << "Enter the string, and press enter." << endl;
	cin >> s1;

	strcpy(pData, s1.c_str()); 

	for (int x = 0; pData[x] != 0; x++)
	{
		length++;
	}
}

void MyString::Put()
{
	cout << "The string is:" << endl;

	for (int x = 0; pData[x] != 0; x++)
	{
		cout << pData[x];
	}
}

void MyString::Reverse()
{
	cout << "The string in reverse is:" << endl;

	for (int x = length - 1; x < 0; x--)
	{
		cout << pData[x];
	}
}

MyString MyString::operator= (MyString const& s)
{
	pData = s.pData;
                length = s.length;
                return *this;
}

bool MyString::operator+ (const MyString &s)
{

}


1>Generating Code...
1>Linking...
1>myStringMain.obj : error LNK2005: "public: __thiscall MyString::MyString(void)" (??0MyString@@QAE@XZ) already defined in myString.obj
1>myStringMain.obj : error LNK2005: "public: __thiscall MyString::MyString(char *)" (??0MyString@@QAE@PAD@Z) already defined in myString.obj
1>myStringMain.obj : error LNK2005: "public: __thiscall MyString::MyString(class MyString const &)" (??0MyString@@QAE@ABV0@@Z) already defined in myString.obj
1>myStringMain.obj : error LNK2005: "public: __thiscall MyString::~MyString(void)" (??1MyString@@QAE@XZ) already defined in myString.obj
1>myStringMain.obj : error LNK2005: "public: void __thiscall MyString::Read(void)" (?Read@MyString@@QAEXXZ) already defined in myString.obj
1>myStringMain.obj : error LNK2005: "public: void __thiscall MyString::Put(void)" (?Put@MyString@@QAEXXZ) already defined in myString.obj
1>myStringMain.obj : error LNK2005: "public: void __thiscall MyString::Reverse(void)" (?Reverse@MyString@@QAEXXZ) already defined in myString.obj
1>myStringMain.obj : error LNK2005: "public: bool __thiscall MyString::operator=(class MyString const &)" (??4MyString@@QAE_NABV0@@Z) already defined in myString.obj
1>myStringMain.obj : error LNK2005: "public: bool __thiscall MyString::operator+(class MyString const &)" (??HMyString@@QAE_NABV0@@Z) already defined in myString.obj
1>C:\Users\Matt\Documents\Visual Studio 2008\Projects\cs210\Debug\cs210.exe : fatal error LNK1169: one or more multiply defined symbols found
Last edited on
Got the implementation of your class? It may be related although I'm not expert enough to say.
what do you mean my implementation? like how i linked them??
No like, the members of your class.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef MYSTRING_H
#define MYSTRING_H

class MyString	
{
private:
	char *pData;	
	int length;

public:
     MyString();	
     MyString(char *cString); 							
     MyString(MyString const& s);  
     ~MyString();
     void Read();
     void Put();		
     void Reverse();
     MyString operator= (MyString const& s);
     bool operator+ (MyString const& s);    
};

#endif   // MYSTRING_H
Last edited on
Post your entire code, including the #include directives, etc. regarding the errors.

For the default constructor, is there a reason why you are allocating space for 100 chars?

In the constructor that takes a C string, you still are not copying the string--it is just storing a pointer to the string passed in as a parameter.

In the destructor, you want to use delete[], not just delete. If you use new to allocate an array it is deleted with delete[].

Worry about these first before moving on to other methods. A review of this tutorial might help:
http://www.cplusplus.com/doc/tutorial/

Last edited on
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
#include"myString.h"
#include <iostream>
#include<string>
#include<cstring>

using namespace std;

MyString::MyString()
{
	length = 0;
	pData = new char[100];
}

MyString::MyString(char *cString)
{
	pData = cString;
}

MyString::MyString(MyString const& s)
{
	pData = s.pData;
}

MyString::~MyString()
{
	delete[] pData;
	*pData = NULL;
}

void MyString::Read()
{
	string s1;

	cout << "Enter the string, and press enter." << endl;
	cin >> s1;

	strcpy(pData, s1.c_str()); 

	for (int x = 0; pData[x] != 0; x++)
	{
		length++;
	}
}

void MyString::Put()
{
	cout << "The string is:" << endl;

	for (int x = 0; pData[x] != 0; x++)
	{
		cout << pData[x];
	}
}

void MyString::Reverse()
{
	cout << "The string in reverse is:" << endl;

	for (int x = length - 1; x < 0; x--)
	{
		cout << pData[x];
	}
}

MyString MyString::operator= (const MyString &s)
{
	pData = s.pData;
                length = s.length;
                return *this;
}

bool MyString::operator+ (const MyString &s)
{
              return true;
}


#include"myString.h"
#include"myString.cpp"
#include<iostream>
#include<string>
#include<cstring>

using namespace std;

int main()
{
	MyString string1;
	
	cout << "My String Class" << endl;
	cout << endl;


	return 0;
}


i just put 100, cause i asssumed it needed a size. so in the constructor do i want to use the derefernce operator?
Last edited on
Start by deleting line "77" that includes the .cpp file.

Those are two files right? myString.cpp: lines 1 - 75; and main.cpp: lines 76 - 94. In order to compile the files with gcc, it would go something like this:

g++ myString.cpp main.cpp
yea we have to use visual studio though... i made some changes to the assignment operator overload the type, and these are the errors im getting...

1>c:\users\matt\documents\visual studio 2008\projects\cs210\cs210\mystring.cpp(66) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\matt\documents\visual studio 2008\projects\cs210\cs210\mystring.cpp(66) : error C2556: 'int MyString::operator =(const MyString &)' : overloaded function differs only by return type from 'MyString MyString::operator =(const MyString &)'
1> c:\users\matt\documents\visual studio 2008\projects\cs210\cs210\mystring.h(18) : see declaration of 'MyString::operator ='
1>c:\users\matt\documents\visual studio 2008\projects\cs210\cs210\mystring.cpp(66) : error C2371: 'MyString::operator =' : redefinition; different basic types
1> c:\users\matt\documents\visual studio 2008\projects\cs210\cs210\mystring.h(18) : see declaration of 'MyString::operator ='
1>c:\users\matt\documents\visual studio 2008\projects\cs210\cs210\mystring.cpp(66) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\matt\documents\visual studio 2008\projects\cs210\cs210\mystring.cpp(66) : error C2556: 'int MyString::operator =(const MyString &)' : overloaded function differs only by return type from 'MyString MyString::operator =(const MyString &)'
1> c:\users\matt\documents\visual studio 2008\projects\cs210\cs210\mystring.h(18) : see declaration of 'MyString::operator ='
1>c:\users\matt\documents\visual studio 2008\projects\cs210\cs210\mystring.cpp(66) : error C2371: 'MyString::operator =' : redefinition; different basic types
1> c:\users\matt\documents\visual studio 2008\projects\cs210\cs210\mystring.h(18) : see declaration of 'MyString::operator ='


1
2
3
4
5
6
7
8
9
MyString MyString::operator= (const MyString& s)
{
	pData = s.pData;
                length = s.length;
                return *this;

}

MyString operator= (const MyString &s);


the six errors are from the assignment operator overload function. thanks for any help.
Last edited on
I'm pretty sure that you mean to deploy the == logical operator not the = assignment operator. In addition the version of the operator that is not a member of your class needs two arguments because the = and == operators are binary. If the operator was a member it would have an implicit argument (this, the left hand operand) but if not it must have 2.
no i have to overload the assignment operator. and im not sure why i was trying to return that but anyways i changed it to so its not returning anything its just pData = s.pData, still same errors. Im trying to go from this site below, but i think im screwing up how im using the pointers in this case..? not sure but still same errors all from assignment operator overload.

http://www.learncpp.com/cpp-tutorial/911-the-copy-constructor-and-overloading-the-assignment-operator/

Well what's the difference between the first operator and the second? And what is the first one's return type?
chewykb24 wrote:
hmm yea i wish my prof actually taught us it would be nice.


Yea unfortunately programming teachers rarely teach us anything, they are simply there for guidence to put us on the path / resources / materials to learn what we need. What we have to figure out as students, is how to teach ourselves.

Actually the best teacher I had (hopefully I still have him this year) is my java teacher, every lesson instead of sitting there telling us to read this or read that, instead he would right up problems on the board, and then we would have 15-30mins to try and solve them, then as we are solving he writes the solution on the board.
This was the best style I found as
1. we had sample solutions that WORKED!
2. we were able to interact somewhat and ask questions if something was not understood.

But for the majority, my god it just made the whole process soooo much easier. Even the complete programming dunces in the class were capable of producing somewhat workable code. (except for the few geniuses who prefered to play video games instead of learning)

And my C++ class was the opposite, no interaction at all (no teacher for the most part)... I only just passed C++. I got a distinction in java on an assignment 3x more complicated than the C++ end of year assignment. And 96/100 on the Java exam.
Last edited on
yea thats the best way i agree much more helpfull... and first one was bool but from what i was reading from the site and what i was given it makes senses to be of the type MyString, im jus not sure how to fix these errors, it talks about definition , redefinition, and im jus not sure where to go. i edited it above to how it looks now.(assignment overload function)
Last edited on
k well i figured the assignment operator out, but im back to some weird errors guessing with how i link it, edited changes of assignment operator above btw. any ideas, thanks. using visual studio (have to)

1>myStringMain.obj : error LNK2005: "public: __thiscall MyString::MyString(void)" (??0MyString@@QAE@XZ) already defined in myString.obj
1>myStringMain.obj : error LNK2005: "public: __thiscall MyString::MyString(char *)" (??0MyString@@QAE@PAD@Z) already defined in myString.obj
1>myStringMain.obj : error LNK2005: "public: __thiscall MyString::MyString(class MyString const &)" (??0MyString@@QAE@ABV0@@Z) already defined in myString.obj
1>myStringMain.obj : error LNK2005: "public: __thiscall MyString::~MyString(void)" (??1MyString@@QAE@XZ) already defined in myString.obj
1>myStringMain.obj : error LNK2005: "public: void __thiscall MyString::Read(void)" (?Read@MyString@@QAEXXZ) already defined in myString.obj
1>myStringMain.obj : error LNK2005: "public: void __thiscall MyString::Put(void)" (?Put@MyString@@QAEXXZ) already defined in myString.obj
1>myStringMain.obj : error LNK2005: "public: void __thiscall MyString::Reverse(void)" (?Reverse@MyString@@QAEXXZ) already defined in myString.obj
1>myStringMain.obj : error LNK2005: "public: class MyString __thiscall MyString::operator=(class MyString const &)" (??4MyString@@QAE?AV0@ABV0@@Z) already defined in myString.obj
1>myStringMain.obj : error LNK2005: "public: bool __thiscall MyString::operator+(class MyString const &)" (??HMyString@@QAE_NABV0@@Z) already defined in myString.obj
1>C:\Users\Matt\Documents\Visual Studio 2008\Projects\cs210\Debug\cs210.exe : fatal error LNK1169: one or more multiply defined symbols found
Last edited on
You probably have code inside a header file that is included in more than one cpp file.
(header files should really only contain declarations - not definitions).

Post your code - there has been a lot of activity regarding your problem - so let's see where we are at this point in time.

hey thanks, yea i figured it out that was the problem, i only needed to include my .h file not the .cpp which included the definitions. jus figuring out some other minor stuff here and there now to get it working.
hey im jus having some trouble with my copy constructor and overloading my assignment operator, not too sure why its not working? I dont get any errors. string2 outputed fine, and then when i made string3 and did the operations with that it messed up output of string2. thanks for help.

EDIT: figured this out ! lol

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
#ifndef MYSTRING_H
#define MYSTRING_H

class MyString	
{
private:
	char *pData;	
	int length;

public:
     MyString();	
     MyString(char *cString); 							
	 MyString(MyString const& s);  
	 ~MyString();
	 void Read();
     void Put();		
	 void Reverse();
	 MyString operator= (MyString const& s);
     bool operator+ (MyString const& s);    
};

#endif   // MYSTRING_H
#include"myString.h"
#include <iostream>
#include<string>
#include<cstring>

using namespace std;

MyString::MyString()
{
	length = 0;
	pData = new char[length + 1];
}

MyString::MyString(char *cString)
{
	pData = cString;
	length = strlen(pData); 
}

MyString::MyString(MyString const& s)
{
	pData = s.pData;
	length = s.length;
}

MyString::~MyString()
{
	delete[] pData;
}

void MyString::Read()
{
	string s1;

	cout << "Enter the string, and press enter." << endl;
	cin >> s1;

	strcpy(pData, s1.c_str()); 

	for (int x = 0; pData[x] != 0; x++)
	{
		length++;
	}
}

void MyString::Put()
{
	cout << "The string is:" << endl;

	for (int x = 0; pData[x] != 0; x++)
	{
		cout << pData[x];
	}
	cout << endl;
}

void MyString::Reverse()
{
	cout << "The string in reverse is:" << endl;

	for (int x = (length - 1); x >= 0; x--)
	{
		cout << pData[x];
	}
	cout << endl;
}

MyString MyString::operator= (MyString const& s)
{
	pData = s.pData;
	length = s.length;
	return *this;
}

bool MyString::operator+ (MyString const& s)
{
	return true;
}


#include"myString.h"
#include<iostream>
#include<string>
#include<cstring>

using namespace std;

int main()
{
	MyString string1;
	
	char *test = new char[6];
	strcpy(test, "greg");
	MyString string2(test);

	MyString string3;
	string3 = string2;
	
	cout << "My String Class" << endl;
	cout << endl;

	string1.Read();
	cout << endl;

	string1.Put();
	cout << endl;

	string1.Reverse();
	cout << endl;

	string2.Put();
	cout << endl;

	string2.Reverse();
	cout << endl;

	string3.Put();
	cout << endl;

	string3.Reverse();
	cout << endl;

	return 0;
}
Last edited on
any idea on how i should do the string concatenation -- (overload + operator)... my brain is kinda stuck , thanks for any. im a little unsure of how i would use that function in the driver because there is only the one operand. thanks for any help.
Pages: 123