![]() |
| ||
| Help Need for Resolving a C++ error C2664 I wrote a program for a class assignment that is supposed to use random number generator to produce sentences. The program has to use four arrays of (pointers to char) and each chosen word must be concatenated to the previous words in an array that has to be able to hold an entire sentence. The sentence also has to begin in a capital letter and end with a period. The number of letters in the array cannot be pre-counted so that the program can be easily modifiable. Well, I wrote the program, resolved most of the build errors, and then I got 6 occurrences of error C2664. I've tried multiple methods of resolving this and I can't figure it out (probably because this is only my second semester using C++ and it's my independent study class). Would you happen to know how to fix this? Thanks so much! .\main.cpp(32) : error C2664: 'getSize' : cannot convert parameter 1 from 'const char *[6]' to 'const char *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast The code for the line (and function) are as follows: const char* article[]={"the", "a", "one", "some", "every", "any"}; |
| ||
| Re: Help Need for Resolving a C++ error C2664 you are attempting to pass an array of strings to getSize(), but it only expects an array of characters. getSize() is nothing more or less than your own version of the standard C library function strlen(). |
| ||
| Re: Help Need for Resolving a C++ error C2664 Thanks so much for taking the time to reply! But strlen() gave me the exact same error when I tried that. That's why I made my own getSize(). Is there something wrong with my array? I'm not one-hundred percent sure about using arrays of pointers to char because I haven't fully understood it yet. Am I missing some key point here that would help me figure out/resolve this error? Thank you! |
| ||
| Re: Help Need for Resolving a C++ error C2664 Try..
|
| ||
| Re: Help Need for Resolving a C++ error C2664 > const char* article[]={"the", "a", "one", "some", "every", "any"} Is (for the compiler) const char* article[6]={"the", "a", "one", "some", "every", "any"} You cannot write a function which will tell you the answer 6. You can do this, which will give you 6 sizeof(article)/sizeof(article[0]) Which can be conveniently expressed as a macro #define ASIZE(x) (sizeof(x)/sizeof(x[0])) You then write sentence = article[(rand()%ASIZE(article))] |
| ||
| Re: Help Need for Resolving a C++ error C2664 Thank you very much, Salem! That worked brilliantly! I applied it to the rest of the program and now have some very interesting sentences! Complete success! |
| All times are GMT -4. The time now is 5:36 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC