943,744 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 4486
  • C++ RSS
Dec 3rd, 2008
0

Help Need for Resolving a C++ error C2664

Expand Post »
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:

C++ Syntax (Toggle Plain Text)
  1. const char* article[]={"the", "a", "one", "some", "every", "any"};
  2. const char* noun[]={"boy", "girl", "monkey", "LU", "car"};
  3. const char* verb[]={"drove", "jumped", "ran", "walked", "bit", "slithered"};
  4. const char* prep[]={"to", "from", "over", "under", "on"};
  5. ...
  6. sentence = article[(1 + rand()%getSize(article))] + " " +
  7. noun[(1 + rand()%getSize(noun))] + " " +
  8. verb[(1 + rand()%getSize(verb))] + " " +
  9. prep[(1 + rand()%getSize(prep))] + " " +
  10. article[(1 + rand()%getSize(article))] + " " +
  11. noun[(1 + rand()%getSize(noun))] + ".";
  12. ...
  13. int getSize(const char* s)
  14. {
  15. int size;
  16. for (size=0; *s != '/0'; s++)
  17. size++;
  18. return size;
  19. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
acperson is offline Offline
20 posts
since Dec 2008
Dec 3rd, 2008
0

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().
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Dec 3rd, 2008
0

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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
acperson is offline Offline
20 posts
since Dec 2008
Dec 4th, 2008
0

Re: Help Need for Resolving a C++ error C2664

Try..
C++ Syntax (Toggle Plain Text)
  1.  
  2. strlen(article[0]); //or
  3. getSize(article[0]); //???
Reputation Points: 47
Solved Threads: 69
Posting Whiz
cikara21 is offline Offline
340 posts
since Jul 2008
Dec 4th, 2008
0

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))]
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Dec 4th, 2008
0

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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
acperson is offline Offline
20 posts
since Dec 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: what is the execution sequence of this code?
Next Thread in C++ Forum Timeline: Debug Assertion Failed! Error - how do I fix this?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC