Constructor

Apr 21, 2019 at 6:43am
I am starting a new project on classes with dynamic memory and I am trying to make sure that I correctly understand the set up of the constructor.

The instructions:

Construction of a MyString from a const c-string. You should copy the string data, not just store a pointer to an argument passed to the constructor. Constructing a MyString with no arguments creates an empty MyString object (i.e. ""). A MyString object should be implemented efficiently (space-wise) which is to say you should not have a fixed-size buffer of chars, but instead allocate space for chars on an as-needed basis. Use strcpy().

I am not sure of the differences between, or how it would look, "copy" and "store as a pointer." Here is the code that I started. I went this route because it is similar to what the instructor has done but it uses a pointer so I am unsure about that based on the instructions and I get this error.

error C2679: binary '<<': no operator found which takes a right-hand operand of type 'cs_mystring::MyString' (or there is no acceptable conversion)



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

// in main
MyString s1("hello");

	cout << s1;

}




namespace cs_mystring
{
	class MyString
	{
	public:
		MyString(const char *inString);

	private:
		char *whatisthis;
	};
}





MyString::MyString()
	{
		whatisthis = new char[1];
		strcpy(whatisthis, "");
	}

 
	
Apr 21, 2019 at 7:11am
stoneJax wrote:
cout << s1;

If you want it to be possible to print MyString objects this way you need to overload the << operator. That's what the error is about.
Last edited on Apr 21, 2019 at 7:11am
Apr 21, 2019 at 7:18am
Ok thank you. What do you think about the set up of the constructor? The difference between how he says to copy and not store a pointer.
Apr 21, 2019 at 7:39am
I added the overloaded << operator and there is an error that says:

error C2447: '{': missing function header (old-style formal list?)

I have only done one overloaded << function before and I set this one up very similar so I am not sure what's wrong with it.

Here is my updated code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

class MyString
	{
	public:
		MyString();
		friend std::ostream& operator << (std::ostream& out, const MyString& right);

	private:
		char *whatisthis;
	};







std::ostream& operator << (std::ostream& out, const MyString& right);
	{
		out << right.whatisthis;
		return out;
	}
Last edited on Apr 21, 2019 at 7:41am
Apr 21, 2019 at 7:45am
There is a semi colon in the definition. I must be getting too tired.
Apr 21, 2019 at 10:03pm
stoneJax wrote:
I am not sure of the differences between, or how it would look, "copy" and "store as a pointer."
1
2
3
	int b=5;
	int *c=&b; // not like this
	int a=b; // like this 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <cstring> // for strcpy()
using namespace std;

class po{
	public:
		char ss[100]; //its size must be bigger than size of source
		po(){
			ss[0]='\0';
		}
		po(const char *str){
			strcpy(ss, str);
		}

};

int main () {
	char line[]="it's a line.";
	po obj=line;
	cout << obj.ss;
	return 0;
}
Topic archived. No new replies allowed.