Help with errors: C++
Okay, I've struggled with this long enough to risk embarrassing myself.
Help will be appreciated. Here's my newbie code: Code:
barth@bluegospel:~$ g++ ~/cppexperiments/billscheduler.cpp /home/barth/cppexperiments/billscheduler.cpp: In function 'int main()': /home/barth/cppexperiments/billscheduler.cpp:16: error: invalid conversion from 'char*' to 'size_t' /home/barth/cppexperiments/billscheduler.cpp:16: error: initializing argument 1 of 'void* malloc(size_t)' /home/barth/cppexperiments/billscheduler.cpp:16: error: invalid conversion from 'void*' to 'char*' /home/barth/cppexperiments/billscheduler.cpp:17: error: 'memset' was not declared in this scope /home/barth/cppexperiments/billscheduler.cpp:19: error: invalid conversion from 'char*' to 'size_t' /home/barth/cppexperiments/billscheduler.cpp:19: error: initializing argument 3 of 'size_t fread(void*, size_t, size_t, FILE*)' barth@bluegospel:~$ |
Quote:
|
Start from
Quote:
man 3 memset . |
Quote:
Code:
billschedBuffer = malloc(nContentLength+1); Andi |
Here's the code again w/ line numbers:
Code:
|
Quote:
man 3 fread - it contains info explaining the Quote:
|
Sergei, I'm trying to profit from your honest attempt to simplify this without spoon feeding, but I'm just not seeing the answer. A little more help, if I'm not asking too much?
ForzaItalia, I wondered about that too, but the example I'm using as a template is as such: Code:
|
Sergei, were you hinting that I should include <string.h> in my pre-processing directives? I did that and some of my error messages have changed. I also added a few others.
Here's my new code w/ errors: bash-3.1# cat -n cppexperiments/billscheduler.cpp 1 #include <iostream> 2 #include <cstdlib> 3 #include <stdlib.h> 4 #include <cstdio> 5 #include <stdio.h> 6 #include <malloc.h> 7 #include <string.h> 8 #include <stddef.h> 9 10 using namespace std; 11 12 int main() 13 { 14 char* billschedContentLength = getenv("CONTENT_LENGTH"); 15 char* billschedBuffer; 16 int nContentLength = atoi(billschedContentLength); 17 18 billschedBuffer = malloc(billschedContentLength+1); // allocate a buffer 19 memset(billschedBuffer, 0, billschedContentLength+1); // zero it out 20 21 fread(billschedBuffer,1,billschedContentLength,stdin); // get data 22 23 cout << "Content-type: text/html" << endl << endl 24 << "<html>" << endl 25 << "<body>" << endl 26 << "<p>" << endl 27 28 <<billschedBuffer<<""<<endl 29 30 << endl 31 << "" << endl 32 << "" << endl 33 << "" ; 34 35 36 free(billschedBuffer); 37 38 return 0; 39 40 } bash-3.1# g++ cppexperiments/billscheduler.cpp cppexperiments/billscheduler.cpp: In function 'int main()': cppexperiments/billscheduler.cpp:18: error: invalid conversion from 'char*' to 'size_t' cppexperiments/billscheduler.cpp:18: error: initializing argument 1 of 'void* malloc(size_t)' cppexperiments/billscheduler.cpp:18: error: invalid conversion from 'void*' to 'char*' cppexperiments/billscheduler.cpp:19: error: invalid conversion from 'char*' to 'size_t' cppexperiments/billscheduler.cpp:19: error: initializing argument 3 of 'void* memset(void*, int, size_t)' cppexperiments/billscheduler.cpp:21: error: invalid conversion from 'char*' to 'size_t' cppexperiments/billscheduler.cpp:21: error: initializing argument 3 of 'size_t fread(void*, size_t, size_t, FILE*)' bash-3.1# |
Quote:
So, reread the manpage paying attention to the function arguments types, reread the compiler error messages as a hint and recheck in your program whether you are supplying to 'fread' arguments of correct type. |
Quote:
|
Quote:
You still need to use this one: Code:
int nContentLength = atoi(lpszContentLength); EDIT: Hint: The other two error messages have the same cause! Andi |
Bluegospel,
there's a problem with your datatype, try read the manpage for malloc for the expected return data type, change the billschedContentLength from char * to size_t. cheers |
So, since "billschedContentLength+1" on line 18 designates the address location plus one, not it's size, I should rather dereference that pointer, to provide the size of "Content_Length" to malloc, so it can allocate that size of memory, assigning an address to pointer billschedBuffer.
I should also dereference "billschedContentLength" on line 19, rather than using "billschedContentLength+1". This way billschedBuffer is a pointer to an area of memory *billschedContentLength" bytes long, whose cells all contain zero. Finally, do the same for the third argument in fread on line 21. Is this correct? |
Quote:
Code:
char* lpszContentLength = getenv("CONTENT_LENGTH"); |
Sergei, the excerpt you most recently posted is actually from the tutorial I'm using as a model.
Here are my lines, corresponding to the lines you reference from the model: Code:
14 char* billschedContentLength = getenv("CONTENT_LENGTH"); I'm guessing the obvious error is that, since "billschedContentLength+1" on line 18 designates the address location plus one, not it's size, I should rather dereference that pointer, to provide the size of "Content_Length" to malloc, so it can allocate that size of memory, assigning an address to pointer billschedBuffer. Am I in the right ballpark? |
All times are GMT -5. The time now is 02:50 PM. |