| | |
Help Need for Resolving a C++ error C2664
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Dec 2008
Posts: 20
Reputation:
Solved Threads: 0
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:
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:
C++ Syntax (Toggle Plain Text)
const char* article[]={"the", "a", "one", "some", "every", "any"}; const char* noun[]={"boy", "girl", "monkey", "LU", "car"}; const char* verb[]={"drove", "jumped", "ran", "walked", "bit", "slithered"}; const char* prep[]={"to", "from", "over", "under", "on"}; ... sentence = article[(1 + rand()%getSize(article))] + " " + noun[(1 + rand()%getSize(noun))] + " " + verb[(1 + rand()%getSize(verb))] + " " + prep[(1 + rand()%getSize(prep))] + " " + article[(1 + rand()%getSize(article))] + " " + noun[(1 + rand()%getSize(noun))] + "."; ... int getSize(const char* s) { int size; for (size=0; *s != '/0'; s++) size++; return size; }
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().
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Dec 2008
Posts: 20
Reputation:
Solved Threads: 0
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!
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!
> 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
Which can be conveniently expressed as a macro
You then write
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))] ![]() |
Other Threads in the C++ Forum
- Previous Thread: what is the execution sequence of this code?
- Next Thread: Debug Assertion Failed! Error - how do I fix this?
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph homeworkhelp homeworkhelper iamthwee ifstream input int integer lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






