strlen

I am trying to write a member function that returns the length of a dynamically allocated c-string. I have some code up but it is not correct and I get a warning saying: warning C4018: '<': signed/unsigned mismatch.

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

namespace cs_mystring
{
	class MyString
	{
	public:
		MyString();
		MyString(const char* inString);
		MyString(const MyString& right);
		~MyString();
		friend std::ostream& operator << (std::ostream& out, const MyString& right);
		MyString operator = (const MyString& right);
		int getStringLength();
		

	private:
		char *ptr;
	};
}






  int MyString::getStringLength()
	{
		size_t stringSize = 0;
		stringSize = strlen(ptr);

		return stringSize;
	}
We do not see operator< used in your code, yet the error message refers to '<'.

1
2
3
4
5
int MyString::getStringLength()
{
  size_t stringSize;
  return stringSize;
}

You convert a size_t value into int value.
resist the bloat.

1
2
3
4
size_t  MyString::getStringLength()
	{	
		return strlen(ptr);
	}
That was it. Thank you so much!
Topic archived. No new replies allowed.