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: acperson is an unknown quantity at this point 
Solved Threads: 0
acperson acperson is offline Offline
Newbie Poster

Help Need for Resolving a C++ error C2664

 
0
  #1
Dec 3rd, 2008
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:

  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. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,342
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

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

 
0
  #2
Dec 3rd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 20
Reputation: acperson is an unknown quantity at this point 
Solved Threads: 0
acperson acperson is offline Offline
Newbie Poster

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

 
0
  #3
Dec 3rd, 2008
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!
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 320
Reputation: cikara21 is an unknown quantity at this point 
Solved Threads: 63
cikara21's Avatar
cikara21 cikara21 is offline Offline
Posting Whiz

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

 
0
  #4
Dec 4th, 2008
Try..
  1.  
  2. strlen(article[0]); //or
  3. getSize(article[0]); //???
.:-cikara21-:.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

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

 
0
  #5
Dec 4th, 2008
> 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))]
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 20
Reputation: acperson is an unknown quantity at this point 
Solved Threads: 0
acperson acperson is offline Offline
Newbie Poster

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

 
0
  #6
Dec 4th, 2008
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!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC