Problem implementing member function

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 502 Views - Last Post: 25 October 2011 - 12:01 PM Rate Topic: -----

#1 Rumsy  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 26
  • Joined: 30-January 11

Problem implementing member function

Posted 23 October 2011 - 08:36 PM

Hello, I'm attempting to utilize the member function in my program (my character array constructor to be specific) that returns the length of an array of characters so I can use that length to allocate the appropriate memory. The error I keep getting is as follows:

"error: request for member 'length' in 'a', which is of non-class type 'const char*'"

Length Function:
int my_string::length() const    
{
  int i=0;
  while(i<capacity && ptr[i]!='\0')
  {
    ++i;
  }
  return i;
}



Constructor:
my_string::my_string(const char* a)
{
  capacity = a.length();
  ptr = new char[capacity];
}



Prototypes in header:
my_string(const char*);
int length() const;



As always any help/tips would be appreciated. (I already know 'a' is not a great variable name. :whistling: )

Is This A Good Question/Topic? 0
  • +

Replies To: Problem implementing member function

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1329
  • View blog
  • Posts: 4,555
  • Joined: 19-February 09

Re: Problem implementing member function

Posted 23 October 2011 - 09:41 PM

The data type char * means it is a C type string.

3	  capacity = a.length();


So :

my_string::my_string(const char* a)
{
  capacity = strlen(a); 

  // or
  string str(a);
  capacity = str.length();




You might need extra 1 for null character, depends on design where that is implemented.

This post has been edited by #define: 23 October 2011 - 09:42 PM

Was This Post Helpful? 0
  • +
  • -

#3 Rumsy  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 26
  • Joined: 30-January 11

Re: Problem implementing member function

Posted 23 October 2011 - 09:43 PM

View Post#define, on 23 October 2011 - 09:41 PM, said:

The data type char * means it is a C type string.

3	  capacity = a.length();


So :

my_string::my_string(const char* a)
{
  capacity = strlen(a); 

  // or
  string str(a);
  capacity = str.length();



Yeah I'm aware of strlen but this was for an assignment (a very late assignment now), so I'm supposed to use my own function. Thanks.
Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1329
  • View blog
  • Posts: 4,555
  • Joined: 19-February 09

Re: Problem implementing member function

Posted 23 October 2011 - 09:50 PM

Do you mean :-

my_string::my_string(my_string a)
{


Was This Post Helpful? 0
  • +
  • -

#5 Rumsy  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 26
  • Joined: 30-January 11

Re: Problem implementing member function

Posted 23 October 2011 - 09:54 PM

View Post#define, on 23 October 2011 - 09:50 PM, said:

Do you mean :-

my_string::my_string(my_string a)
{



I don't believe so, I already have a copy constructor.
Was This Post Helpful? 0
  • +
  • -

#6 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1329
  • View blog
  • Posts: 4,555
  • Joined: 19-February 09

Re: Problem implementing member function

Posted 23 October 2011 - 10:20 PM

Your length() method would need a parameter :

int my_string::length(const char* ptr) const
{
  ... 



So :

my_string::my_string(const char* a)
{
  capacity = my_string::length(a); 



Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5800
  • View blog
  • Posts: 12,636
  • Joined: 16-October 07

Re: Problem implementing member function

Posted 24 October 2011 - 04:51 AM

View PostRumsy, on 24 October 2011 - 12:43 AM, said:

Yeah I'm aware of strlen but this was for an assignment (a very late assignment now), so I'm supposed to use my own function.


The point is that const char* a is just an array. It has no methods, no .length(). If you want the length you either use the c-string method strlen or write you own code to loop through the array until you hit the end.
Was This Post Helpful? 1
  • +
  • -

#8 Rumsy  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 26
  • Joined: 30-January 11

Re: Problem implementing member function

Posted 24 October 2011 - 06:33 PM

View Postbaavgai, on 24 October 2011 - 04:51 AM, said:

View PostRumsy, on 24 October 2011 - 12:43 AM, said:

Yeah I'm aware of strlen but this was for an assignment (a very late assignment now), so I'm supposed to use my own function.


The point is that const char* a is just an array. It has no methods, no .length(). If you want the length you either use the c-string method strlen or write you own code to loop through the array until you hit the end.


I was under the impression that I could use my length function to do that, I guess I'm just not quite sure how to use it.

int my_string::length(const char* ptr) const                                
{                                                             
  int i=0;                                                    
  while(i<capacity && ptr[i]!='\0')                           
  {                                                           
    ++i;                                                      
  }                                                           
  return i;                                                   
}                                                             


Was This Post Helpful? 0
  • +
  • -

#9 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1329
  • View blog
  • Posts: 4,555
  • Joined: 19-February 09

Re: Problem implementing member function

Posted 24 October 2011 - 09:35 PM

View PostRumsy, on 25 October 2011 - 04:33 AM, said:

I was under the impression that I could use my length function to do that, I guess I'm just not quite sure how to use it.


You need two functions.

One returns the length of the data string in your class, ...

// return length of data string in class
int my_string::length() const                                
{                                                             
  ...
}                                                             



... the other the length of any char string.

// return length of any C string
int my_string::length(char * str) const                                
{                                                             
  ...
}                                                             



The second function could be a member function (edit: it might need to be a friend) or a global function.
When you have the general function you can use it to find the length of the data in the class.

Edit: I'll check out the second function.

This post has been edited by #define: 24 October 2011 - 10:31 PM

Was This Post Helpful? 0
  • +
  • -

#10 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5800
  • View blog
  • Posts: 12,636
  • Joined: 16-October 07

Re: Problem implementing member function

Posted 25 October 2011 - 02:48 AM

View PostRumsy, on 24 October 2011 - 09:33 PM, said:

I was under the impression that I could use my length function to do that


You can use that logic. The problem is that in the constructor you also need similar logic. You class length has an extra limitation where it checks for capacity, which is fine. But initially you need something that doesn't.

I'd do something like:
class my_string {
public:
	my_string(const char*);
	~my_string();
	int length() const;
	// more stuff here
private:
	char *ptr;
	int capacity;
};

static int get_length(const char* ptr, int max_size=0) {
	int i = 0;
	while(ptr[i]!='\0') { 
		++i;
		if (max_size!=0 && i==max_size) { break; }
	}
	return i;
}

my_string::my_string(const char* a) {
	capacity = get_length(a);
	ptr = new char[capacity + 1];
	// you should now be doing some copying
}

my_string::~my_string() { delete [] ptr; }

int my_string::length() const {
	return get_length(ptr, capacity);
}



Note, you still need more stuff in your constructor. You also need some more methods so you can actually use that string.

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#11 Ricky65  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 115
  • Joined: 03-June 10

Re: Problem implementing member function

Posted 25 October 2011 - 04:51 AM

View Postbaavgai, on 24 October 2011 - 04:51 AM, said:

The point is that const char* a is just an array. It has no methods, no .length(). If you want the length you either use the c-string method strlen or write you own code to loop through the array until you hit the end.


Technically this isn't true. const char* a is just a pointer. If it were an array we could use sizeof() on it and wouldn't have to use strlen or any other hand rolled function.
Was This Post Helpful? 0
  • +
  • -

#12 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5800
  • View blog
  • Posts: 12,636
  • Joined: 16-October 07

Re: Problem implementing member function

Posted 25 October 2011 - 06:39 AM

Technically, you can't pass an array, so it's a moot point. In context, it is an array.

Also, sizeof would only tell you the memory allocated to an array and not the actual size of the string it contains, which could be significantly smaller.
Was This Post Helpful? 0
  • +
  • -

#13 Ricky65  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 115
  • Joined: 03-June 10

Re: Problem implementing member function

Posted 25 October 2011 - 08:18 AM

View Postbaavgai, on 25 October 2011 - 06:39 AM, said:

Technically, you can't pass an array, so it's a moot point. In context, it is an array.

Actually you can pass an array by value if you enclose it in a structure:

template <typename T, std::size_t m>
struct carray_int {
T values[m];
int& operator[](std::size_t n) {
assert(n < m);
return values[n];
}
const int& operator[](std::size_t n) const {
assert(n < m);
return values[n];
}
};


It's not a "moot point". Its semantics doesn't change just by the context it is used. It's a pointer and not an array. There is nothing to argue about. The fact that an array decays to a pointer is irrelevant here.
Why can't you accept you were wrong? You're doing people on here a disservice by providing incorrect information in this instance.

View Postbaavgai, on 25 October 2011 - 06:39 AM, said:

Also, sizeof would only tell you the memory allocated to an array and not the actual size of the string it contains, which could be significantly smaller.

True, I mentioned sizeof to illustrate how a const char pointer is not a const char array. My point was that you can't use sizeof on a pointer to determine the number of bytes allocated as it returns the size of the pointer instead which in this case is not very useful. If const char * a was indeed an array, as you keep incorrectly stating, sizeof would return the number of bytes allocated.
Was This Post Helpful? 0
  • +
  • -

#14 jimblumberg  Icon User is offline

  • member icon


Reputation: 4018
  • View blog
  • Posts: 12,400
  • Joined: 25-December 09

Re: Problem implementing member function

Posted 25 October 2011 - 08:41 AM

Quote

Actually you can pass an array by value if you enclose it in a structure:

But then you are not passing an array you are passing a structure, big difference.

Jim
Was This Post Helpful? 0
  • +
  • -

#15 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5800
  • View blog
  • Posts: 12,636
  • Joined: 16-October 07

Re: Problem implementing member function

Posted 25 October 2011 - 08:57 AM

View PostRicky65, on 25 October 2011 - 11:18 AM, said:

Why can't you accept you were wrong? You're doing people on here a disservice by providing incorrect information in this instance.


Probably not as much as your pedantic insistence on bringing up things that aren't relevant to the problem at hand... :P

Yes, const char *a is clearly a pointer. Stating that "a" is an array is "technically" incorrect. ( There, happy? ) It is a pointer that presumably, but not necessarily, references data that represents a c-string. Do you believe this clarifies things for the OP, because I sure as hell don't.

Referring to the instance of an object referenced via a pointer as being that object is simple language. e.g. foo(int *x, int *y); here you are passing two ints. No, wait, in this code you have passed pointers that are said to reference blocks of memory that can consist of one or more values of type int. However, if the value of the pointer is NULL, then it is said to be...

At a certain level all abstraction is metaphor, not technical specification, for the student. I would prefer to err on the side of understanding. Most, but clearly not all, instructors would.

This post has been edited by baavgai: 25 October 2011 - 08:58 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2