error: expected primary-expression before 'unsigned'

Hi guys,
I've been having a lot of difficulty finding the solution to this specific error that I've been getting and I can't seem to find any good solutions floating around.

Basically, I'm creating my own string class. Here's the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include"CPPString.h"

CPPString::CPPString(unsigned char* s)
{
  int i = 0;
  sPtr = (unsigned char*)malloc(sizeof(unsigned char)*(s->size() + 1));
    while((s[i]) != '\0')
   {
     sPtr[i] = s[i];
     i++;
   }
   sPtr[i] = '\0';

};


And here's the error that I've been getting:

In file included from Main.cpp:4:
CPPString.cpp: In constructor ‘CPPString::CPPString(unsigned char*)’:
CPPString.cpp:6: error: expected primary-expression before ‘unsigned’
make: *** [main] Error 1


The compiler finds the error at what is pictured above as line 3.

From what I have been able to find on other forums and from looking around, this error message seems to be very program-specific so I'm not sure how much everyone would be able to help, but I would appreciate any input!
s->size()

You can't do this.

s points to an unsigned char. unsigned chars do not have a 'size' member function.

If you want to get the length of a C-string, you'll probalby want to call strlen.

Regardless, even if I replace that function with a simple int, I will still get the same error message.

And I cannot use strlen as I can't include string.h, I'll be creating my own methods to deal with stuff like that.
Regardless, even if I replace that function with a simple int, I will still get the same error message


Replacing the whole function with 'int' doesn't make any sense. =P

And I cannot use strlen as I can't include string.h, I'll be creating my own methods to deal with stuff like that.


Then you'll have to write your own function that does the same thing and call it.
In file included from Main.cpp:4:
CPPString.cpp: ...

What...? Are you including a cpp file?
Sorry, I guess I wasn't phrasing it correctly. I meant that I still get the error even if I hardcode in the size of my sPtr.

Meaning:

 
  sPtr = (unsigned char*)malloc(sizeof(unsigned char)*(100));


still gives me the same error, so I'm fairly confident that the s->size() thing was not what was causing said error.
You haven't declared sPtr. Might be whats causing the issue.
Sorry again, the code there isn't the full class - sPtr is declared globally
^What...? Your class requires a globally defined variable??? That's...really bad design. Anyway, I'm not completely sure what the error is now, assuming it's still the same problem. Are you *sure* sPtr is declared?
Okay, another possibilty is you didn't put a semicolon after the curly brace at the end of your class declaration.
Topic archived. No new replies allowed.